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" |
| 34 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 35 | #ifndef _WIN32 |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 36 | #include <libgen.h> |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 37 | #include <pwd.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 38 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 39 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 40 | #include <termios.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 41 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 43 | #include <sys/resource.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 44 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 45 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 46 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 47 | #include <arpa/inet.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 48 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 49 | #include <netdb.h> |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 50 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_BSD |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 53 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 54 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 55 | #else |
| 56 | #include <util.h> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 57 | #endif |
Aurelien Jarno | bbe813a | 2009-11-30 15:42:59 +0100 | [diff] [blame] | 58 | #else |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 59 | #ifdef __linux__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 60 | #include <pty.h> |
| 61 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 62 | #include <linux/rtc.h> |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 63 | #include <sys/prctl.h> |
ths | bd494f4 | 2007-09-16 20:03:23 +0000 | [diff] [blame] | 64 | |
| 65 | /* For the benefit of older linux systems which don't supply it, |
| 66 | we use a local copy of hpet.h. */ |
| 67 | /* #include <linux/hpet.h> */ |
| 68 | #include "hpet.h" |
| 69 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 70 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 71 | #include <linux/parport.h> |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 72 | #endif |
| 73 | #ifdef __sun__ |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 74 | #include <sys/stat.h> |
| 75 | #include <sys/ethernet.h> |
| 76 | #include <sys/sockio.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 77 | #include <netinet/arp.h> |
| 78 | #include <netinet/in.h> |
| 79 | #include <netinet/in_systm.h> |
| 80 | #include <netinet/ip.h> |
| 81 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 82 | #include <netinet/udp.h> |
| 83 | #include <netinet/tcp.h> |
| 84 | #include <net/if.h> |
| 85 | #include <syslog.h> |
| 86 | #include <stropts.h> |
Blue Swirl | 8d32cf0 | 2009-10-02 19:32:12 +0000 | [diff] [blame] | 87 | /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for |
| 88 | discussion about Solaris header problems */ |
| 89 | extern int madvise(caddr_t, size_t, int); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 90 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 91 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 92 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 93 | |
blueswir1 | 9892fbf | 2008-08-24 10:34:20 +0000 | [diff] [blame] | 94 | #if defined(__OpenBSD__) |
| 95 | #include <util.h> |
| 96 | #endif |
| 97 | |
ths | 8a16d27 | 2008-07-19 09:56:24 +0000 | [diff] [blame] | 98 | #if defined(CONFIG_VDE) |
| 99 | #include <libvdeplug.h> |
| 100 | #endif |
| 101 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 102 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 103 | #include <windows.h> |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 104 | #include <mmsystem.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 107 | #ifdef CONFIG_SDL |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 108 | #if defined(__APPLE__) || defined(main) |
Stefan Weil | 6693665 | 2009-06-13 13:19:11 +0200 | [diff] [blame] | 109 | #include <SDL.h> |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 110 | int qemu_main(int argc, char **argv, char **envp); |
| 111 | int main(int argc, char **argv) |
| 112 | { |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 113 | return qemu_main(argc, argv, NULL); |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 114 | } |
| 115 | #undef main |
| 116 | #define main qemu_main |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 117 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 118 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 119 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_COCOA |
| 121 | #undef main |
| 122 | #define main qemu_main |
| 123 | #endif /* CONFIG_COCOA */ |
| 124 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 125 | #include "hw/hw.h" |
| 126 | #include "hw/boards.h" |
| 127 | #include "hw/usb.h" |
| 128 | #include "hw/pcmcia.h" |
| 129 | #include "hw/pc.h" |
| 130 | #include "hw/audiodev.h" |
| 131 | #include "hw/isa.h" |
| 132 | #include "hw/baum.h" |
| 133 | #include "hw/bt.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 134 | #include "hw/watchdog.h" |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 135 | #include "hw/smbios.h" |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 136 | #include "hw/xen.h" |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 137 | #include "hw/qdev.h" |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 138 | #include "hw/loader.h" |
aurel32 | 5ef4efa | 2009-03-10 21:43:35 +0000 | [diff] [blame] | 139 | #include "bt-host.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 140 | #include "net.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 141 | #include "net/slirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 142 | #include "monitor.h" |
| 143 | #include "console.h" |
| 144 | #include "sysemu.h" |
| 145 | #include "gdbstub.h" |
| 146 | #include "qemu-timer.h" |
| 147 | #include "qemu-char.h" |
| 148 | #include "cache-utils.h" |
| 149 | #include "block.h" |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 150 | #include "block_int.h" |
| 151 | #include "block-migration.h" |
blueswir1 | a718ace | 2009-03-28 08:24:44 +0000 | [diff] [blame] | 152 | #include "dma.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 153 | #include "audio/audio.h" |
| 154 | #include "migration.h" |
| 155 | #include "kvm.h" |
| 156 | #include "balloon.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 157 | #include "qemu-option.h" |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 158 | #include "qemu-config.h" |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 159 | #include "qemu-objects.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 160 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 161 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 162 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 163 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 164 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 165 | #include "qemu_socket.h" |
| 166 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 167 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 168 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 169 | #include "qemu-queue.h" |
| 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 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 176 | #define MAX_VIRTIO_CONSOLES 1 |
| 177 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 178 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 179 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 180 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 181 | to store the VM snapshots */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 182 | struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives); |
| 183 | struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 184 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 185 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 186 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 187 | const char* keyboard_layout = NULL; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 188 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 189 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 190 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 191 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 192 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 193 | static int rtc_utc = 1; |
| 194 | static int rtc_date_offset = -1; /* -1 means no change */ |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 195 | QEMUClock *rtc_clock; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 196 | int vga_interface_type = VGA_NONE; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 197 | #ifdef TARGET_SPARC |
| 198 | int graphic_width = 1024; |
| 199 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 200 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 201 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 202 | int graphic_width = 800; |
| 203 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 204 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 205 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 206 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 207 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 208 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 209 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 210 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 211 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 212 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 213 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 214 | #ifdef TARGET_I386 |
| 215 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 216 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 217 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 218 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 219 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 220 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 221 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 222 | int smp_cores = 1; |
| 223 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 224 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 225 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 226 | int no_hpet = 0; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 227 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 228 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 229 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 230 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 231 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 232 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 233 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 234 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 235 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 236 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 237 | const char *option_rom[MAX_OPTION_ROMS]; |
| 238 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 239 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 240 | #ifdef TARGET_ARM |
| 241 | int old_param = 0; |
| 242 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 243 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 244 | int alt_grab = 0; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 245 | int ctrl_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 246 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 247 | unsigned int nb_prom_envs = 0; |
| 248 | const char *prom_envs[MAX_PROM_ENVS]; |
| 249 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 250 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 251 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 252 | int nb_numa_nodes; |
| 253 | uint64_t node_mem[MAX_NODES]; |
| 254 | uint64_t node_cpumask[MAX_NODES]; |
| 255 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 256 | static CPUState *cur_cpu; |
| 257 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 258 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 259 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 260 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 261 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 262 | #define MAX_ICOUNT_SHIFT 10 |
| 263 | /* Compensate for varying guest execution speed. */ |
| 264 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 265 | static QEMUTimer *icount_rt_timer; |
| 266 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 267 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 268 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 269 | uint8_t qemu_uuid[16]; |
| 270 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 271 | static QEMUBootSetHandler *boot_set_handler; |
| 272 | static void *boot_set_opaque; |
| 273 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 274 | static int default_serial = 1; |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 275 | static int default_parallel = 1; |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 276 | static int default_virtcon = 1; |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 277 | static int default_monitor = 1; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 278 | static int default_vga = 1; |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 279 | static int default_floppy = 1; |
| 280 | static int default_cdrom = 1; |
| 281 | static int default_sdcard = 1; |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 282 | |
| 283 | static struct { |
| 284 | const char *driver; |
| 285 | int *flag; |
| 286 | } default_list[] = { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 287 | { .driver = "isa-serial", .flag = &default_serial }, |
| 288 | { .driver = "isa-parallel", .flag = &default_parallel }, |
Gerd Hoffmann | d8bcbab | 2009-12-16 14:25:40 +0100 | [diff] [blame] | 289 | { .driver = "isa-fdc", .flag = &default_floppy }, |
| 290 | { .driver = "ide-drive", .flag = &default_cdrom }, |
Amit Shah | 392ecf5 | 2010-01-21 16:19:23 +0530 | [diff] [blame] | 291 | { .driver = "virtio-serial-pci", .flag = &default_virtcon }, |
| 292 | { .driver = "virtio-serial-s390", .flag = &default_virtcon }, |
| 293 | { .driver = "virtio-serial", .flag = &default_virtcon }, |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 294 | { .driver = "VGA", .flag = &default_vga }, |
Gerd Hoffmann | 69fd02e | 2009-12-16 13:35:19 +0100 | [diff] [blame] | 295 | { .driver = "cirrus-vga", .flag = &default_vga }, |
| 296 | { .driver = "vmware-svga", .flag = &default_vga }, |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | static int default_driver_check(QemuOpts *opts, void *opaque) |
| 300 | { |
| 301 | const char *driver = qemu_opt_get(opts, "driver"); |
| 302 | int i; |
| 303 | |
| 304 | if (!driver) |
| 305 | return 0; |
| 306 | for (i = 0; i < ARRAY_SIZE(default_list); i++) { |
| 307 | if (strcmp(default_list[i].driver, driver) != 0) |
| 308 | continue; |
| 309 | *(default_list[i].flag) = 0; |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 314 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 315 | /* x86 ISA bus support */ |
| 316 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 317 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 318 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 319 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 320 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 321 | void hw_error(const char *fmt, ...) |
| 322 | { |
| 323 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 324 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 325 | |
| 326 | va_start(ap, fmt); |
| 327 | fprintf(stderr, "qemu: hardware error: "); |
| 328 | vfprintf(stderr, fmt, ap); |
| 329 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 330 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 331 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 332 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 333 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 334 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 335 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 336 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 337 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 338 | va_end(ap); |
| 339 | abort(); |
| 340 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 341 | |
| 342 | static void set_proc_name(const char *s) |
| 343 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 344 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 345 | char name[16]; |
| 346 | if (!s) |
| 347 | return; |
| 348 | name[sizeof(name) - 1] = 0; |
| 349 | strncpy(name, s, sizeof(name)); |
| 350 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 351 | This simple way is enough for `top'. */ |
| 352 | prctl(PR_SET_NAME, name); |
| 353 | #endif |
| 354 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 355 | |
| 356 | /***************/ |
| 357 | /* ballooning */ |
| 358 | |
| 359 | static QEMUBalloonEvent *qemu_balloon_event; |
| 360 | void *qemu_balloon_event_opaque; |
| 361 | |
| 362 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 363 | { |
| 364 | qemu_balloon_event = func; |
| 365 | qemu_balloon_event_opaque = opaque; |
| 366 | } |
| 367 | |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 368 | int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 369 | { |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 370 | if (qemu_balloon_event) { |
| 371 | qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque); |
| 372 | return 1; |
| 373 | } else { |
| 374 | return 0; |
| 375 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 378 | int qemu_balloon_status(MonitorCompletion cb, void *opaque) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 379 | { |
Adam Litke | 625a5be | 2010-01-26 14:17:35 -0600 | [diff] [blame] | 380 | if (qemu_balloon_event) { |
| 381 | qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque); |
| 382 | return 1; |
| 383 | } else { |
| 384 | return 0; |
| 385 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 386 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 387 | |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 388 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 389 | /***********************************************************/ |
Paolo Bonzini | 8f0056b | 2010-01-13 14:05:34 +0100 | [diff] [blame] | 390 | /* real time host monotonic timer */ |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 391 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 392 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 393 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 394 | { |
| 395 | union { |
| 396 | uint64_t ll; |
| 397 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 398 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 399 | uint32_t high, low; |
| 400 | #else |
| 401 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 402 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 403 | } l; |
| 404 | } u, res; |
| 405 | uint64_t rl, rh; |
| 406 | |
| 407 | u.ll = a; |
| 408 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 409 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 410 | rh += (rl >> 32); |
| 411 | res.l.high = rh / c; |
| 412 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 413 | return res.ll; |
| 414 | } |
| 415 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 416 | static int64_t get_clock_realtime(void) |
| 417 | { |
| 418 | struct timeval tv; |
| 419 | |
| 420 | gettimeofday(&tv, NULL); |
| 421 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 422 | } |
| 423 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 424 | #ifdef WIN32 |
| 425 | |
| 426 | static int64_t clock_freq; |
| 427 | |
| 428 | static void init_get_clock(void) |
| 429 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 430 | LARGE_INTEGER freq; |
| 431 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 432 | ret = QueryPerformanceFrequency(&freq); |
| 433 | if (ret == 0) { |
| 434 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 435 | exit(1); |
| 436 | } |
| 437 | clock_freq = freq.QuadPart; |
| 438 | } |
| 439 | |
| 440 | static int64_t get_clock(void) |
| 441 | { |
| 442 | LARGE_INTEGER ti; |
| 443 | QueryPerformanceCounter(&ti); |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 444 | return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | #else |
| 448 | |
| 449 | static int use_rt_clock; |
| 450 | |
| 451 | static void init_get_clock(void) |
| 452 | { |
| 453 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 454 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 455 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 456 | { |
| 457 | struct timespec ts; |
| 458 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 459 | use_rt_clock = 1; |
| 460 | } |
| 461 | } |
| 462 | #endif |
| 463 | } |
| 464 | |
| 465 | static int64_t get_clock(void) |
| 466 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 467 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 468 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 469 | if (use_rt_clock) { |
| 470 | struct timespec ts; |
| 471 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 472 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 473 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 474 | #endif |
| 475 | { |
| 476 | /* XXX: using gettimeofday leads to problems if the date |
| 477 | changes, so it should be avoided. */ |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 478 | return get_clock_realtime(); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 481 | #endif |
| 482 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 483 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 484 | static int64_t cpu_get_icount(void) |
| 485 | { |
| 486 | int64_t icount; |
| 487 | CPUState *env = cpu_single_env;; |
| 488 | icount = qemu_icount; |
| 489 | if (env) { |
| 490 | if (!can_do_io(env)) |
| 491 | fprintf(stderr, "Bad clock read\n"); |
| 492 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 493 | } |
| 494 | return qemu_icount_bias + (icount << icount_time_shift); |
| 495 | } |
| 496 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 497 | /***********************************************************/ |
| 498 | /* guest cycle counter */ |
| 499 | |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 500 | typedef struct TimersState { |
| 501 | int64_t cpu_ticks_prev; |
| 502 | int64_t cpu_ticks_offset; |
| 503 | int64_t cpu_clock_offset; |
| 504 | int32_t cpu_ticks_enabled; |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 505 | int64_t dummy; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 506 | } TimersState; |
| 507 | |
| 508 | TimersState timers_state; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 509 | |
| 510 | /* return the host CPU cycle counter and handle stop/restart */ |
| 511 | int64_t cpu_get_ticks(void) |
| 512 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 513 | if (use_icount) { |
| 514 | return cpu_get_icount(); |
| 515 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 516 | if (!timers_state.cpu_ticks_enabled) { |
| 517 | return timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 518 | } else { |
| 519 | int64_t ticks; |
| 520 | ticks = cpu_get_real_ticks(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 521 | if (timers_state.cpu_ticks_prev > ticks) { |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 522 | /* Note: non increasing ticks may happen if the host uses |
| 523 | software suspend */ |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 524 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 525 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 526 | timers_state.cpu_ticks_prev = ticks; |
| 527 | return ticks + timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 532 | static int64_t cpu_get_clock(void) |
| 533 | { |
| 534 | int64_t ti; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 535 | if (!timers_state.cpu_ticks_enabled) { |
| 536 | return timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 537 | } else { |
| 538 | ti = get_clock(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 539 | return ti + timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | /* enable cpu_get_ticks() */ |
| 544 | void cpu_enable_ticks(void) |
| 545 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 546 | if (!timers_state.cpu_ticks_enabled) { |
| 547 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 548 | timers_state.cpu_clock_offset -= get_clock(); |
| 549 | timers_state.cpu_ticks_enabled = 1; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 554 | cpu_get_ticks() after that. */ |
| 555 | void cpu_disable_ticks(void) |
| 556 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 557 | if (timers_state.cpu_ticks_enabled) { |
| 558 | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
| 559 | timers_state.cpu_clock_offset = cpu_get_clock(); |
| 560 | timers_state.cpu_ticks_enabled = 0; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | /***********************************************************/ |
| 565 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 566 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 567 | #define QEMU_CLOCK_REALTIME 0 |
| 568 | #define QEMU_CLOCK_VIRTUAL 1 |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 569 | #define QEMU_CLOCK_HOST 2 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 570 | |
| 571 | struct QEMUClock { |
| 572 | int type; |
| 573 | /* XXX: add frequency */ |
| 574 | }; |
| 575 | |
| 576 | struct QEMUTimer { |
| 577 | QEMUClock *clock; |
| 578 | int64_t expire_time; |
| 579 | QEMUTimerCB *cb; |
| 580 | void *opaque; |
| 581 | struct QEMUTimer *next; |
| 582 | }; |
| 583 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 584 | struct qemu_alarm_timer { |
| 585 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 586 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 587 | |
| 588 | int (*start)(struct qemu_alarm_timer *t); |
| 589 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 590 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 591 | void *priv; |
| 592 | }; |
| 593 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 594 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 595 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 596 | |
| 597 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 598 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 599 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 603 | { |
| 604 | if (!alarm_has_dynticks(t)) |
| 605 | return; |
| 606 | |
| 607 | t->rearm(t); |
| 608 | } |
| 609 | |
| 610 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 611 | #define MIN_TIMER_REARM_US 250 |
| 612 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 613 | static struct qemu_alarm_timer *alarm_timer; |
| 614 | |
| 615 | #ifdef _WIN32 |
| 616 | |
| 617 | struct qemu_alarm_win32 { |
| 618 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 619 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 620 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 621 | |
| 622 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 623 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 624 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 625 | |
| 626 | #else |
| 627 | |
| 628 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 629 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 630 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 631 | #ifdef __linux__ |
| 632 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 633 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 634 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 635 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 636 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 637 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 638 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 639 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 640 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 641 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 642 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 643 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 644 | |
| 645 | #endif /* _WIN32 */ |
| 646 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 647 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 648 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 649 | When the guest is idle real and virtual time will be aligned in |
| 650 | the IO wait loop. */ |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 651 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 652 | |
| 653 | static void icount_adjust(void) |
| 654 | { |
| 655 | int64_t cur_time; |
| 656 | int64_t cur_icount; |
| 657 | int64_t delta; |
| 658 | static int64_t last_delta; |
| 659 | /* If the VM is not running, then do nothing. */ |
| 660 | if (!vm_running) |
| 661 | return; |
| 662 | |
| 663 | cur_time = cpu_get_clock(); |
| 664 | cur_icount = qemu_get_clock(vm_clock); |
| 665 | delta = cur_icount - cur_time; |
| 666 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 667 | if (delta > 0 |
| 668 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 669 | && icount_time_shift > 0) { |
| 670 | /* The guest is getting too far ahead. Slow time down. */ |
| 671 | icount_time_shift--; |
| 672 | } |
| 673 | if (delta < 0 |
| 674 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 675 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 676 | /* The guest is getting too far behind. Speed time up. */ |
| 677 | icount_time_shift++; |
| 678 | } |
| 679 | last_delta = delta; |
| 680 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 681 | } |
| 682 | |
| 683 | static void icount_adjust_rt(void * opaque) |
| 684 | { |
| 685 | qemu_mod_timer(icount_rt_timer, |
| 686 | qemu_get_clock(rt_clock) + 1000); |
| 687 | icount_adjust(); |
| 688 | } |
| 689 | |
| 690 | static void icount_adjust_vm(void * opaque) |
| 691 | { |
| 692 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 693 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 694 | icount_adjust(); |
| 695 | } |
| 696 | |
| 697 | static void init_icount_adjust(void) |
| 698 | { |
| 699 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 700 | The realtime trigger catches emulated time passing too slowly, |
| 701 | the virtual time trigger catches emulated time passing too fast. |
| 702 | Realtime triggers occur even when idle, so use them less frequently |
| 703 | than VM triggers. */ |
| 704 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 705 | qemu_mod_timer(icount_rt_timer, |
| 706 | qemu_get_clock(rt_clock) + 1000); |
| 707 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 708 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 709 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 710 | } |
| 711 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 712 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 713 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 714 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 715 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 716 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 717 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 718 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 719 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 720 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 721 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 722 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 723 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 724 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 725 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 726 | {"win32", 0, win32_start_timer, |
| 727 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 728 | #endif |
| 729 | {NULL, } |
| 730 | }; |
| 731 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 732 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 733 | { |
| 734 | int i; |
| 735 | |
| 736 | printf("Available alarm timers, in order of precedence:\n"); |
| 737 | for (i = 0; alarm_timers[i].name; i++) |
| 738 | printf("%s\n", alarm_timers[i].name); |
| 739 | } |
| 740 | |
| 741 | static void configure_alarms(char const *opt) |
| 742 | { |
| 743 | int i; |
| 744 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 745 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 746 | char *arg; |
| 747 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 748 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 749 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 750 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 751 | show_available_alarms(); |
| 752 | exit(0); |
| 753 | } |
| 754 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 755 | arg = qemu_strdup(opt); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 756 | |
| 757 | /* Reorder the array */ |
| 758 | name = strtok(arg, ","); |
| 759 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 760 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 761 | if (!strcmp(alarm_timers[i].name, name)) |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | if (i == count) { |
| 766 | fprintf(stderr, "Unknown clock %s\n", name); |
| 767 | goto next; |
| 768 | } |
| 769 | |
| 770 | if (i < cur) |
| 771 | /* Ignore */ |
| 772 | goto next; |
| 773 | |
| 774 | /* Swap */ |
| 775 | tmp = alarm_timers[i]; |
| 776 | alarm_timers[i] = alarm_timers[cur]; |
| 777 | alarm_timers[cur] = tmp; |
| 778 | |
| 779 | cur++; |
| 780 | next: |
| 781 | name = strtok(NULL, ","); |
| 782 | } |
| 783 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 784 | qemu_free(arg); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 785 | |
| 786 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 787 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 788 | for (i = cur; i < count; i++) |
| 789 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 790 | } else { |
| 791 | show_available_alarms(); |
| 792 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 793 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 796 | #define QEMU_NUM_CLOCKS 3 |
| 797 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 798 | QEMUClock *rt_clock; |
| 799 | QEMUClock *vm_clock; |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 800 | QEMUClock *host_clock; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 801 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 802 | static QEMUTimer *active_timers[QEMU_NUM_CLOCKS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 803 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 804 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 805 | { |
| 806 | QEMUClock *clock; |
| 807 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 808 | clock->type = type; |
| 809 | return clock; |
| 810 | } |
| 811 | |
| 812 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 813 | { |
| 814 | QEMUTimer *ts; |
| 815 | |
| 816 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 817 | ts->clock = clock; |
| 818 | ts->cb = cb; |
| 819 | ts->opaque = opaque; |
| 820 | return ts; |
| 821 | } |
| 822 | |
| 823 | void qemu_free_timer(QEMUTimer *ts) |
| 824 | { |
| 825 | qemu_free(ts); |
| 826 | } |
| 827 | |
| 828 | /* stop a timer, but do not dealloc it */ |
| 829 | void qemu_del_timer(QEMUTimer *ts) |
| 830 | { |
| 831 | QEMUTimer **pt, *t; |
| 832 | |
| 833 | /* NOTE: this code must be signal safe because |
| 834 | qemu_timer_expired() can be called from a signal. */ |
| 835 | pt = &active_timers[ts->clock->type]; |
| 836 | for(;;) { |
| 837 | t = *pt; |
| 838 | if (!t) |
| 839 | break; |
| 840 | if (t == ts) { |
| 841 | *pt = t->next; |
| 842 | break; |
| 843 | } |
| 844 | pt = &t->next; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* modify the current timer so that it will be fired when current_time |
| 849 | >= expire_time. The corresponding callback will be called. */ |
| 850 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 851 | { |
| 852 | QEMUTimer **pt, *t; |
| 853 | |
| 854 | qemu_del_timer(ts); |
| 855 | |
| 856 | /* add the timer in the sorted list */ |
| 857 | /* NOTE: this code must be signal safe because |
| 858 | qemu_timer_expired() can be called from a signal. */ |
| 859 | pt = &active_timers[ts->clock->type]; |
| 860 | for(;;) { |
| 861 | t = *pt; |
| 862 | if (!t) |
| 863 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 864 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 865 | break; |
| 866 | pt = &t->next; |
| 867 | } |
| 868 | ts->expire_time = expire_time; |
| 869 | ts->next = *pt; |
| 870 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 871 | |
| 872 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 873 | if (pt == &active_timers[ts->clock->type]) { |
| 874 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 875 | qemu_rearm_alarm_timer(alarm_timer); |
| 876 | } |
| 877 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 878 | if (use_icount) |
| 879 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 880 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | int qemu_timer_pending(QEMUTimer *ts) |
| 884 | { |
| 885 | QEMUTimer *t; |
| 886 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 887 | if (t == ts) |
| 888 | return 1; |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 893 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 894 | { |
| 895 | if (!timer_head) |
| 896 | return 0; |
| 897 | return (timer_head->expire_time <= current_time); |
| 898 | } |
| 899 | |
| 900 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 901 | { |
| 902 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 903 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 904 | for(;;) { |
| 905 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 906 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 907 | break; |
| 908 | /* remove timer from the list before calling the callback */ |
| 909 | *ptimer_head = ts->next; |
| 910 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 911 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 912 | /* run the callback (the timer list can be modified) */ |
| 913 | ts->cb(ts->opaque); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | int64_t qemu_get_clock(QEMUClock *clock) |
| 918 | { |
| 919 | switch(clock->type) { |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 920 | case QEMU_CLOCK_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 921 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 922 | default: |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 923 | case QEMU_CLOCK_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 924 | if (use_icount) { |
| 925 | return cpu_get_icount(); |
| 926 | } else { |
| 927 | return cpu_get_clock(); |
| 928 | } |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 929 | case QEMU_CLOCK_HOST: |
| 930 | return get_clock_realtime(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 934 | static void init_clocks(void) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 935 | { |
| 936 | init_get_clock(); |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 937 | rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
| 938 | vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 939 | host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 940 | |
| 941 | rtc_clock = host_clock; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 942 | } |
| 943 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 944 | /* save a timer */ |
| 945 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 946 | { |
| 947 | uint64_t expire_time; |
| 948 | |
| 949 | if (qemu_timer_pending(ts)) { |
| 950 | expire_time = ts->expire_time; |
| 951 | } else { |
| 952 | expire_time = -1; |
| 953 | } |
| 954 | qemu_put_be64(f, expire_time); |
| 955 | } |
| 956 | |
| 957 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 958 | { |
| 959 | uint64_t expire_time; |
| 960 | |
| 961 | expire_time = qemu_get_be64(f); |
| 962 | if (expire_time != -1) { |
| 963 | qemu_mod_timer(ts, expire_time); |
| 964 | } else { |
| 965 | qemu_del_timer(ts); |
| 966 | } |
| 967 | } |
| 968 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 969 | static const VMStateDescription vmstate_timers = { |
| 970 | .name = "timer", |
| 971 | .version_id = 2, |
| 972 | .minimum_version_id = 1, |
| 973 | .minimum_version_id_old = 1, |
| 974 | .fields = (VMStateField []) { |
| 975 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 976 | VMSTATE_INT64(dummy, TimersState), |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 977 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 978 | VMSTATE_END_OF_LIST() |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 979 | } |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 980 | }; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 981 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 982 | static void qemu_event_increment(void); |
| 983 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 984 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 985 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 986 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 987 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 988 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 989 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 990 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 991 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 992 | #if 0 |
| 993 | #define DISP_FREQ 1000 |
| 994 | { |
| 995 | static int64_t delta_min = INT64_MAX; |
| 996 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 997 | static int count; |
| 998 | ti = qemu_get_clock(vm_clock); |
| 999 | if (last_clock != 0) { |
| 1000 | delta = ti - last_clock; |
| 1001 | if (delta < delta_min) |
| 1002 | delta_min = delta; |
| 1003 | if (delta > delta_max) |
| 1004 | delta_max = delta; |
| 1005 | delta_cum += delta; |
| 1006 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1007 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 1008 | muldiv64(delta_min, 1000000, get_ticks_per_sec()), |
| 1009 | muldiv64(delta_max, 1000000, get_ticks_per_sec()), |
| 1010 | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()), |
| 1011 | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ)); |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1012 | count = 0; |
| 1013 | delta_min = INT64_MAX; |
| 1014 | delta_max = 0; |
| 1015 | delta_cum = 0; |
| 1016 | } |
| 1017 | } |
| 1018 | last_clock = ti; |
| 1019 | } |
| 1020 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1021 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1022 | (!use_icount && |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1023 | qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL], |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1024 | qemu_get_clock(vm_clock))) || |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1025 | qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME], |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1026 | qemu_get_clock(rt_clock)) || |
| 1027 | qemu_timer_expired(active_timers[QEMU_CLOCK_HOST], |
| 1028 | qemu_get_clock(host_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1029 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1030 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1031 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1032 | #ifndef CONFIG_IOTHREAD |
| 1033 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1034 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1035 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1036 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1037 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1038 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1039 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1043 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1044 | { |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1045 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1046 | int64_t delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1047 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1048 | if (active_timers[QEMU_CLOCK_VIRTUAL]) { |
| 1049 | delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1050 | qemu_get_clock(vm_clock); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1051 | } |
| 1052 | if (active_timers[QEMU_CLOCK_HOST]) { |
| 1053 | int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time - |
| 1054 | qemu_get_clock(host_clock); |
| 1055 | if (hdelta < delta) |
| 1056 | delta = hdelta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1059 | if (delta < 0) |
| 1060 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1061 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1062 | return delta; |
| 1063 | } |
| 1064 | |
Jan Kiszka | f64382b | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1065 | #if defined(__linux__) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1066 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1067 | { |
| 1068 | int64_t delta; |
| 1069 | int64_t rtdelta; |
| 1070 | |
| 1071 | if (use_icount) |
| 1072 | delta = INT32_MAX; |
| 1073 | else |
| 1074 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1075 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1076 | if (active_timers[QEMU_CLOCK_REALTIME]) { |
| 1077 | rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1078 | qemu_get_clock(rt_clock))*1000; |
| 1079 | if (rtdelta < delta) |
| 1080 | delta = rtdelta; |
| 1081 | } |
| 1082 | |
| 1083 | if (delta < MIN_TIMER_REARM_US) |
| 1084 | delta = MIN_TIMER_REARM_US; |
| 1085 | |
| 1086 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1087 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1088 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1089 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1090 | #ifndef _WIN32 |
| 1091 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1092 | /* Sets a specific flag */ |
| 1093 | static int fcntl_setfl(int fd, int flag) |
| 1094 | { |
| 1095 | int flags; |
| 1096 | |
| 1097 | flags = fcntl(fd, F_GETFL); |
| 1098 | if (flags == -1) |
| 1099 | return -errno; |
| 1100 | |
| 1101 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1102 | return -errno; |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1107 | #if defined(__linux__) |
| 1108 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1109 | #define RTC_FREQ 1024 |
| 1110 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1111 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1112 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1113 | struct sigaction act; |
| 1114 | |
| 1115 | /* timer signal */ |
| 1116 | sigfillset(&act.sa_mask); |
| 1117 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1118 | act.sa_handler = host_alarm_handler; |
| 1119 | |
| 1120 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1121 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1122 | fcntl(fd, F_SETOWN, getpid()); |
| 1123 | } |
| 1124 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1125 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1126 | { |
| 1127 | struct hpet_info info; |
| 1128 | int r, fd; |
| 1129 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1130 | fd = qemu_open("/dev/hpet", O_RDONLY); |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1131 | if (fd < 0) |
| 1132 | return -1; |
| 1133 | |
| 1134 | /* Set frequency */ |
| 1135 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1136 | if (r < 0) { |
| 1137 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1138 | "error, but for better emulation accuracy type:\n" |
| 1139 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1140 | goto fail; |
| 1141 | } |
| 1142 | |
| 1143 | /* Check capabilities */ |
| 1144 | r = ioctl(fd, HPET_INFO, &info); |
| 1145 | if (r < 0) |
| 1146 | goto fail; |
| 1147 | |
| 1148 | /* Enable periodic mode */ |
| 1149 | r = ioctl(fd, HPET_EPI, 0); |
| 1150 | if (info.hi_flags && (r < 0)) |
| 1151 | goto fail; |
| 1152 | |
| 1153 | /* Enable interrupt */ |
| 1154 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1155 | if (r < 0) |
| 1156 | goto fail; |
| 1157 | |
| 1158 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1159 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1160 | |
| 1161 | return 0; |
| 1162 | fail: |
| 1163 | close(fd); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | |
| 1167 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1168 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1169 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1170 | |
| 1171 | close(fd); |
| 1172 | } |
| 1173 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1174 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1175 | { |
| 1176 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1177 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1178 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1179 | TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1180 | if (rtc_fd < 0) |
| 1181 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1182 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1183 | if (current_rtc_freq != RTC_FREQ && |
| 1184 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1185 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1186 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1187 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1188 | goto fail; |
| 1189 | } |
| 1190 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1191 | fail: |
| 1192 | close(rtc_fd); |
| 1193 | return -1; |
| 1194 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1195 | |
| 1196 | enable_sigio_timer(rtc_fd); |
| 1197 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1198 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1199 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1200 | return 0; |
| 1201 | } |
| 1202 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1203 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1204 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1205 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1206 | |
| 1207 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1210 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1211 | { |
| 1212 | struct sigevent ev; |
| 1213 | timer_t host_timer; |
| 1214 | struct sigaction act; |
| 1215 | |
| 1216 | sigfillset(&act.sa_mask); |
| 1217 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1218 | act.sa_handler = host_alarm_handler; |
| 1219 | |
| 1220 | sigaction(SIGALRM, &act, NULL); |
| 1221 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1222 | /* |
| 1223 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1224 | * about uninitialized data in timer_create call |
| 1225 | */ |
| 1226 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1227 | ev.sigev_value.sival_int = 0; |
| 1228 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1229 | ev.sigev_signo = SIGALRM; |
| 1230 | |
| 1231 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1232 | perror("timer_create"); |
| 1233 | |
| 1234 | /* disable dynticks */ |
| 1235 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1236 | |
| 1237 | return -1; |
| 1238 | } |
| 1239 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1240 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1246 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1247 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1248 | |
| 1249 | timer_delete(host_timer); |
| 1250 | } |
| 1251 | |
| 1252 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1253 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1254 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1255 | struct itimerspec timeout; |
| 1256 | int64_t nearest_delta_us = INT64_MAX; |
| 1257 | int64_t current_us; |
| 1258 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1259 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1260 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1261 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1262 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1263 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1264 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1265 | |
| 1266 | /* check whether a timer is already running */ |
| 1267 | if (timer_gettime(host_timer, &timeout)) { |
| 1268 | perror("gettime"); |
| 1269 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1270 | exit(1); |
| 1271 | } |
| 1272 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1273 | if (current_us && current_us <= nearest_delta_us) |
| 1274 | return; |
| 1275 | |
| 1276 | timeout.it_interval.tv_sec = 0; |
| 1277 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1278 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1279 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1280 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1281 | perror("settime"); |
| 1282 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1283 | exit(1); |
| 1284 | } |
| 1285 | } |
| 1286 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1287 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1288 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1289 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1290 | { |
| 1291 | struct sigaction act; |
| 1292 | struct itimerval itv; |
| 1293 | int err; |
| 1294 | |
| 1295 | /* timer signal */ |
| 1296 | sigfillset(&act.sa_mask); |
| 1297 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1298 | act.sa_handler = host_alarm_handler; |
| 1299 | |
| 1300 | sigaction(SIGALRM, &act, NULL); |
| 1301 | |
| 1302 | itv.it_interval.tv_sec = 0; |
| 1303 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1304 | itv.it_interval.tv_usec = 999; |
| 1305 | itv.it_value.tv_sec = 0; |
| 1306 | itv.it_value.tv_usec = 10 * 1000; |
| 1307 | |
| 1308 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1309 | if (err) |
| 1310 | return -1; |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1316 | { |
| 1317 | struct itimerval itv; |
| 1318 | |
| 1319 | memset(&itv, 0, sizeof(itv)); |
| 1320 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1321 | } |
| 1322 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1323 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1324 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1325 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1326 | #ifdef _WIN32 |
| 1327 | |
| 1328 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1329 | { |
| 1330 | TIMECAPS tc; |
| 1331 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1332 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1333 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1334 | memset(&tc, 0, sizeof(tc)); |
| 1335 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1336 | |
| 1337 | if (data->period < tc.wPeriodMin) |
| 1338 | data->period = tc.wPeriodMin; |
| 1339 | |
| 1340 | timeBeginPeriod(data->period); |
| 1341 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1342 | flags = TIME_CALLBACK_FUNCTION; |
| 1343 | if (alarm_has_dynticks(t)) |
| 1344 | flags |= TIME_ONESHOT; |
| 1345 | else |
| 1346 | flags |= TIME_PERIODIC; |
| 1347 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1348 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1349 | data->period, // resolution |
| 1350 | host_alarm_handler, // function |
| 1351 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1352 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1355 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1356 | GetLastError()); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1357 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1358 | return -1; |
| 1359 | } |
| 1360 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1365 | { |
| 1366 | struct qemu_alarm_win32 *data = t->priv; |
| 1367 | |
| 1368 | timeKillEvent(data->timerId); |
| 1369 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1372 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1373 | { |
| 1374 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1375 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1376 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1377 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1378 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1379 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1380 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1381 | timeKillEvent(data->timerId); |
| 1382 | |
| 1383 | data->timerId = timeSetEvent(1, |
| 1384 | data->period, |
| 1385 | host_alarm_handler, |
| 1386 | (DWORD)t, |
| 1387 | TIME_ONESHOT | TIME_PERIODIC); |
| 1388 | |
| 1389 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1390 | fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1391 | GetLastError()); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1392 | |
| 1393 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1394 | exit(1); |
| 1395 | } |
| 1396 | } |
| 1397 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1398 | #endif /* _WIN32 */ |
| 1399 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1400 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1401 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1402 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1403 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1404 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1405 | for (i = 0; alarm_timers[i].name; i++) { |
| 1406 | t = &alarm_timers[i]; |
| 1407 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1408 | err = t->start(t); |
| 1409 | if (!err) |
| 1410 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1411 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1412 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1413 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1414 | err = -ENOENT; |
| 1415 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1416 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1417 | |
| 1418 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1419 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1420 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1421 | |
| 1422 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1423 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1426 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1427 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1428 | alarm_timer->stop(alarm_timer); |
| 1429 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1432 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1433 | /* host time/date access */ |
| 1434 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1435 | { |
| 1436 | time_t ti; |
| 1437 | struct tm *ret; |
| 1438 | |
| 1439 | time(&ti); |
| 1440 | ti += offset; |
| 1441 | if (rtc_date_offset == -1) { |
| 1442 | if (rtc_utc) |
| 1443 | ret = gmtime(&ti); |
| 1444 | else |
| 1445 | ret = localtime(&ti); |
| 1446 | } else { |
| 1447 | ti -= rtc_date_offset; |
| 1448 | ret = gmtime(&ti); |
| 1449 | } |
| 1450 | |
| 1451 | memcpy(tm, ret, sizeof(struct tm)); |
| 1452 | } |
| 1453 | |
| 1454 | int qemu_timedate_diff(struct tm *tm) |
| 1455 | { |
| 1456 | time_t seconds; |
| 1457 | |
| 1458 | if (rtc_date_offset == -1) |
| 1459 | if (rtc_utc) |
| 1460 | seconds = mktimegm(tm); |
| 1461 | else |
| 1462 | seconds = mktime(tm); |
| 1463 | else |
| 1464 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1465 | |
| 1466 | return seconds - time(NULL); |
| 1467 | } |
| 1468 | |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1469 | static void configure_rtc_date_offset(const char *startdate, int legacy) |
| 1470 | { |
| 1471 | time_t rtc_start_date; |
| 1472 | struct tm tm; |
| 1473 | |
| 1474 | if (!strcmp(startdate, "now") && legacy) { |
| 1475 | rtc_date_offset = -1; |
| 1476 | } else { |
| 1477 | if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", |
| 1478 | &tm.tm_year, |
| 1479 | &tm.tm_mon, |
| 1480 | &tm.tm_mday, |
| 1481 | &tm.tm_hour, |
| 1482 | &tm.tm_min, |
| 1483 | &tm.tm_sec) == 6) { |
| 1484 | /* OK */ |
| 1485 | } else if (sscanf(startdate, "%d-%d-%d", |
| 1486 | &tm.tm_year, |
| 1487 | &tm.tm_mon, |
| 1488 | &tm.tm_mday) == 3) { |
| 1489 | tm.tm_hour = 0; |
| 1490 | tm.tm_min = 0; |
| 1491 | tm.tm_sec = 0; |
| 1492 | } else { |
| 1493 | goto date_fail; |
| 1494 | } |
| 1495 | tm.tm_year -= 1900; |
| 1496 | tm.tm_mon--; |
| 1497 | rtc_start_date = mktimegm(&tm); |
| 1498 | if (rtc_start_date == -1) { |
| 1499 | date_fail: |
| 1500 | fprintf(stderr, "Invalid date format. Valid formats are:\n" |
| 1501 | "'2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 1502 | exit(1); |
| 1503 | } |
| 1504 | rtc_date_offset = time(NULL) - rtc_start_date; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | static void configure_rtc(QemuOpts *opts) |
| 1509 | { |
| 1510 | const char *value; |
| 1511 | |
| 1512 | value = qemu_opt_get(opts, "base"); |
| 1513 | if (value) { |
| 1514 | if (!strcmp(value, "utc")) { |
| 1515 | rtc_utc = 1; |
| 1516 | } else if (!strcmp(value, "localtime")) { |
| 1517 | rtc_utc = 0; |
| 1518 | } else { |
| 1519 | configure_rtc_date_offset(value, 0); |
| 1520 | } |
| 1521 | } |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1522 | value = qemu_opt_get(opts, "clock"); |
| 1523 | if (value) { |
| 1524 | if (!strcmp(value, "host")) { |
| 1525 | rtc_clock = host_clock; |
| 1526 | } else if (!strcmp(value, "vm")) { |
| 1527 | rtc_clock = vm_clock; |
| 1528 | } else { |
| 1529 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1530 | exit(1); |
| 1531 | } |
| 1532 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1533 | #ifdef CONFIG_TARGET_I386 |
| 1534 | value = qemu_opt_get(opts, "driftfix"); |
| 1535 | if (value) { |
| 1536 | if (!strcmp(buf, "slew")) { |
| 1537 | rtc_td_hack = 1; |
| 1538 | } else if (!strcmp(buf, "none")) { |
| 1539 | rtc_td_hack = 0; |
| 1540 | } else { |
| 1541 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1542 | exit(1); |
| 1543 | } |
| 1544 | } |
| 1545 | #endif |
| 1546 | } |
| 1547 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1548 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1549 | static void socket_cleanup(void) |
| 1550 | { |
| 1551 | WSACleanup(); |
| 1552 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1553 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1554 | static int socket_init(void) |
| 1555 | { |
| 1556 | WSADATA Data; |
| 1557 | int ret, err; |
| 1558 | |
| 1559 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1560 | if (ret != 0) { |
| 1561 | err = WSAGetLastError(); |
| 1562 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1563 | return -1; |
| 1564 | } |
| 1565 | atexit(socket_cleanup); |
| 1566 | return 0; |
| 1567 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1568 | #endif |
| 1569 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1570 | /***********************************************************/ |
| 1571 | /* Bluetooth support */ |
| 1572 | static int nb_hcis; |
| 1573 | static int cur_hci; |
| 1574 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1575 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1576 | static struct bt_vlan_s { |
| 1577 | struct bt_scatternet_s net; |
| 1578 | int id; |
| 1579 | struct bt_vlan_s *next; |
| 1580 | } *first_bt_vlan; |
| 1581 | |
| 1582 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1583 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1584 | { |
| 1585 | struct bt_vlan_s **pvlan, *vlan; |
| 1586 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1587 | if (vlan->id == id) |
| 1588 | return &vlan->net; |
| 1589 | } |
| 1590 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1591 | vlan->id = id; |
| 1592 | pvlan = &first_bt_vlan; |
| 1593 | while (*pvlan != NULL) |
| 1594 | pvlan = &(*pvlan)->next; |
| 1595 | *pvlan = vlan; |
| 1596 | return &vlan->net; |
| 1597 | } |
| 1598 | |
| 1599 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1600 | { |
| 1601 | } |
| 1602 | |
| 1603 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1604 | { |
| 1605 | return -ENOTSUP; |
| 1606 | } |
| 1607 | |
| 1608 | static struct HCIInfo null_hci = { |
| 1609 | .cmd_send = null_hci_send, |
| 1610 | .sco_send = null_hci_send, |
| 1611 | .acl_send = null_hci_send, |
| 1612 | .bdaddr_set = null_hci_addr_set, |
| 1613 | }; |
| 1614 | |
| 1615 | struct HCIInfo *qemu_next_hci(void) |
| 1616 | { |
| 1617 | if (cur_hci == nb_hcis) |
| 1618 | return &null_hci; |
| 1619 | |
| 1620 | return hci_table[cur_hci++]; |
| 1621 | } |
| 1622 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1623 | static struct HCIInfo *hci_init(const char *str) |
| 1624 | { |
| 1625 | char *endp; |
| 1626 | struct bt_scatternet_s *vlan = 0; |
| 1627 | |
| 1628 | if (!strcmp(str, "null")) |
| 1629 | /* null */ |
| 1630 | return &null_hci; |
| 1631 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1632 | /* host[:hciN] */ |
| 1633 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1634 | else if (!strncmp(str, "hci", 3)) { |
| 1635 | /* hci[,vlan=n] */ |
| 1636 | if (str[3]) { |
| 1637 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1638 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1639 | if (*endp) |
| 1640 | vlan = 0; |
| 1641 | } |
| 1642 | } else |
| 1643 | vlan = qemu_find_bt_vlan(0); |
| 1644 | if (vlan) |
| 1645 | return bt_new_hci(vlan); |
| 1646 | } |
| 1647 | |
| 1648 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1649 | |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | static int bt_hci_parse(const char *str) |
| 1654 | { |
| 1655 | struct HCIInfo *hci; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1656 | bdaddr_t bdaddr; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (nb_hcis >= MAX_NICS) { |
| 1659 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1660 | return -1; |
| 1661 | } |
| 1662 | |
| 1663 | hci = hci_init(str); |
| 1664 | if (!hci) |
| 1665 | return -1; |
| 1666 | |
| 1667 | bdaddr.b[0] = 0x52; |
| 1668 | bdaddr.b[1] = 0x54; |
| 1669 | bdaddr.b[2] = 0x00; |
| 1670 | bdaddr.b[3] = 0x12; |
| 1671 | bdaddr.b[4] = 0x34; |
| 1672 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1673 | hci->bdaddr_set(hci, bdaddr.b); |
| 1674 | |
| 1675 | hci_table[nb_hcis++] = hci; |
| 1676 | |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | static void bt_vhci_add(int vlan_id) |
| 1681 | { |
| 1682 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1683 | |
| 1684 | if (!vlan->slave) |
| 1685 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1686 | "an empty scatternet %i\n", vlan_id); |
| 1687 | |
| 1688 | bt_vhci_init(bt_new_hci(vlan)); |
| 1689 | } |
| 1690 | |
| 1691 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1692 | { |
| 1693 | struct bt_scatternet_s *vlan; |
| 1694 | int vlan_id = 0; |
| 1695 | char *endp = strstr(opt, ",vlan="); |
| 1696 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1697 | char devname[10]; |
| 1698 | |
| 1699 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1700 | |
| 1701 | if (endp) { |
| 1702 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1703 | if (*endp) { |
| 1704 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1705 | return 0; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1710 | |
| 1711 | if (!vlan->slave) |
| 1712 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1713 | "an empty scatternet %i\n", vlan_id); |
| 1714 | |
| 1715 | if (!strcmp(devname, "keyboard")) |
| 1716 | return bt_keyboard_init(vlan); |
| 1717 | |
| 1718 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | static int bt_parse(const char *opt) |
| 1723 | { |
| 1724 | const char *endp, *p; |
| 1725 | int vlan; |
| 1726 | |
| 1727 | if (strstart(opt, "hci", &endp)) { |
| 1728 | if (!*endp || *endp == ',') { |
| 1729 | if (*endp) |
| 1730 | if (!strstart(endp, ",vlan=", 0)) |
| 1731 | opt = endp + 1; |
| 1732 | |
| 1733 | return bt_hci_parse(opt); |
| 1734 | } |
| 1735 | } else if (strstart(opt, "vhci", &endp)) { |
| 1736 | if (!*endp || *endp == ',') { |
| 1737 | if (*endp) { |
| 1738 | if (strstart(endp, ",vlan=", &p)) { |
| 1739 | vlan = strtol(p, (char **) &endp, 0); |
| 1740 | if (*endp) { |
| 1741 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1742 | return 1; |
| 1743 | } |
| 1744 | } else { |
| 1745 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1746 | return 1; |
| 1747 | } |
| 1748 | } else |
| 1749 | vlan = 0; |
| 1750 | |
| 1751 | bt_vhci_add(vlan); |
| 1752 | return 0; |
| 1753 | } |
| 1754 | } else if (strstart(opt, "device:", &endp)) |
| 1755 | return !bt_device_add(endp); |
| 1756 | |
| 1757 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1761 | /***********************************************************/ |
| 1762 | /* QEMU Block devices */ |
| 1763 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1764 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1765 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1766 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1767 | #define PFLASH_ALIAS "if=pflash" |
| 1768 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1769 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1770 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1771 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1772 | { |
| 1773 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1774 | char optstr[1024]; |
| 1775 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1776 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1777 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1778 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1779 | va_end(ap); |
| 1780 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1781 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1782 | if (!opts) { |
| 1783 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1784 | __FUNCTION__, optstr); |
| 1785 | return NULL; |
| 1786 | } |
| 1787 | if (file) |
| 1788 | qemu_opt_set(opts, "file", file); |
| 1789 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1792 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1793 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1794 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* seek interface, bus and unit */ |
| 1797 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1798 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1799 | if (dinfo->type == type && |
| 1800 | dinfo->bus == bus && |
| 1801 | dinfo->unit == unit) |
| 1802 | return dinfo; |
| 1803 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1804 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1805 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 1808 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1809 | { |
| 1810 | DriveInfo *dinfo; |
| 1811 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1812 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1813 | if (strcmp(id, dinfo->id)) |
| 1814 | continue; |
| 1815 | return dinfo; |
| 1816 | } |
| 1817 | return NULL; |
| 1818 | } |
| 1819 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1820 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1821 | { |
| 1822 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1823 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1824 | |
| 1825 | max_bus = -1; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1826 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1827 | if(dinfo->type == type && |
| 1828 | dinfo->bus > max_bus) |
| 1829 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1830 | } |
| 1831 | return max_bus; |
| 1832 | } |
| 1833 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1834 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 1835 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1836 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1837 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1838 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1839 | if (dinfo->bdrv == bdrv) |
| 1840 | return dinfo->serial; |
| 1841 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1842 | |
| 1843 | return "\0"; |
| 1844 | } |
| 1845 | |
Kevin Wolf | f785009 | 2009-11-27 13:25:36 +0100 | [diff] [blame] | 1846 | BlockInterfaceErrorAction drive_get_on_error( |
| 1847 | BlockDriverState *bdrv, int is_read) |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1848 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1849 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1850 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1851 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1852 | if (dinfo->bdrv == bdrv) |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 1853 | return is_read ? dinfo->on_read_error : dinfo->on_write_error; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1854 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1855 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 1856 | return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 1859 | static void bdrv_format_print(void *opaque, const char *name) |
| 1860 | { |
| 1861 | fprintf(stderr, " %s", name); |
| 1862 | } |
| 1863 | |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 1864 | void drive_uninit(DriveInfo *dinfo) |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1865 | { |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 1866 | qemu_opts_del(dinfo->opts); |
| 1867 | bdrv_delete(dinfo->bdrv); |
| 1868 | QTAILQ_REMOVE(&drives, dinfo, next); |
| 1869 | qemu_free(dinfo); |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 1872 | static int parse_block_error_action(const char *buf, int is_read) |
| 1873 | { |
| 1874 | if (!strcmp(buf, "ignore")) { |
| 1875 | return BLOCK_ERR_IGNORE; |
| 1876 | } else if (!is_read && !strcmp(buf, "enospc")) { |
| 1877 | return BLOCK_ERR_STOP_ENOSPC; |
| 1878 | } else if (!strcmp(buf, "stop")) { |
| 1879 | return BLOCK_ERR_STOP_ANY; |
| 1880 | } else if (!strcmp(buf, "report")) { |
| 1881 | return BLOCK_ERR_REPORT; |
| 1882 | } else { |
| 1883 | fprintf(stderr, "qemu: '%s' invalid %s error action\n", |
| 1884 | buf, is_read ? "read" : "write"); |
| 1885 | return -1; |
| 1886 | } |
| 1887 | } |
| 1888 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1889 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1890 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1891 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1892 | const char *buf; |
| 1893 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1894 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1895 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1896 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1897 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1898 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 1899 | int bus_id, unit_id; |
| 1900 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 1901 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 1902 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1903 | int max_devs; |
| 1904 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 1905 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1906 | int aio = 0; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 1907 | int ro = 0; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 1908 | int bdrv_flags; |
| 1909 | int on_read_error, on_write_error; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 1910 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1911 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1912 | int snapshot = 0; |
| 1913 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1914 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1915 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1916 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 1917 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1918 | |
Gerd Hoffmann | 4d00781 | 2009-08-31 14:23:57 +0200 | [diff] [blame] | 1919 | if (machine && machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1920 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1921 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1922 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1923 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1924 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1925 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1926 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1927 | } |
| 1928 | media = MEDIA_DISK; |
| 1929 | |
| 1930 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1931 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 1932 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 1933 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1934 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1935 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 1936 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 1937 | secs = qemu_opt_get_number(opts, "secs", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1938 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1939 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 1940 | ro = qemu_opt_get_bool(opts, "readonly", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1941 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1942 | file = qemu_opt_get(opts, "file"); |
| 1943 | serial = qemu_opt_get(opts, "serial"); |
| 1944 | |
| 1945 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 1946 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1947 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1948 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1949 | max_devs = MAX_IDE_DEVS; |
| 1950 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1951 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1952 | max_devs = MAX_SCSI_DEVS; |
| 1953 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1954 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1955 | max_devs = 0; |
| 1956 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1957 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1958 | max_devs = 0; |
| 1959 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1960 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1961 | max_devs = 0; |
| 1962 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1963 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1964 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1965 | } else if (!strcmp(buf, "virtio")) { |
| 1966 | type = IF_VIRTIO; |
| 1967 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1968 | } else if (!strcmp(buf, "xen")) { |
| 1969 | type = IF_XEN; |
| 1970 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 1971 | } else if (!strcmp(buf, "none")) { |
| 1972 | type = IF_NONE; |
| 1973 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1974 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1975 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1976 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1980 | if (cyls || heads || secs) { |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 1981 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1982 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\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 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 1985 | if (heads < 1 || (type == IF_IDE && heads > 16)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1986 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1987 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1988 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 1989 | if (secs < 1 || (type == IF_IDE && secs > 63)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1990 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1991 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1995 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1996 | if (!cyls) { |
| 1997 | fprintf(stderr, |
| 1998 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1999 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2000 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2001 | } |
| 2002 | if (!strcmp(buf, "none")) |
| 2003 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2004 | else if (!strcmp(buf, "lba")) |
| 2005 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2006 | else if (!strcmp(buf, "auto")) |
| 2007 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2008 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2009 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2010 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2014 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2015 | if (!strcmp(buf, "disk")) { |
| 2016 | media = MEDIA_DISK; |
| 2017 | } else if (!strcmp(buf, "cdrom")) { |
| 2018 | if (cyls || secs || heads) { |
| 2019 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2020 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2021 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2022 | } |
| 2023 | media = MEDIA_CDROM; |
| 2024 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2025 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2026 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2027 | } |
| 2028 | } |
| 2029 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2030 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2031 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2032 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2033 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2034 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2035 | else if (!strcmp(buf, "writeback")) |
| 2036 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2037 | else { |
| 2038 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2039 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2043 | #ifdef CONFIG_LINUX_AIO |
| 2044 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2045 | if (!strcmp(buf, "threads")) |
| 2046 | aio = 0; |
| 2047 | else if (!strcmp(buf, "native")) |
| 2048 | aio = 1; |
| 2049 | else { |
| 2050 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2051 | return NULL; |
| 2052 | } |
| 2053 | } |
| 2054 | #endif |
| 2055 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2056 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2057 | if (strcmp(buf, "?") == 0) { |
| 2058 | fprintf(stderr, "qemu: Supported formats:"); |
| 2059 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2060 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2061 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2062 | } |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 2063 | drv = bdrv_find_whitelisted_format(buf); |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2064 | if (!drv) { |
| 2065 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2066 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
| 2069 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2070 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2071 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2072 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2073 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2074 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2075 | } |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2076 | |
| 2077 | on_write_error = parse_block_error_action(buf, 0); |
| 2078 | if (on_write_error < 0) { |
| 2079 | return NULL; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | on_read_error = BLOCK_ERR_REPORT; |
| 2084 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 2085 | if (type != IF_IDE && type != IF_VIRTIO) { |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2086 | fprintf(stderr, "rerror is no supported by this format\n"); |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | |
| 2090 | on_read_error = parse_block_error_action(buf, 1); |
| 2091 | if (on_read_error < 0) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2092 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2096 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2097 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2098 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2099 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2100 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2103 | /* compute bus and unit according index */ |
| 2104 | |
| 2105 | if (index != -1) { |
| 2106 | if (bus_id != 0 || unit_id != -1) { |
| 2107 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2108 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2109 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2110 | } |
| 2111 | if (max_devs == 0) |
| 2112 | { |
| 2113 | unit_id = index; |
| 2114 | bus_id = 0; |
| 2115 | } else { |
| 2116 | unit_id = index % max_devs; |
| 2117 | bus_id = index / max_devs; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | /* if user doesn't specify a unit_id, |
| 2122 | * try to find the first free |
| 2123 | */ |
| 2124 | |
| 2125 | if (unit_id == -1) { |
| 2126 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2127 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2128 | unit_id++; |
| 2129 | if (max_devs && unit_id >= max_devs) { |
| 2130 | unit_id -= max_devs; |
| 2131 | bus_id++; |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /* check unit id */ |
| 2137 | |
| 2138 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2139 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2140 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2141 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * ignore multiple definitions |
| 2146 | */ |
| 2147 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2148 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2149 | *fatal_error = 0; |
| 2150 | return NULL; |
| 2151 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2152 | |
| 2153 | /* init */ |
| 2154 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2155 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2156 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2157 | dinfo->id = qemu_strdup(buf); |
| 2158 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2159 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2160 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2161 | if (type == IF_IDE || type == IF_SCSI) |
| 2162 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2163 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2164 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2165 | devname, bus_id, mediastr, unit_id); |
| 2166 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2167 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2168 | devname, mediastr, unit_id); |
| 2169 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2170 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2171 | dinfo->devaddr = devaddr; |
| 2172 | dinfo->type = type; |
| 2173 | dinfo->bus = bus_id; |
| 2174 | dinfo->unit = unit_id; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2175 | dinfo->on_read_error = on_read_error; |
| 2176 | dinfo->on_write_error = on_write_error; |
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)); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2180 | QTAILQ_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: |
Gerd Hoffmann | c219331 | 2009-09-15 19:23:28 +0000 | [diff] [blame] | 2186 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2187 | switch(media) { |
| 2188 | case MEDIA_DISK: |
| 2189 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2190 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2191 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2192 | } |
| 2193 | break; |
| 2194 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2195 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2196 | break; |
| 2197 | } |
| 2198 | break; |
| 2199 | case IF_SD: |
| 2200 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2201 | approximation. */ |
| 2202 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2203 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2204 | break; |
| 2205 | case IF_PFLASH: |
| 2206 | case IF_MTD: |
| 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 | |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2239 | if (ro == 1) { |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2240 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) { |
| 2241 | fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n"); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2242 | return NULL; |
| 2243 | } |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2244 | } |
Naphtali Sprei | b196b15 | 2010-01-17 16:48:12 +0200 | [diff] [blame] | 2245 | /* |
| 2246 | * cdrom is read-only. Set it now, after above interface checking |
| 2247 | * since readonly attribute not explicitly required, so no error. |
| 2248 | */ |
| 2249 | if (media == MEDIA_CDROM) { |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2250 | ro = 1; |
Naphtali Sprei | b196b15 | 2010-01-17 16:48:12 +0200 | [diff] [blame] | 2251 | } |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2252 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2253 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2254 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 2255 | fprintf(stderr, "qemu: could not open disk image %s: %s\n", |
| 2256 | file, strerror(errno)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2257 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2258 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2259 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2260 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2261 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2262 | *fatal_error = 0; |
| 2263 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2266 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2267 | { |
| 2268 | QEMUMachine *machine = opaque; |
| 2269 | int fatal_error = 0; |
| 2270 | |
| 2271 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2272 | if (fatal_error) |
| 2273 | return 1; |
| 2274 | } |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
| 2278 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2279 | { |
| 2280 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2281 | qemu_opt_set(opts, "snapshot", "on"); |
| 2282 | } |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2286 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2287 | { |
| 2288 | boot_set_handler = func; |
| 2289 | boot_set_opaque = opaque; |
| 2290 | } |
| 2291 | |
| 2292 | int qemu_boot_set(const char *boot_devices) |
| 2293 | { |
| 2294 | if (!boot_set_handler) { |
| 2295 | return -EINVAL; |
| 2296 | } |
| 2297 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2298 | } |
| 2299 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2300 | static int parse_bootdevices(char *devices) |
| 2301 | { |
| 2302 | /* We just do some generic consistency checks */ |
| 2303 | const char *p; |
| 2304 | int bitmap = 0; |
| 2305 | |
| 2306 | for (p = devices; *p != '\0'; p++) { |
| 2307 | /* Allowed boot devices are: |
| 2308 | * a-b: floppy disk drives |
| 2309 | * c-f: IDE disk drives |
| 2310 | * g-m: machine implementation dependant drives |
| 2311 | * n-p: network devices |
| 2312 | * It's up to each machine implementation to check if the given boot |
| 2313 | * devices match the actual hardware implementation and firmware |
| 2314 | * features. |
| 2315 | */ |
| 2316 | if (*p < 'a' || *p > 'p') { |
| 2317 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2318 | exit(1); |
| 2319 | } |
| 2320 | if (bitmap & (1 << (*p - 'a'))) { |
| 2321 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2322 | exit(1); |
| 2323 | } |
| 2324 | bitmap |= 1 << (*p - 'a'); |
| 2325 | } |
| 2326 | return bitmap; |
| 2327 | } |
| 2328 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2329 | static void restore_boot_devices(void *opaque) |
| 2330 | { |
| 2331 | char *standard_boot_devices = opaque; |
| 2332 | |
| 2333 | qemu_boot_set(standard_boot_devices); |
| 2334 | |
| 2335 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2336 | qemu_free(standard_boot_devices); |
| 2337 | } |
| 2338 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2339 | static void numa_add(const char *optarg) |
| 2340 | { |
| 2341 | char option[128]; |
| 2342 | char *endptr; |
| 2343 | unsigned long long value, endvalue; |
| 2344 | int nodenr; |
| 2345 | |
| 2346 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2347 | if (!strcmp(option, "node")) { |
| 2348 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2349 | nodenr = nb_numa_nodes; |
| 2350 | } else { |
| 2351 | nodenr = strtoull(option, NULL, 10); |
| 2352 | } |
| 2353 | |
| 2354 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2355 | node_mem[nodenr] = 0; |
| 2356 | } else { |
| 2357 | value = strtoull(option, &endptr, 0); |
| 2358 | switch (*endptr) { |
| 2359 | case 0: case 'M': case 'm': |
| 2360 | value <<= 20; |
| 2361 | break; |
| 2362 | case 'G': case 'g': |
| 2363 | value <<= 30; |
| 2364 | break; |
| 2365 | } |
| 2366 | node_mem[nodenr] = value; |
| 2367 | } |
| 2368 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2369 | node_cpumask[nodenr] = 0; |
| 2370 | } else { |
| 2371 | value = strtoull(option, &endptr, 10); |
| 2372 | if (value >= 64) { |
| 2373 | value = 63; |
| 2374 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2375 | } else { |
| 2376 | if (*endptr == '-') { |
| 2377 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2378 | if (endvalue >= 63) { |
| 2379 | endvalue = 62; |
| 2380 | fprintf(stderr, |
| 2381 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2382 | } |
Paolo Bonzini | 0dfbd51 | 2010-02-04 14:31:50 +0100 | [diff] [blame^] | 2383 | value = (2ULL << endvalue) - (1ULL << value); |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2384 | } else { |
Paolo Bonzini | 0dfbd51 | 2010-02-04 14:31:50 +0100 | [diff] [blame^] | 2385 | value = 1ULL << value; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | node_cpumask[nodenr] = value; |
| 2389 | } |
| 2390 | nb_numa_nodes++; |
| 2391 | } |
| 2392 | return; |
| 2393 | } |
| 2394 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2395 | static void smp_parse(const char *optarg) |
| 2396 | { |
| 2397 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2398 | char *endptr; |
| 2399 | char option[128]; |
| 2400 | |
| 2401 | smp = strtoul(optarg, &endptr, 10); |
| 2402 | if (endptr != optarg) { |
| 2403 | if (*endptr == ',') { |
| 2404 | endptr++; |
| 2405 | } |
| 2406 | } |
| 2407 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2408 | sockets = strtoull(option, NULL, 10); |
| 2409 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2410 | cores = strtoull(option, NULL, 10); |
| 2411 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2412 | threads = strtoull(option, NULL, 10); |
| 2413 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2414 | max_cpus = strtoull(option, NULL, 10); |
| 2415 | |
| 2416 | /* compute missing values, prefer sockets over cores over threads */ |
| 2417 | if (smp == 0 || sockets == 0) { |
| 2418 | sockets = sockets > 0 ? sockets : 1; |
| 2419 | cores = cores > 0 ? cores : 1; |
| 2420 | threads = threads > 0 ? threads : 1; |
| 2421 | if (smp == 0) { |
| 2422 | smp = cores * threads * sockets; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2423 | } |
| 2424 | } else { |
| 2425 | if (cores == 0) { |
| 2426 | threads = threads > 0 ? threads : 1; |
| 2427 | cores = smp / (sockets * threads); |
| 2428 | } else { |
Amit Shah | 5cdc9b7 | 2010-01-13 16:24:42 +0530 | [diff] [blame] | 2429 | if (sockets) { |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2430 | threads = smp / (cores * sockets); |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | smp_cpus = smp; |
| 2435 | smp_cores = cores > 0 ? cores : 1; |
| 2436 | smp_threads = threads > 0 ? threads : 1; |
| 2437 | if (max_cpus == 0) |
| 2438 | max_cpus = smp_cpus; |
| 2439 | } |
| 2440 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2441 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2442 | /* USB devices */ |
| 2443 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2444 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2445 | { |
| 2446 | const char *p; |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2447 | USBDevice *dev = NULL; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2448 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2449 | if (!usb_enabled) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2450 | return -1; |
| 2451 | |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 2452 | /* drivers with .usbdevice_name entry in USBDeviceInfo */ |
| 2453 | dev = usbdevice_create(devname); |
| 2454 | if (dev) |
| 2455 | goto done; |
| 2456 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2457 | /* the other ones */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2458 | if (strstart(devname, "host:", &p)) { |
| 2459 | dev = usb_host_device_open(p); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2460 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2461 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2462 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2463 | } else { |
| 2464 | return -1; |
| 2465 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2466 | if (!dev) |
| 2467 | return -1; |
| 2468 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2469 | done: |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2470 | return 0; |
| 2471 | } |
| 2472 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2473 | static int usb_device_del(const char *devname) |
| 2474 | { |
| 2475 | int bus_num, addr; |
| 2476 | const char *p; |
| 2477 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2478 | if (strstart(devname, "host:", &p)) |
| 2479 | return usb_host_device_close(p); |
| 2480 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2481 | if (!usb_enabled) |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2482 | return -1; |
| 2483 | |
| 2484 | p = strchr(devname, '.'); |
| 2485 | if (!p) |
| 2486 | return -1; |
| 2487 | bus_num = strtoul(devname, NULL, 0); |
| 2488 | addr = strtoul(p + 1, NULL, 0); |
| 2489 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2490 | return usb_device_delete_addr(bus_num, addr); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2493 | static int usb_parse(const char *cmdline) |
| 2494 | { |
Scott Tsai | 59d1c1c | 2009-12-23 04:30:18 +0800 | [diff] [blame] | 2495 | int r; |
| 2496 | r = usb_device_add(cmdline, 0); |
| 2497 | if (r < 0) { |
| 2498 | fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline); |
| 2499 | } |
| 2500 | return r; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2501 | } |
| 2502 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2503 | void do_usb_add(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2504 | { |
Scott Tsai | 59d1c1c | 2009-12-23 04:30:18 +0800 | [diff] [blame] | 2505 | const char *devname = qdict_get_str(qdict, "devname"); |
| 2506 | if (usb_device_add(devname, 1) < 0) { |
| 2507 | qemu_error("could not add USB device '%s'\n", devname); |
| 2508 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2511 | void do_usb_del(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2512 | { |
Scott Tsai | 59d1c1c | 2009-12-23 04:30:18 +0800 | [diff] [blame] | 2513 | const char *devname = qdict_get_str(qdict, "devname"); |
| 2514 | if (usb_device_del(devname) < 0) { |
| 2515 | qemu_error("could not delete USB device '%s'\n", devname); |
| 2516 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2519 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2520 | /* PCMCIA/Cardbus */ |
| 2521 | |
| 2522 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2523 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2524 | struct pcmcia_socket_entry_s *next; |
| 2525 | } *pcmcia_sockets = 0; |
| 2526 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2527 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2528 | { |
| 2529 | struct pcmcia_socket_entry_s *entry; |
| 2530 | |
| 2531 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2532 | entry->socket = socket; |
| 2533 | entry->next = pcmcia_sockets; |
| 2534 | pcmcia_sockets = entry; |
| 2535 | } |
| 2536 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2537 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2538 | { |
| 2539 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2540 | |
| 2541 | ptr = &pcmcia_sockets; |
| 2542 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2543 | if (entry->socket == socket) { |
| 2544 | *ptr = entry->next; |
| 2545 | qemu_free(entry); |
| 2546 | } |
| 2547 | } |
| 2548 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2549 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2550 | { |
| 2551 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2552 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2553 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2554 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2555 | |
| 2556 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2557 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2558 | iter->socket->attached ? iter->socket->card_string : |
| 2559 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /***********************************************************/ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2563 | /* register display */ |
| 2564 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2565 | struct DisplayAllocator default_allocator = { |
| 2566 | defaultallocator_create_displaysurface, |
| 2567 | defaultallocator_resize_displaysurface, |
| 2568 | defaultallocator_free_displaysurface |
| 2569 | }; |
| 2570 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2571 | void register_displaystate(DisplayState *ds) |
| 2572 | { |
| 2573 | DisplayState **s; |
| 2574 | s = &display_state; |
| 2575 | while (*s != NULL) |
| 2576 | s = &(*s)->next; |
| 2577 | ds->next = NULL; |
| 2578 | *s = ds; |
| 2579 | } |
| 2580 | |
| 2581 | DisplayState *get_displaystate(void) |
| 2582 | { |
| 2583 | return display_state; |
| 2584 | } |
| 2585 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2586 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2587 | { |
| 2588 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2589 | return ds->allocator; |
| 2590 | } |
| 2591 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2592 | /* dumb display */ |
| 2593 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2594 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2595 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2596 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2597 | ds->allocator = &default_allocator; |
| 2598 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2599 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2603 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2604 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2605 | typedef struct IOHandlerRecord { |
| 2606 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2607 | IOCanRWHandler *fd_read_poll; |
| 2608 | IOHandler *fd_read; |
| 2609 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2610 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2611 | void *opaque; |
| 2612 | /* temporary data */ |
| 2613 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2614 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2615 | } IOHandlerRecord; |
| 2616 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2617 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2618 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2619 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2620 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2621 | int qemu_set_fd_handler2(int fd, |
| 2622 | IOCanRWHandler *fd_read_poll, |
| 2623 | IOHandler *fd_read, |
| 2624 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2625 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2626 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2627 | IOHandlerRecord **pioh, *ioh; |
| 2628 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2629 | if (!fd_read && !fd_write) { |
| 2630 | pioh = &first_io_handler; |
| 2631 | for(;;) { |
| 2632 | ioh = *pioh; |
| 2633 | if (ioh == NULL) |
| 2634 | break; |
| 2635 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2636 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | } |
| 2639 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2640 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2641 | } else { |
| 2642 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2643 | if (ioh->fd == fd) |
| 2644 | goto found; |
| 2645 | } |
| 2646 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2647 | ioh->next = first_io_handler; |
| 2648 | first_io_handler = ioh; |
| 2649 | found: |
| 2650 | ioh->fd = fd; |
| 2651 | ioh->fd_read_poll = fd_read_poll; |
| 2652 | ioh->fd_read = fd_read; |
| 2653 | ioh->fd_write = fd_write; |
| 2654 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2655 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2656 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2657 | return 0; |
| 2658 | } |
| 2659 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2660 | int qemu_set_fd_handler(int fd, |
| 2661 | IOHandler *fd_read, |
| 2662 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2663 | void *opaque) |
| 2664 | { |
| 2665 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2668 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2669 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2670 | /* Polling handling */ |
| 2671 | |
| 2672 | typedef struct PollingEntry { |
| 2673 | PollingFunc *func; |
| 2674 | void *opaque; |
| 2675 | struct PollingEntry *next; |
| 2676 | } PollingEntry; |
| 2677 | |
| 2678 | static PollingEntry *first_polling_entry; |
| 2679 | |
| 2680 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2681 | { |
| 2682 | PollingEntry **ppe, *pe; |
| 2683 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2684 | pe->func = func; |
| 2685 | pe->opaque = opaque; |
| 2686 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2687 | *ppe = pe; |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2692 | { |
| 2693 | PollingEntry **ppe, *pe; |
| 2694 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2695 | pe = *ppe; |
| 2696 | if (pe->func == func && pe->opaque == opaque) { |
| 2697 | *ppe = pe->next; |
| 2698 | qemu_free(pe); |
| 2699 | break; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2704 | /***********************************************************/ |
| 2705 | /* Wait objects support */ |
| 2706 | typedef struct WaitObjects { |
| 2707 | int num; |
| 2708 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2709 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2710 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2711 | } WaitObjects; |
| 2712 | |
| 2713 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2714 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2715 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2716 | { |
| 2717 | WaitObjects *w = &wait_objects; |
| 2718 | |
| 2719 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2720 | return -1; |
| 2721 | w->events[w->num] = handle; |
| 2722 | w->func[w->num] = func; |
| 2723 | w->opaque[w->num] = opaque; |
| 2724 | w->num++; |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2729 | { |
| 2730 | int i, found; |
| 2731 | WaitObjects *w = &wait_objects; |
| 2732 | |
| 2733 | found = 0; |
| 2734 | for (i = 0; i < w->num; i++) { |
| 2735 | if (w->events[i] == handle) |
| 2736 | found = 1; |
| 2737 | if (found) { |
| 2738 | w->events[i] = w->events[i + 1]; |
| 2739 | w->func[i] = w->func[i + 1]; |
| 2740 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2741 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2742 | } |
| 2743 | if (found) |
| 2744 | w->num--; |
| 2745 | } |
| 2746 | #endif |
| 2747 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2748 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2749 | /* ram save/restore */ |
| 2750 | |
Juan Quintela | 94fb090 | 2009-09-10 03:04:23 +0200 | [diff] [blame] | 2751 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2752 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2753 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2754 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2755 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2756 | |
| 2757 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2758 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2759 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2760 | uint32_t *array = (uint32_t *)page; |
| 2761 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2762 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2763 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2764 | if (array[i] != val) |
| 2765 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2766 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2767 | |
| 2768 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2771 | static int ram_save_block(QEMUFile *f) |
| 2772 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2773 | static ram_addr_t current_addr = 0; |
| 2774 | ram_addr_t saved_addr = current_addr; |
| 2775 | ram_addr_t addr = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2776 | int found = 0; |
| 2777 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2778 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2779 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2780 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2781 | |
| 2782 | cpu_physical_memory_reset_dirty(current_addr, |
| 2783 | current_addr + TARGET_PAGE_SIZE, |
| 2784 | MIGRATION_DIRTY_FLAG); |
| 2785 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2786 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2787 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2788 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2789 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2790 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2791 | } else { |
| 2792 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2793 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | found = 1; |
| 2797 | break; |
| 2798 | } |
| 2799 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2800 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | return found; |
| 2804 | } |
| 2805 | |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 2806 | static uint64_t bytes_transferred; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2807 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2808 | static ram_addr_t ram_save_remaining(void) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2809 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2810 | ram_addr_t addr; |
| 2811 | ram_addr_t count = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2812 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2813 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2814 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 2815 | count++; |
| 2816 | } |
| 2817 | |
| 2818 | return count; |
| 2819 | } |
| 2820 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2821 | uint64_t ram_bytes_remaining(void) |
| 2822 | { |
| 2823 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 2824 | } |
| 2825 | |
| 2826 | uint64_t ram_bytes_transferred(void) |
| 2827 | { |
| 2828 | return bytes_transferred; |
| 2829 | } |
| 2830 | |
| 2831 | uint64_t ram_bytes_total(void) |
| 2832 | { |
| 2833 | return last_ram_offset; |
| 2834 | } |
| 2835 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2836 | static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2837 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2838 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2839 | uint64_t bytes_transferred_last; |
| 2840 | double bwidth = 0; |
| 2841 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2842 | |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2843 | if (stage < 0) { |
| 2844 | cpu_physical_memory_set_dirty_tracking(0); |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 2848 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 2849 | qemu_file_set_error(f); |
| 2850 | return 0; |
| 2851 | } |
| 2852 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2853 | if (stage == 1) { |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 2854 | bytes_transferred = 0; |
| 2855 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2856 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2857 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2858 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 2859 | cpu_physical_memory_set_dirty(addr); |
| 2860 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 2861 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2862 | /* Enable dirty memory tracking */ |
| 2863 | cpu_physical_memory_set_dirty_tracking(1); |
| 2864 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2865 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2868 | bytes_transferred_last = bytes_transferred; |
| 2869 | bwidth = get_clock(); |
| 2870 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2871 | while (!qemu_file_rate_limit(f)) { |
| 2872 | int ret; |
| 2873 | |
| 2874 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2875 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2876 | if (ret == 0) /* no more blocks */ |
| 2877 | break; |
| 2878 | } |
| 2879 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2880 | bwidth = get_clock() - bwidth; |
| 2881 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 2882 | |
| 2883 | /* if we haven't transferred anything this round, force expected_time to a |
| 2884 | * a very high value, but without crashing */ |
| 2885 | if (bwidth == 0) |
| 2886 | bwidth = 0.000001; |
| 2887 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2888 | /* try transferring iterative blocks of memory */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2889 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2890 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2891 | while (ram_save_block(f) != 0) { |
| 2892 | bytes_transferred += TARGET_PAGE_SIZE; |
| 2893 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 2894 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 2898 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2899 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 2900 | |
| 2901 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2904 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 2905 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2906 | ram_addr_t addr; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2907 | int flags; |
| 2908 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2909 | if (version_id != 3) |
| 2910 | return -EINVAL; |
| 2911 | |
| 2912 | do { |
| 2913 | addr = qemu_get_be64(f); |
| 2914 | |
| 2915 | flags = addr & ~TARGET_PAGE_MASK; |
| 2916 | addr &= TARGET_PAGE_MASK; |
| 2917 | |
| 2918 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2919 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2920 | return -EINVAL; |
| 2921 | } |
| 2922 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2923 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 2924 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 2925 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 2926 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 2927 | if (ch == 0 && |
| 2928 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 2929 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 2930 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 2931 | #endif |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2932 | } else if (flags & RAM_SAVE_FLAG_PAGE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2933 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2934 | } |
| 2935 | if (qemu_file_has_error(f)) { |
| 2936 | return -EIO; |
| 2937 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2938 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 2939 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2940 | return 0; |
| 2941 | } |
| 2942 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 2943 | void qemu_service_io(void) |
| 2944 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 2945 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2948 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2949 | /* machine registration */ |
| 2950 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 2951 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2952 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2953 | |
| 2954 | int qemu_register_machine(QEMUMachine *m) |
| 2955 | { |
| 2956 | QEMUMachine **pm; |
| 2957 | pm = &first_machine; |
| 2958 | while (*pm != NULL) |
| 2959 | pm = &(*pm)->next; |
| 2960 | m->next = NULL; |
| 2961 | *pm = m; |
| 2962 | return 0; |
| 2963 | } |
| 2964 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 2965 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2966 | { |
| 2967 | QEMUMachine *m; |
| 2968 | |
| 2969 | for(m = first_machine; m != NULL; m = m->next) { |
| 2970 | if (!strcmp(m->name, name)) |
| 2971 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 2972 | if (m->alias && !strcmp(m->alias, name)) |
| 2973 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2974 | } |
| 2975 | return NULL; |
| 2976 | } |
| 2977 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 2978 | static QEMUMachine *find_default_machine(void) |
| 2979 | { |
| 2980 | QEMUMachine *m; |
| 2981 | |
| 2982 | for(m = first_machine; m != NULL; m = m->next) { |
| 2983 | if (m->is_default) { |
| 2984 | return m; |
| 2985 | } |
| 2986 | } |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2990 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2991 | /* main execution loop */ |
| 2992 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 2993 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2994 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 2995 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 2996 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 2997 | DisplayChangeListener *dcl = ds->listeners; |
| 2998 | |
| 2999 | dpy_refresh(ds); |
| 3000 | |
| 3001 | while (dcl != NULL) { |
| 3002 | if (dcl->gui_timer_interval && |
| 3003 | dcl->gui_timer_interval < interval) |
| 3004 | interval = dcl->gui_timer_interval; |
| 3005 | dcl = dcl->next; |
| 3006 | } |
| 3007 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3010 | static void nographic_update(void *opaque) |
| 3011 | { |
| 3012 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3013 | |
| 3014 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3015 | } |
| 3016 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3017 | struct vm_change_state_entry { |
| 3018 | VMChangeStateHandler *cb; |
| 3019 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3020 | QLIST_ENTRY (vm_change_state_entry) entries; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3021 | }; |
| 3022 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3023 | static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3024 | |
| 3025 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3026 | void *opaque) |
| 3027 | { |
| 3028 | VMChangeStateEntry *e; |
| 3029 | |
| 3030 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3031 | |
| 3032 | e->cb = cb; |
| 3033 | e->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3034 | QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3035 | return e; |
| 3036 | } |
| 3037 | |
| 3038 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3039 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3040 | QLIST_REMOVE (e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3041 | qemu_free (e); |
| 3042 | } |
| 3043 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3044 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3045 | { |
| 3046 | VMChangeStateEntry *e; |
| 3047 | |
| 3048 | 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] | 3049 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3050 | } |
| 3051 | } |
| 3052 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3053 | static void resume_all_vcpus(void); |
| 3054 | static void pause_all_vcpus(void); |
| 3055 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3056 | void vm_start(void) |
| 3057 | { |
| 3058 | if (!vm_running) { |
| 3059 | cpu_enable_ticks(); |
| 3060 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3061 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3062 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3063 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3067 | /* reset/shutdown handler */ |
| 3068 | |
| 3069 | typedef struct QEMUResetEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3070 | QTAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3071 | QEMUResetHandler *func; |
| 3072 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3073 | } QEMUResetEntry; |
| 3074 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3075 | static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3076 | QTAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3077 | static int reset_requested; |
| 3078 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3079 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3080 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3081 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3082 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3083 | int qemu_shutdown_requested(void) |
| 3084 | { |
| 3085 | int r = shutdown_requested; |
| 3086 | shutdown_requested = 0; |
| 3087 | return r; |
| 3088 | } |
| 3089 | |
| 3090 | int qemu_reset_requested(void) |
| 3091 | { |
| 3092 | int r = reset_requested; |
| 3093 | reset_requested = 0; |
| 3094 | return r; |
| 3095 | } |
| 3096 | |
| 3097 | int qemu_powerdown_requested(void) |
| 3098 | { |
| 3099 | int r = powerdown_requested; |
| 3100 | powerdown_requested = 0; |
| 3101 | return r; |
| 3102 | } |
| 3103 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3104 | static int qemu_debug_requested(void) |
| 3105 | { |
| 3106 | int r = debug_requested; |
| 3107 | debug_requested = 0; |
| 3108 | return r; |
| 3109 | } |
| 3110 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3111 | static int qemu_vmstop_requested(void) |
| 3112 | { |
| 3113 | int r = vmstop_requested; |
| 3114 | vmstop_requested = 0; |
| 3115 | return r; |
| 3116 | } |
| 3117 | |
| 3118 | static void do_vm_stop(int reason) |
| 3119 | { |
| 3120 | if (vm_running) { |
| 3121 | cpu_disable_ticks(); |
| 3122 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3123 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3124 | vm_state_notify(0, reason); |
| 3125 | } |
| 3126 | } |
| 3127 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3128 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3129 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3130 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3131 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3132 | re->func = func; |
| 3133 | re->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3134 | QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3137 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3138 | { |
| 3139 | QEMUResetEntry *re; |
| 3140 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3141 | QTAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3142 | if (re->func == func && re->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3143 | QTAILQ_REMOVE(&reset_handlers, re, entry); |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3144 | qemu_free(re); |
| 3145 | return; |
| 3146 | } |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | void qemu_system_reset(void) |
| 3151 | { |
| 3152 | QEMUResetEntry *re, *nre; |
| 3153 | |
| 3154 | /* reset all devices */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3155 | QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3156 | re->func(re->opaque); |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | void qemu_system_reset_request(void) |
| 3161 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3162 | if (no_reboot) { |
| 3163 | shutdown_requested = 1; |
| 3164 | } else { |
| 3165 | reset_requested = 1; |
| 3166 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3167 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | void qemu_system_shutdown_request(void) |
| 3171 | { |
| 3172 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3173 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3176 | void qemu_system_powerdown_request(void) |
| 3177 | { |
| 3178 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3179 | qemu_notify_event(); |
| 3180 | } |
| 3181 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3182 | #ifdef CONFIG_IOTHREAD |
| 3183 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3184 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3185 | vmstop_requested = reason; |
| 3186 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3187 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3188 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3189 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3190 | #ifndef _WIN32 |
| 3191 | static int io_thread_fd = -1; |
| 3192 | |
| 3193 | static void qemu_event_increment(void) |
| 3194 | { |
| 3195 | static const char byte = 0; |
Kirill A. Shutemov | dc330e2 | 2010-01-20 00:56:18 +0100 | [diff] [blame] | 3196 | ssize_t ret; |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3197 | |
| 3198 | if (io_thread_fd == -1) |
| 3199 | return; |
| 3200 | |
Kirill A. Shutemov | dc330e2 | 2010-01-20 00:56:18 +0100 | [diff] [blame] | 3201 | ret = write(io_thread_fd, &byte, sizeof(byte)); |
| 3202 | if (ret < 0 && (errno != EINTR && errno != EAGAIN)) { |
| 3203 | fprintf(stderr, "qemu_event_increment: write() filed: %s\n", |
| 3204 | strerror(errno)); |
| 3205 | exit (1); |
| 3206 | } |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | static void qemu_event_read(void *opaque) |
| 3210 | { |
| 3211 | int fd = (unsigned long)opaque; |
| 3212 | ssize_t len; |
| 3213 | |
| 3214 | /* Drain the notify pipe */ |
| 3215 | do { |
| 3216 | char buffer[512]; |
| 3217 | len = read(fd, buffer, sizeof(buffer)); |
| 3218 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3219 | } |
| 3220 | |
| 3221 | static int qemu_event_init(void) |
| 3222 | { |
| 3223 | int err; |
| 3224 | int fds[2]; |
| 3225 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 3226 | err = qemu_pipe(fds); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3227 | if (err == -1) |
| 3228 | return -errno; |
| 3229 | |
| 3230 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3231 | if (err < 0) |
| 3232 | goto fail; |
| 3233 | |
| 3234 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3235 | if (err < 0) |
| 3236 | goto fail; |
| 3237 | |
| 3238 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3239 | (void *)(unsigned long)fds[0]); |
| 3240 | |
| 3241 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3242 | return 0; |
| 3243 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3244 | fail: |
| 3245 | close(fds[0]); |
| 3246 | close(fds[1]); |
| 3247 | return err; |
| 3248 | } |
| 3249 | #else |
| 3250 | HANDLE qemu_event_handle; |
| 3251 | |
| 3252 | static void dummy_event_handler(void *opaque) |
| 3253 | { |
| 3254 | } |
| 3255 | |
| 3256 | static int qemu_event_init(void) |
| 3257 | { |
| 3258 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3259 | if (!qemu_event_handle) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3260 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3261 | return -1; |
| 3262 | } |
| 3263 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | static void qemu_event_increment(void) |
| 3268 | { |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3269 | if (!SetEvent(qemu_event_handle)) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3270 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3271 | GetLastError()); |
| 3272 | exit (1); |
| 3273 | } |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3274 | } |
| 3275 | #endif |
| 3276 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3277 | static int cpu_can_run(CPUState *env) |
| 3278 | { |
| 3279 | if (env->stop) |
| 3280 | return 0; |
| 3281 | if (env->stopped) |
| 3282 | return 0; |
| 3283 | return 1; |
| 3284 | } |
| 3285 | |
| 3286 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3287 | static int qemu_init_main_loop(void) |
| 3288 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3289 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3292 | void qemu_init_vcpu(void *_env) |
| 3293 | { |
| 3294 | CPUState *env = _env; |
| 3295 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3296 | env->nr_cores = smp_cores; |
| 3297 | env->nr_threads = smp_threads; |
Jiri Denemark | 3f7638e | 2010-01-05 17:26:34 +0100 | [diff] [blame] | 3298 | if (kvm_enabled()) |
| 3299 | kvm_init_vcpu(env); |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3300 | return; |
| 3301 | } |
| 3302 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3303 | int qemu_cpu_self(void *env) |
| 3304 | { |
| 3305 | return 1; |
| 3306 | } |
| 3307 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3308 | static void resume_all_vcpus(void) |
| 3309 | { |
| 3310 | } |
| 3311 | |
| 3312 | static void pause_all_vcpus(void) |
| 3313 | { |
| 3314 | } |
| 3315 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3316 | void qemu_cpu_kick(void *env) |
| 3317 | { |
| 3318 | return; |
| 3319 | } |
| 3320 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3321 | void qemu_notify_event(void) |
| 3322 | { |
| 3323 | CPUState *env = cpu_single_env; |
| 3324 | |
| 3325 | if (env) { |
| 3326 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3327 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3330 | void qemu_mutex_lock_iothread(void) {} |
| 3331 | void qemu_mutex_unlock_iothread(void) {} |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3332 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3333 | void vm_stop(int reason) |
| 3334 | { |
| 3335 | do_vm_stop(reason); |
| 3336 | } |
| 3337 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3338 | #else /* CONFIG_IOTHREAD */ |
| 3339 | |
| 3340 | #include "qemu-thread.h" |
| 3341 | |
| 3342 | QemuMutex qemu_global_mutex; |
| 3343 | static QemuMutex qemu_fair_mutex; |
| 3344 | |
| 3345 | static QemuThread io_thread; |
| 3346 | |
| 3347 | static QemuThread *tcg_cpu_thread; |
| 3348 | static QemuCond *tcg_halt_cond; |
| 3349 | |
| 3350 | static int qemu_system_ready; |
| 3351 | /* cpu creation */ |
| 3352 | static QemuCond qemu_cpu_cond; |
| 3353 | /* system init */ |
| 3354 | static QemuCond qemu_system_cond; |
| 3355 | static QemuCond qemu_pause_cond; |
| 3356 | |
| 3357 | static void block_io_signals(void); |
| 3358 | static void unblock_io_signals(void); |
| 3359 | static int tcg_has_work(void); |
| 3360 | |
| 3361 | static int qemu_init_main_loop(void) |
| 3362 | { |
| 3363 | int ret; |
| 3364 | |
| 3365 | ret = qemu_event_init(); |
| 3366 | if (ret) |
| 3367 | return ret; |
| 3368 | |
| 3369 | qemu_cond_init(&qemu_pause_cond); |
| 3370 | qemu_mutex_init(&qemu_fair_mutex); |
| 3371 | qemu_mutex_init(&qemu_global_mutex); |
| 3372 | qemu_mutex_lock(&qemu_global_mutex); |
| 3373 | |
| 3374 | unblock_io_signals(); |
| 3375 | qemu_thread_self(&io_thread); |
| 3376 | |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
| 3380 | static void qemu_wait_io_event(CPUState *env) |
| 3381 | { |
| 3382 | while (!tcg_has_work()) |
| 3383 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3384 | |
| 3385 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3386 | |
| 3387 | /* |
| 3388 | * Users of qemu_global_mutex can be starved, having no chance |
| 3389 | * to acquire it since this path will get to it first. |
| 3390 | * So use another lock to provide fairness. |
| 3391 | */ |
| 3392 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3393 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3394 | |
| 3395 | qemu_mutex_lock(&qemu_global_mutex); |
| 3396 | if (env->stop) { |
| 3397 | env->stop = 0; |
| 3398 | env->stopped = 1; |
| 3399 | qemu_cond_signal(&qemu_pause_cond); |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | static int qemu_cpu_exec(CPUState *env); |
| 3404 | |
| 3405 | static void *kvm_cpu_thread_fn(void *arg) |
| 3406 | { |
| 3407 | CPUState *env = arg; |
| 3408 | |
| 3409 | block_io_signals(); |
| 3410 | qemu_thread_self(env->thread); |
Jean-Christophe DUBOIS | 321c1cb | 2009-09-02 23:59:04 +0200 | [diff] [blame] | 3411 | if (kvm_enabled()) |
| 3412 | kvm_init_vcpu(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3413 | |
| 3414 | /* signal CPU creation */ |
| 3415 | qemu_mutex_lock(&qemu_global_mutex); |
| 3416 | env->created = 1; |
| 3417 | qemu_cond_signal(&qemu_cpu_cond); |
| 3418 | |
| 3419 | /* and wait for machine initialization */ |
| 3420 | while (!qemu_system_ready) |
| 3421 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3422 | |
| 3423 | while (1) { |
| 3424 | if (cpu_can_run(env)) |
| 3425 | qemu_cpu_exec(env); |
Anthony Liguori | 1c3173b | 2009-09-10 08:45:43 -0500 | [diff] [blame] | 3426 | qemu_wait_io_event(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | return NULL; |
| 3430 | } |
| 3431 | |
| 3432 | static void tcg_cpu_exec(void); |
| 3433 | |
| 3434 | static void *tcg_cpu_thread_fn(void *arg) |
| 3435 | { |
| 3436 | CPUState *env = arg; |
| 3437 | |
| 3438 | block_io_signals(); |
| 3439 | qemu_thread_self(env->thread); |
| 3440 | |
| 3441 | /* signal CPU creation */ |
| 3442 | qemu_mutex_lock(&qemu_global_mutex); |
| 3443 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3444 | env->created = 1; |
| 3445 | qemu_cond_signal(&qemu_cpu_cond); |
| 3446 | |
| 3447 | /* and wait for machine initialization */ |
| 3448 | while (!qemu_system_ready) |
| 3449 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3450 | |
| 3451 | while (1) { |
| 3452 | tcg_cpu_exec(); |
| 3453 | qemu_wait_io_event(cur_cpu); |
| 3454 | } |
| 3455 | |
| 3456 | return NULL; |
| 3457 | } |
| 3458 | |
| 3459 | void qemu_cpu_kick(void *_env) |
| 3460 | { |
| 3461 | CPUState *env = _env; |
| 3462 | qemu_cond_broadcast(env->halt_cond); |
| 3463 | if (kvm_enabled()) |
| 3464 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3465 | } |
| 3466 | |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3467 | int qemu_cpu_self(void *_env) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3468 | { |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3469 | CPUState *env = _env; |
| 3470 | QemuThread this; |
| 3471 | |
| 3472 | qemu_thread_self(&this); |
| 3473 | |
| 3474 | return qemu_thread_equal(&this, env->thread); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | static void cpu_signal(int sig) |
| 3478 | { |
| 3479 | if (cpu_single_env) |
| 3480 | cpu_exit(cpu_single_env); |
| 3481 | } |
| 3482 | |
| 3483 | static void block_io_signals(void) |
| 3484 | { |
| 3485 | sigset_t set; |
| 3486 | struct sigaction sigact; |
| 3487 | |
| 3488 | sigemptyset(&set); |
| 3489 | sigaddset(&set, SIGUSR2); |
| 3490 | sigaddset(&set, SIGIO); |
| 3491 | sigaddset(&set, SIGALRM); |
| 3492 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3493 | |
| 3494 | sigemptyset(&set); |
| 3495 | sigaddset(&set, SIGUSR1); |
| 3496 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3497 | |
| 3498 | memset(&sigact, 0, sizeof(sigact)); |
| 3499 | sigact.sa_handler = cpu_signal; |
| 3500 | sigaction(SIGUSR1, &sigact, NULL); |
| 3501 | } |
| 3502 | |
| 3503 | static void unblock_io_signals(void) |
| 3504 | { |
| 3505 | sigset_t set; |
| 3506 | |
| 3507 | sigemptyset(&set); |
| 3508 | sigaddset(&set, SIGUSR2); |
| 3509 | sigaddset(&set, SIGIO); |
| 3510 | sigaddset(&set, SIGALRM); |
| 3511 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3512 | |
| 3513 | sigemptyset(&set); |
| 3514 | sigaddset(&set, SIGUSR1); |
| 3515 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3516 | } |
| 3517 | |
| 3518 | static void qemu_signal_lock(unsigned int msecs) |
| 3519 | { |
| 3520 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3521 | |
| 3522 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3523 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3524 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3525 | break; |
| 3526 | } |
| 3527 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3528 | } |
| 3529 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3530 | void qemu_mutex_lock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3531 | { |
| 3532 | if (kvm_enabled()) { |
| 3533 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3534 | qemu_mutex_lock(&qemu_global_mutex); |
| 3535 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3536 | } else |
| 3537 | qemu_signal_lock(100); |
| 3538 | } |
| 3539 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3540 | void qemu_mutex_unlock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3541 | { |
| 3542 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3543 | } |
| 3544 | |
| 3545 | static int all_vcpus_paused(void) |
| 3546 | { |
| 3547 | CPUState *penv = first_cpu; |
| 3548 | |
| 3549 | while (penv) { |
| 3550 | if (!penv->stopped) |
| 3551 | return 0; |
| 3552 | penv = (CPUState *)penv->next_cpu; |
| 3553 | } |
| 3554 | |
| 3555 | return 1; |
| 3556 | } |
| 3557 | |
| 3558 | static void pause_all_vcpus(void) |
| 3559 | { |
| 3560 | CPUState *penv = first_cpu; |
| 3561 | |
| 3562 | while (penv) { |
| 3563 | penv->stop = 1; |
| 3564 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3565 | qemu_cpu_kick(penv); |
| 3566 | penv = (CPUState *)penv->next_cpu; |
| 3567 | } |
| 3568 | |
| 3569 | while (!all_vcpus_paused()) { |
| 3570 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3571 | penv = first_cpu; |
| 3572 | while (penv) { |
| 3573 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3574 | penv = (CPUState *)penv->next_cpu; |
| 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | static void resume_all_vcpus(void) |
| 3580 | { |
| 3581 | CPUState *penv = first_cpu; |
| 3582 | |
| 3583 | while (penv) { |
| 3584 | penv->stop = 0; |
| 3585 | penv->stopped = 0; |
| 3586 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3587 | qemu_cpu_kick(penv); |
| 3588 | penv = (CPUState *)penv->next_cpu; |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | static void tcg_init_vcpu(void *_env) |
| 3593 | { |
| 3594 | CPUState *env = _env; |
| 3595 | /* share a single thread for all cpus with TCG */ |
| 3596 | if (!tcg_cpu_thread) { |
| 3597 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3598 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3599 | qemu_cond_init(env->halt_cond); |
| 3600 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3601 | while (env->created == 0) |
| 3602 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3603 | tcg_cpu_thread = env->thread; |
| 3604 | tcg_halt_cond = env->halt_cond; |
| 3605 | } else { |
| 3606 | env->thread = tcg_cpu_thread; |
| 3607 | env->halt_cond = tcg_halt_cond; |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | static void kvm_start_vcpu(CPUState *env) |
| 3612 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3613 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3614 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3615 | qemu_cond_init(env->halt_cond); |
| 3616 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3617 | while (env->created == 0) |
| 3618 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3619 | } |
| 3620 | |
| 3621 | void qemu_init_vcpu(void *_env) |
| 3622 | { |
| 3623 | CPUState *env = _env; |
| 3624 | |
Jiri Denemark | 3f7638e | 2010-01-05 17:26:34 +0100 | [diff] [blame] | 3625 | env->nr_cores = smp_cores; |
| 3626 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3627 | if (kvm_enabled()) |
| 3628 | kvm_start_vcpu(env); |
| 3629 | else |
| 3630 | tcg_init_vcpu(env); |
| 3631 | } |
| 3632 | |
| 3633 | void qemu_notify_event(void) |
| 3634 | { |
| 3635 | qemu_event_increment(); |
| 3636 | } |
| 3637 | |
| 3638 | void vm_stop(int reason) |
| 3639 | { |
| 3640 | QemuThread me; |
| 3641 | qemu_thread_self(&me); |
| 3642 | |
| 3643 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3644 | qemu_system_vmstop_request(reason); |
| 3645 | /* |
| 3646 | * FIXME: should not return to device code in case |
| 3647 | * vm_stop() has been requested. |
| 3648 | */ |
| 3649 | if (cpu_single_env) { |
| 3650 | cpu_exit(cpu_single_env); |
| 3651 | cpu_single_env->stop = 1; |
| 3652 | } |
| 3653 | return; |
| 3654 | } |
| 3655 | do_vm_stop(reason); |
| 3656 | } |
| 3657 | |
| 3658 | #endif |
| 3659 | |
| 3660 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3661 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3662 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3663 | { |
| 3664 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3665 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3666 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3667 | |
| 3668 | /* XXX: need to suppress polling by better using win32 events */ |
| 3669 | ret = 0; |
| 3670 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 3671 | ret |= pe->func(pe->opaque); |
| 3672 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3673 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3674 | int err; |
| 3675 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3676 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3677 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3678 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 3679 | if (w->func[ret - WAIT_OBJECT_0]) |
| 3680 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3681 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3682 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3683 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3684 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3685 | /* Check if event is signaled */ |
| 3686 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 3687 | if(ret2 == WAIT_OBJECT_0) { |
| 3688 | if (w->func[i]) |
| 3689 | w->func[i](w->opaque[i]); |
| 3690 | } else if (ret2 == WAIT_TIMEOUT) { |
| 3691 | } else { |
| 3692 | err = GetLastError(); |
| 3693 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3694 | } |
| 3695 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3696 | } else if (ret == WAIT_TIMEOUT) { |
| 3697 | } else { |
| 3698 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3699 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3700 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3701 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3702 | |
| 3703 | *timeout = 0; |
| 3704 | } |
| 3705 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3706 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3707 | { |
| 3708 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3709 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3710 | |
| 3711 | void main_loop_wait(int timeout) |
| 3712 | { |
| 3713 | IOHandlerRecord *ioh; |
| 3714 | fd_set rfds, wfds, xfds; |
| 3715 | int ret, nfds; |
| 3716 | struct timeval tv; |
| 3717 | |
| 3718 | qemu_bh_update_timeout(&timeout); |
| 3719 | |
| 3720 | host_main_loop_wait(&timeout); |
| 3721 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3722 | /* poll any events */ |
| 3723 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 3724 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3725 | FD_ZERO(&rfds); |
| 3726 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3727 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3728 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3729 | if (ioh->deleted) |
| 3730 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3731 | if (ioh->fd_read && |
| 3732 | (!ioh->fd_read_poll || |
| 3733 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 3734 | FD_SET(ioh->fd, &rfds); |
| 3735 | if (ioh->fd > nfds) |
| 3736 | nfds = ioh->fd; |
| 3737 | } |
| 3738 | if (ioh->fd_write) { |
| 3739 | FD_SET(ioh->fd, &wfds); |
| 3740 | if (ioh->fd > nfds) |
| 3741 | nfds = ioh->fd; |
| 3742 | } |
| 3743 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3744 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3745 | tv.tv_sec = timeout / 1000; |
| 3746 | tv.tv_usec = (timeout % 1000) * 1000; |
| 3747 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3748 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 3749 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3750 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3751 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3752 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3753 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3754 | IOHandlerRecord **pioh; |
| 3755 | |
| 3756 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3757 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3758 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3759 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3760 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3761 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3762 | } |
| 3763 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3764 | |
| 3765 | /* remove deleted IO handlers */ |
| 3766 | pioh = &first_io_handler; |
| 3767 | while (*pioh) { |
| 3768 | ioh = *pioh; |
| 3769 | if (ioh->deleted) { |
| 3770 | *pioh = ioh->next; |
| 3771 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3772 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3773 | pioh = &ioh->next; |
| 3774 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3775 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3776 | |
| 3777 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3778 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3779 | /* rearm timer, if not periodic */ |
| 3780 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 3781 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 3782 | qemu_rearm_alarm_timer(alarm_timer); |
| 3783 | } |
| 3784 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3785 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3786 | if (vm_running) { |
| 3787 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3788 | qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL], |
| 3789 | qemu_get_clock(vm_clock)); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3790 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3791 | |
| 3792 | /* real time timers */ |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3793 | qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME], |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3794 | qemu_get_clock(rt_clock)); |
| 3795 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3796 | qemu_run_timers(&active_timers[QEMU_CLOCK_HOST], |
| 3797 | qemu_get_clock(host_clock)); |
| 3798 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 3799 | /* Check bottom-halves last in case any of the earlier events triggered |
| 3800 | them. */ |
| 3801 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3802 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3805 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3806 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3807 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 3808 | #ifdef CONFIG_PROFILER |
| 3809 | int64_t ti; |
| 3810 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3811 | |
| 3812 | #ifdef CONFIG_PROFILER |
| 3813 | ti = profile_getclock(); |
| 3814 | #endif |
| 3815 | if (use_icount) { |
| 3816 | int64_t count; |
| 3817 | int decr; |
| 3818 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 3819 | env->icount_decr.u16.low = 0; |
| 3820 | env->icount_extra = 0; |
| 3821 | count = qemu_next_deadline(); |
| 3822 | count = (count + (1 << icount_time_shift) - 1) |
| 3823 | >> icount_time_shift; |
| 3824 | qemu_icount += count; |
| 3825 | decr = (count > 0xffff) ? 0xffff : count; |
| 3826 | count -= decr; |
| 3827 | env->icount_decr.u16.low = decr; |
| 3828 | env->icount_extra = count; |
| 3829 | } |
| 3830 | ret = cpu_exec(env); |
| 3831 | #ifdef CONFIG_PROFILER |
| 3832 | qemu_time += profile_getclock() - ti; |
| 3833 | #endif |
| 3834 | if (use_icount) { |
| 3835 | /* Fold pending instructions back into the |
| 3836 | instruction counter, and clear the interrupt flag. */ |
| 3837 | qemu_icount -= (env->icount_decr.u16.low |
| 3838 | + env->icount_extra); |
| 3839 | env->icount_decr.u32 = 0; |
| 3840 | env->icount_extra = 0; |
| 3841 | } |
| 3842 | return ret; |
| 3843 | } |
| 3844 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3845 | static void tcg_cpu_exec(void) |
| 3846 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3847 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3848 | |
| 3849 | if (next_cpu == NULL) |
| 3850 | next_cpu = first_cpu; |
| 3851 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 3852 | CPUState *env = cur_cpu = next_cpu; |
| 3853 | |
| 3854 | if (!vm_running) |
| 3855 | break; |
| 3856 | if (timer_alarm_pending) { |
| 3857 | timer_alarm_pending = 0; |
| 3858 | break; |
| 3859 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3860 | if (cpu_can_run(env)) |
| 3861 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3862 | if (ret == EXCP_DEBUG) { |
| 3863 | gdb_set_stop_cpu(env); |
| 3864 | debug_requested = 1; |
| 3865 | break; |
| 3866 | } |
| 3867 | } |
| 3868 | } |
| 3869 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3870 | static int cpu_has_work(CPUState *env) |
| 3871 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3872 | if (env->stop) |
| 3873 | return 1; |
| 3874 | if (env->stopped) |
| 3875 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3876 | if (!env->halted) |
| 3877 | return 1; |
| 3878 | if (qemu_cpu_has_work(env)) |
| 3879 | return 1; |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
| 3883 | static int tcg_has_work(void) |
| 3884 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 3885 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3886 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3887 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3888 | if (cpu_has_work(env)) |
| 3889 | return 1; |
| 3890 | return 0; |
| 3891 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 3892 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3893 | static int qemu_calculate_timeout(void) |
| 3894 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 3895 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3896 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 3897 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3898 | if (!vm_running) |
| 3899 | timeout = 5000; |
| 3900 | else if (tcg_has_work()) |
| 3901 | timeout = 0; |
| 3902 | else if (!use_icount) |
| 3903 | timeout = 5000; |
| 3904 | else { |
| 3905 | /* XXX: use timeout computed from timers */ |
| 3906 | int64_t add; |
| 3907 | int64_t delta; |
| 3908 | /* Advance virtual time to the next event. */ |
| 3909 | if (use_icount == 1) { |
| 3910 | /* When not using an adaptive execution frequency |
| 3911 | we tend to get badly out of sync with real time, |
| 3912 | so just delay for a reasonable amount of time. */ |
| 3913 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3914 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3915 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3916 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3917 | if (delta > 0) { |
| 3918 | /* If virtual time is ahead of real time then just |
| 3919 | wait for IO. */ |
| 3920 | timeout = (delta / 1000000) + 1; |
| 3921 | } else { |
| 3922 | /* Wait for either IO to occur or the next |
| 3923 | timer event. */ |
| 3924 | add = qemu_next_deadline(); |
| 3925 | /* We advance the timer before checking for IO. |
| 3926 | Limit the amount we advance so that early IO |
| 3927 | activity won't get the guest too far ahead. */ |
| 3928 | if (add > 10000000) |
| 3929 | add = 10000000; |
| 3930 | delta += add; |
| 3931 | add = (add + (1 << icount_time_shift) - 1) |
| 3932 | >> icount_time_shift; |
| 3933 | qemu_icount += add; |
| 3934 | timeout = delta / 1000000; |
| 3935 | if (timeout < 0) |
| 3936 | timeout = 0; |
| 3937 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3938 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3939 | |
| 3940 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 3941 | #else /* CONFIG_IOTHREAD */ |
| 3942 | return 1000; |
| 3943 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | static int vm_can_run(void) |
| 3947 | { |
| 3948 | if (powerdown_requested) |
| 3949 | return 0; |
| 3950 | if (reset_requested) |
| 3951 | return 0; |
| 3952 | if (shutdown_requested) |
| 3953 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3954 | if (debug_requested) |
| 3955 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3956 | return 1; |
| 3957 | } |
| 3958 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 3959 | qemu_irq qemu_system_powerdown; |
| 3960 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3961 | static void main_loop(void) |
| 3962 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3963 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3964 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3965 | #ifdef CONFIG_IOTHREAD |
| 3966 | qemu_system_ready = 1; |
| 3967 | qemu_cond_broadcast(&qemu_system_cond); |
| 3968 | #endif |
| 3969 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3970 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3971 | do { |
| 3972 | #ifdef CONFIG_PROFILER |
| 3973 | int64_t ti; |
| 3974 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3975 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3976 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3977 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3978 | #ifdef CONFIG_PROFILER |
| 3979 | ti = profile_getclock(); |
| 3980 | #endif |
| 3981 | main_loop_wait(qemu_calculate_timeout()); |
| 3982 | #ifdef CONFIG_PROFILER |
| 3983 | dev_time += profile_getclock() - ti; |
| 3984 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3985 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3986 | |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 3987 | if (qemu_debug_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 3988 | monitor_protocol_event(QEVENT_DEBUG, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3989 | vm_stop(EXCP_DEBUG); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 3990 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3991 | if (qemu_shutdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 3992 | monitor_protocol_event(QEVENT_SHUTDOWN, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3993 | if (no_shutdown) { |
| 3994 | vm_stop(0); |
| 3995 | no_shutdown = 0; |
| 3996 | } else |
| 3997 | break; |
| 3998 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3999 | if (qemu_reset_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4000 | monitor_protocol_event(QEVENT_RESET, NULL); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4001 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4002 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4003 | resume_all_vcpus(); |
| 4004 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4005 | if (qemu_powerdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4006 | monitor_protocol_event(QEVENT_POWERDOWN, NULL); |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4007 | qemu_irq_raise(qemu_system_powerdown); |
| 4008 | } |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4009 | if ((r = qemu_vmstop_requested())) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4010 | monitor_protocol_event(QEVENT_STOP, NULL); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4011 | vm_stop(r); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4012 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4013 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4014 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4017 | static void version(void) |
| 4018 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4019 | 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] | 4020 | } |
| 4021 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4022 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4023 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4024 | version(); |
| 4025 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4026 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4027 | "'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] | 4028 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4029 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4030 | opt_help |
| 4031 | #define DEFHEADING(text) stringify(text) "\n" |
| 4032 | #include "qemu-options.h" |
| 4033 | #undef DEF |
| 4034 | #undef DEFHEADING |
| 4035 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4036 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4037 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4038 | "ctrl-alt-f toggle full screen\n" |
| 4039 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4040 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4041 | "\n" |
| 4042 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4043 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4044 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4045 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4046 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4047 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4048 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4049 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4050 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4051 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4052 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4055 | #define HAS_ARG 0x0001 |
| 4056 | |
| 4057 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4058 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4059 | opt_enum, |
| 4060 | #define DEFHEADING(text) |
| 4061 | #include "qemu-options.h" |
| 4062 | #undef DEF |
| 4063 | #undef DEFHEADING |
| 4064 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4065 | }; |
| 4066 | |
| 4067 | typedef struct QEMUOption { |
| 4068 | const char *name; |
| 4069 | int flags; |
| 4070 | int index; |
| 4071 | } QEMUOption; |
| 4072 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4073 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4074 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4075 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4076 | { option, opt_arg, opt_enum }, |
| 4077 | #define DEFHEADING(text) |
| 4078 | #include "qemu-options.h" |
| 4079 | #undef DEF |
| 4080 | #undef DEFHEADING |
| 4081 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4082 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4083 | }; |
| 4084 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4085 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4086 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4087 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4088 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4089 | { |
| 4090 | "pcspk", |
| 4091 | "PC speaker", |
| 4092 | 0, |
| 4093 | 1, |
| 4094 | { .init_isa = pcspk_audio_init } |
| 4095 | }, |
| 4096 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4097 | |
| 4098 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4099 | { |
| 4100 | "sb16", |
| 4101 | "Creative Sound Blaster 16", |
| 4102 | 0, |
| 4103 | 1, |
| 4104 | { .init_isa = SB16_init } |
| 4105 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4106 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4107 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4108 | #ifdef CONFIG_CS4231A |
| 4109 | { |
| 4110 | "cs4231a", |
| 4111 | "CS4231A", |
| 4112 | 0, |
| 4113 | 1, |
| 4114 | { .init_isa = cs4231a_init } |
| 4115 | }, |
| 4116 | #endif |
| 4117 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4118 | #ifdef CONFIG_ADLIB |
| 4119 | { |
| 4120 | "adlib", |
| 4121 | #ifdef HAS_YMF262 |
| 4122 | "Yamaha YMF262 (OPL3)", |
| 4123 | #else |
| 4124 | "Yamaha YM3812 (OPL2)", |
| 4125 | #endif |
| 4126 | 0, |
| 4127 | 1, |
| 4128 | { .init_isa = Adlib_init } |
| 4129 | }, |
| 4130 | #endif |
| 4131 | |
| 4132 | #ifdef CONFIG_GUS |
| 4133 | { |
| 4134 | "gus", |
| 4135 | "Gravis Ultrasound GF1", |
| 4136 | 0, |
| 4137 | 1, |
| 4138 | { .init_isa = GUS_init } |
| 4139 | }, |
| 4140 | #endif |
| 4141 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4142 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4143 | { |
| 4144 | "ac97", |
| 4145 | "Intel 82801AA AC97 Audio", |
| 4146 | 0, |
| 4147 | 0, |
| 4148 | { .init_pci = ac97_init } |
| 4149 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4150 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4151 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4152 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4153 | { |
| 4154 | "es1370", |
| 4155 | "ENSONIQ AudioPCI ES1370", |
| 4156 | 0, |
| 4157 | 0, |
| 4158 | { .init_pci = es1370_init } |
| 4159 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4160 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4161 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4162 | #endif /* HAS_AUDIO_CHOICE */ |
| 4163 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4164 | { NULL, NULL, 0, 0, { NULL } } |
| 4165 | }; |
| 4166 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4167 | static void select_soundhw (const char *optarg) |
| 4168 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4169 | struct soundhw *c; |
| 4170 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4171 | if (*optarg == '?') { |
| 4172 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4173 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4174 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4175 | for (c = soundhw; c->name; ++c) { |
| 4176 | printf ("%-11s %s\n", c->name, c->descr); |
| 4177 | } |
| 4178 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4179 | exit (*optarg != '?'); |
| 4180 | } |
| 4181 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4182 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4183 | const char *p; |
| 4184 | char *e; |
| 4185 | int bad_card = 0; |
| 4186 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4187 | if (!strcmp (optarg, "all")) { |
| 4188 | for (c = soundhw; c->name; ++c) { |
| 4189 | c->enabled = 1; |
| 4190 | } |
| 4191 | return; |
| 4192 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4193 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4194 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4195 | while (*p) { |
| 4196 | e = strchr (p, ','); |
| 4197 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4198 | |
| 4199 | for (c = soundhw; c->name; ++c) { |
malc | b3d6fb4 | 2009-09-06 06:49:03 +0400 | [diff] [blame] | 4200 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4201 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4202 | break; |
| 4203 | } |
| 4204 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4205 | |
| 4206 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4207 | if (l > 80) { |
| 4208 | fprintf (stderr, |
| 4209 | "Unknown sound card name (too big to show)\n"); |
| 4210 | } |
| 4211 | else { |
| 4212 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4213 | (int) l, p); |
| 4214 | } |
| 4215 | bad_card = 1; |
| 4216 | } |
| 4217 | p += l + (e != NULL); |
| 4218 | } |
| 4219 | |
| 4220 | if (bad_card) |
| 4221 | goto show_valid_cards; |
| 4222 | } |
| 4223 | } |
| 4224 | #endif |
| 4225 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4226 | static void select_vgahw (const char *p) |
| 4227 | { |
| 4228 | const char *opts; |
| 4229 | |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 4230 | default_vga = 0; |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4231 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4232 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4233 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4234 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4235 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4236 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4237 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4238 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4239 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4240 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4241 | invalid_vga: |
| 4242 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4243 | exit(1); |
| 4244 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4245 | while (*opts) { |
| 4246 | const char *nextopt; |
| 4247 | |
| 4248 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4249 | opts = nextopt; |
| 4250 | if (strstart(opts, "dumb", &nextopt)) |
| 4251 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4252 | else if (strstart(opts, "precise", &nextopt)) |
| 4253 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4254 | else goto invalid_vga; |
| 4255 | } else goto invalid_vga; |
| 4256 | opts = nextopt; |
| 4257 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4260 | #ifdef TARGET_I386 |
| 4261 | static int balloon_parse(const char *arg) |
| 4262 | { |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4263 | QemuOpts *opts; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4264 | |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4265 | if (strcmp(arg, "none") == 0) { |
| 4266 | return 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4267 | } |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4268 | |
| 4269 | if (!strncmp(arg, "virtio", 6)) { |
| 4270 | if (arg[6] == ',') { |
| 4271 | /* have params -> parse them */ |
| 4272 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
| 4273 | if (!opts) |
| 4274 | return -1; |
| 4275 | } else { |
| 4276 | /* create empty opts */ |
| 4277 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4278 | } |
| 4279 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
| 4280 | return 0; |
| 4281 | } |
| 4282 | |
| 4283 | return -1; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4284 | } |
| 4285 | #endif |
| 4286 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4287 | #ifdef _WIN32 |
| 4288 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4289 | { |
| 4290 | exit(STATUS_CONTROL_C_EXIT); |
| 4291 | return TRUE; |
| 4292 | } |
| 4293 | #endif |
| 4294 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4295 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4296 | { |
| 4297 | int ret; |
| 4298 | |
| 4299 | if(strlen(str) != 36) |
| 4300 | return -1; |
| 4301 | |
| 4302 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4303 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4304 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4305 | |
| 4306 | if(ret != 16) |
| 4307 | return -1; |
| 4308 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4309 | #ifdef TARGET_I386 |
| 4310 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4311 | #endif |
| 4312 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4313 | return 0; |
| 4314 | } |
| 4315 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4316 | #ifndef _WIN32 |
| 4317 | |
| 4318 | static void termsig_handler(int signal) |
| 4319 | { |
| 4320 | qemu_system_shutdown_request(); |
| 4321 | } |
| 4322 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4323 | static void sigchld_handler(int signal) |
| 4324 | { |
| 4325 | waitpid(-1, NULL, WNOHANG); |
| 4326 | } |
| 4327 | |
| 4328 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4329 | { |
| 4330 | struct sigaction act; |
| 4331 | |
| 4332 | memset(&act, 0, sizeof(act)); |
| 4333 | act.sa_handler = termsig_handler; |
| 4334 | sigaction(SIGINT, &act, NULL); |
| 4335 | sigaction(SIGHUP, &act, NULL); |
| 4336 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4337 | |
| 4338 | act.sa_handler = sigchld_handler; |
| 4339 | act.sa_flags = SA_NOCLDSTOP; |
| 4340 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | #endif |
| 4344 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4345 | #ifdef _WIN32 |
| 4346 | /* Look for support files in the same directory as the executable. */ |
| 4347 | static char *find_datadir(const char *argv0) |
| 4348 | { |
| 4349 | char *p; |
| 4350 | char buf[MAX_PATH]; |
| 4351 | DWORD len; |
| 4352 | |
| 4353 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4354 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4355 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | buf[len] = 0; |
| 4359 | p = buf + len - 1; |
| 4360 | while (p != buf && *p != '\\') |
| 4361 | p--; |
| 4362 | *p = 0; |
| 4363 | if (access(buf, R_OK) == 0) { |
| 4364 | return qemu_strdup(buf); |
| 4365 | } |
| 4366 | return NULL; |
| 4367 | } |
| 4368 | #else /* !_WIN32 */ |
| 4369 | |
| 4370 | /* Find a likely location for support files using the location of the binary. |
| 4371 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4372 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4373 | #define SHARE_SUFFIX "/share/qemu" |
| 4374 | #define BUILD_SUFFIX "/pc-bios" |
| 4375 | static char *find_datadir(const char *argv0) |
| 4376 | { |
| 4377 | char *dir; |
| 4378 | char *p = NULL; |
| 4379 | char *res; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4380 | char buf[PATH_MAX]; |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4381 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4382 | |
| 4383 | #if defined(__linux__) |
| 4384 | { |
| 4385 | int len; |
| 4386 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4387 | if (len > 0) { |
| 4388 | buf[len] = 0; |
| 4389 | p = buf; |
| 4390 | } |
| 4391 | } |
| 4392 | #elif defined(__FreeBSD__) |
| 4393 | { |
| 4394 | int len; |
| 4395 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4396 | if (len > 0) { |
| 4397 | buf[len] = 0; |
| 4398 | p = buf; |
| 4399 | } |
| 4400 | } |
| 4401 | #endif |
| 4402 | /* If we don't have any way of figuring out the actual executable |
| 4403 | location then try argv[0]. */ |
| 4404 | if (!p) { |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4405 | p = realpath(argv0, buf); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4406 | if (!p) { |
| 4407 | return NULL; |
| 4408 | } |
| 4409 | } |
| 4410 | dir = dirname(p); |
| 4411 | dir = dirname(dir); |
| 4412 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4413 | max_len = strlen(dir) + |
| 4414 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4415 | res = qemu_mallocz(max_len); |
| 4416 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4417 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4418 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4419 | if (access(res, R_OK)) { |
| 4420 | qemu_free(res); |
| 4421 | res = NULL; |
| 4422 | } |
| 4423 | } |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4424 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4425 | return res; |
| 4426 | } |
| 4427 | #undef SHARE_SUFFIX |
| 4428 | #undef BUILD_SUFFIX |
| 4429 | #endif |
| 4430 | |
| 4431 | char *qemu_find_file(int type, const char *name) |
| 4432 | { |
| 4433 | int len; |
| 4434 | const char *subdir; |
| 4435 | char *buf; |
| 4436 | |
| 4437 | /* If name contains path separators then try it as a straight path. */ |
| 4438 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4439 | && access(name, R_OK) == 0) { |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 4440 | return qemu_strdup(name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4441 | } |
| 4442 | switch (type) { |
| 4443 | case QEMU_FILE_TYPE_BIOS: |
| 4444 | subdir = ""; |
| 4445 | break; |
| 4446 | case QEMU_FILE_TYPE_KEYMAP: |
| 4447 | subdir = "keymaps/"; |
| 4448 | break; |
| 4449 | default: |
| 4450 | abort(); |
| 4451 | } |
| 4452 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4453 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4454 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4455 | if (access(buf, R_OK)) { |
| 4456 | qemu_free(buf); |
| 4457 | return NULL; |
| 4458 | } |
| 4459 | return buf; |
| 4460 | } |
| 4461 | |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 4462 | static int device_help_func(QemuOpts *opts, void *opaque) |
| 4463 | { |
| 4464 | return qdev_device_help(opts); |
| 4465 | } |
| 4466 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4467 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4468 | { |
| 4469 | DeviceState *dev; |
| 4470 | |
| 4471 | dev = qdev_device_add(opts); |
| 4472 | if (!dev) |
| 4473 | return -1; |
| 4474 | return 0; |
| 4475 | } |
| 4476 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 4477 | static int chardev_init_func(QemuOpts *opts, void *opaque) |
| 4478 | { |
| 4479 | CharDriverState *chr; |
| 4480 | |
| 4481 | chr = qemu_chr_open_opts(opts, NULL); |
| 4482 | if (!chr) |
| 4483 | return -1; |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4487 | static int mon_init_func(QemuOpts *opts, void *opaque) |
| 4488 | { |
| 4489 | CharDriverState *chr; |
| 4490 | const char *chardev; |
| 4491 | const char *mode; |
| 4492 | int flags; |
| 4493 | |
| 4494 | mode = qemu_opt_get(opts, "mode"); |
| 4495 | if (mode == NULL) { |
| 4496 | mode = "readline"; |
| 4497 | } |
| 4498 | if (strcmp(mode, "readline") == 0) { |
| 4499 | flags = MONITOR_USE_READLINE; |
| 4500 | } else if (strcmp(mode, "control") == 0) { |
| 4501 | flags = MONITOR_USE_CONTROL; |
| 4502 | } else { |
| 4503 | fprintf(stderr, "unknown monitor mode \"%s\"\n", mode); |
| 4504 | exit(1); |
| 4505 | } |
| 4506 | |
| 4507 | if (qemu_opt_get_bool(opts, "default", 0)) |
| 4508 | flags |= MONITOR_IS_DEFAULT; |
| 4509 | |
| 4510 | chardev = qemu_opt_get(opts, "chardev"); |
| 4511 | chr = qemu_chr_find(chardev); |
| 4512 | if (chr == NULL) { |
| 4513 | fprintf(stderr, "chardev \"%s\" not found\n", chardev); |
| 4514 | exit(1); |
| 4515 | } |
| 4516 | |
| 4517 | monitor_init(chr, flags); |
| 4518 | return 0; |
| 4519 | } |
| 4520 | |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 4521 | static void monitor_parse(const char *optarg, const char *mode) |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4522 | { |
| 4523 | static int monitor_device_index = 0; |
| 4524 | QemuOpts *opts; |
| 4525 | const char *p; |
| 4526 | char label[32]; |
| 4527 | int def = 0; |
| 4528 | |
| 4529 | if (strstart(optarg, "chardev:", &p)) { |
| 4530 | snprintf(label, sizeof(label), "%s", p); |
| 4531 | } else { |
| 4532 | if (monitor_device_index) { |
| 4533 | snprintf(label, sizeof(label), "monitor%d", |
| 4534 | monitor_device_index); |
| 4535 | } else { |
| 4536 | snprintf(label, sizeof(label), "monitor"); |
| 4537 | def = 1; |
| 4538 | } |
| 4539 | opts = qemu_chr_parse_compat(label, optarg); |
| 4540 | if (!opts) { |
| 4541 | fprintf(stderr, "parse error: %s\n", optarg); |
| 4542 | exit(1); |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | opts = qemu_opts_create(&qemu_mon_opts, label, 1); |
| 4547 | if (!opts) { |
| 4548 | fprintf(stderr, "duplicate chardev: %s\n", label); |
| 4549 | exit(1); |
| 4550 | } |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 4551 | qemu_opt_set(opts, "mode", mode); |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4552 | qemu_opt_set(opts, "chardev", label); |
| 4553 | if (def) |
| 4554 | qemu_opt_set(opts, "default", "on"); |
| 4555 | monitor_device_index++; |
| 4556 | } |
| 4557 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4558 | struct device_config { |
| 4559 | enum { |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4560 | DEV_USB, /* -usbdevice */ |
| 4561 | DEV_BT, /* -bt */ |
| 4562 | DEV_SERIAL, /* -serial */ |
| 4563 | DEV_PARALLEL, /* -parallel */ |
| 4564 | DEV_VIRTCON, /* -virtioconsole */ |
H. Peter Anvin | c9f398e | 2009-12-29 13:51:36 -0800 | [diff] [blame] | 4565 | DEV_DEBUGCON, /* -debugcon */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4566 | } type; |
| 4567 | const char *cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4568 | QTAILQ_ENTRY(device_config) next; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4569 | }; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4570 | QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4571 | |
| 4572 | static void add_device_config(int type, const char *cmdline) |
| 4573 | { |
| 4574 | struct device_config *conf; |
| 4575 | |
| 4576 | conf = qemu_mallocz(sizeof(*conf)); |
| 4577 | conf->type = type; |
| 4578 | conf->cmdline = cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4579 | QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4583 | { |
| 4584 | struct device_config *conf; |
| 4585 | int rc; |
| 4586 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4587 | QTAILQ_FOREACH(conf, &device_configs, next) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4588 | if (conf->type != type) |
| 4589 | continue; |
| 4590 | rc = func(conf->cmdline); |
| 4591 | if (0 != rc) |
| 4592 | return rc; |
| 4593 | } |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 4597 | static int serial_parse(const char *devname) |
| 4598 | { |
| 4599 | static int index = 0; |
| 4600 | char label[32]; |
| 4601 | |
| 4602 | if (strcmp(devname, "none") == 0) |
| 4603 | return 0; |
| 4604 | if (index == MAX_SERIAL_PORTS) { |
| 4605 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 4606 | exit(1); |
| 4607 | } |
| 4608 | snprintf(label, sizeof(label), "serial%d", index); |
| 4609 | serial_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4610 | if (!serial_hds[index]) { |
| 4611 | fprintf(stderr, "qemu: could not open serial device '%s': %s\n", |
| 4612 | devname, strerror(errno)); |
| 4613 | return -1; |
| 4614 | } |
| 4615 | index++; |
| 4616 | return 0; |
| 4617 | } |
| 4618 | |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 4619 | static int parallel_parse(const char *devname) |
| 4620 | { |
| 4621 | static int index = 0; |
| 4622 | char label[32]; |
| 4623 | |
| 4624 | if (strcmp(devname, "none") == 0) |
| 4625 | return 0; |
| 4626 | if (index == MAX_PARALLEL_PORTS) { |
| 4627 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 4628 | exit(1); |
| 4629 | } |
| 4630 | snprintf(label, sizeof(label), "parallel%d", index); |
| 4631 | parallel_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4632 | if (!parallel_hds[index]) { |
| 4633 | fprintf(stderr, "qemu: could not open parallel device '%s': %s\n", |
| 4634 | devname, strerror(errno)); |
| 4635 | return -1; |
| 4636 | } |
| 4637 | index++; |
| 4638 | return 0; |
| 4639 | } |
| 4640 | |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4641 | static int virtcon_parse(const char *devname) |
| 4642 | { |
| 4643 | static int index = 0; |
| 4644 | char label[32]; |
Amit Shah | 392ecf5 | 2010-01-21 16:19:23 +0530 | [diff] [blame] | 4645 | QemuOpts *bus_opts, *dev_opts; |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4646 | |
| 4647 | if (strcmp(devname, "none") == 0) |
| 4648 | return 0; |
| 4649 | if (index == MAX_VIRTIO_CONSOLES) { |
| 4650 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 4651 | exit(1); |
| 4652 | } |
Amit Shah | 392ecf5 | 2010-01-21 16:19:23 +0530 | [diff] [blame] | 4653 | |
| 4654 | bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4655 | qemu_opt_set(bus_opts, "driver", "virtio-serial"); |
| 4656 | |
| 4657 | dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4658 | qemu_opt_set(dev_opts, "driver", "virtconsole"); |
| 4659 | |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4660 | snprintf(label, sizeof(label), "virtcon%d", index); |
| 4661 | virtcon_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4662 | if (!virtcon_hds[index]) { |
| 4663 | fprintf(stderr, "qemu: could not open virtio console '%s': %s\n", |
| 4664 | devname, strerror(errno)); |
| 4665 | return -1; |
| 4666 | } |
Amit Shah | 392ecf5 | 2010-01-21 16:19:23 +0530 | [diff] [blame] | 4667 | qemu_opt_set(dev_opts, "chardev", label); |
| 4668 | |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4669 | index++; |
| 4670 | return 0; |
| 4671 | } |
| 4672 | |
H. Peter Anvin | c9f398e | 2009-12-29 13:51:36 -0800 | [diff] [blame] | 4673 | static int debugcon_parse(const char *devname) |
| 4674 | { |
| 4675 | QemuOpts *opts; |
| 4676 | |
| 4677 | if (!qemu_chr_open("debugcon", devname, NULL)) { |
| 4678 | exit(1); |
| 4679 | } |
| 4680 | opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1); |
| 4681 | if (!opts) { |
| 4682 | fprintf(stderr, "qemu: already have a debugcon device\n"); |
| 4683 | exit(1); |
| 4684 | } |
| 4685 | qemu_opt_set(opts, "driver", "isa-debugcon"); |
| 4686 | qemu_opt_set(opts, "chardev", "debugcon"); |
| 4687 | return 0; |
| 4688 | } |
| 4689 | |
Anthony Liguori | 6530a97 | 2010-01-22 09:18:06 -0600 | [diff] [blame] | 4690 | static const QEMUOption *lookup_opt(int argc, char **argv, |
| 4691 | const char **poptarg, int *poptind) |
| 4692 | { |
| 4693 | const QEMUOption *popt; |
| 4694 | int optind = *poptind; |
| 4695 | char *r = argv[optind]; |
| 4696 | const char *optarg; |
| 4697 | |
| 4698 | optind++; |
| 4699 | /* Treat --foo the same as -foo. */ |
| 4700 | if (r[1] == '-') |
| 4701 | r++; |
| 4702 | popt = qemu_options; |
| 4703 | for(;;) { |
| 4704 | if (!popt->name) { |
| 4705 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 4706 | argv[0], r); |
| 4707 | exit(1); |
| 4708 | } |
| 4709 | if (!strcmp(popt->name, r + 1)) |
| 4710 | break; |
| 4711 | popt++; |
| 4712 | } |
| 4713 | if (popt->flags & HAS_ARG) { |
| 4714 | if (optind >= argc) { |
| 4715 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4716 | argv[0], r); |
| 4717 | exit(1); |
| 4718 | } |
| 4719 | optarg = argv[optind++]; |
| 4720 | } else { |
| 4721 | optarg = NULL; |
| 4722 | } |
| 4723 | |
| 4724 | *poptarg = optarg; |
| 4725 | *poptind = optind; |
| 4726 | |
| 4727 | return popt; |
| 4728 | } |
| 4729 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4730 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4731 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4732 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4733 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4734 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4735 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4736 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4737 | const char *kernel_filename, *kernel_cmdline; |
Anthony Liguori | 195325a | 2009-10-30 12:42:29 -0500 | [diff] [blame] | 4738 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4739 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4740 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4741 | int cyls, heads, secs, translation; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4742 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4743 | int optind; |
Anthony Liguori | 6530a97 | 2010-01-22 09:18:06 -0600 | [diff] [blame] | 4744 | const char *optarg; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4745 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4746 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4747 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4748 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4749 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4750 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4751 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4752 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4753 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4754 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4755 | int fd = 0; |
| 4756 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4757 | const char *chroot_dir = NULL; |
| 4758 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4759 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4760 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4761 | int show_vnc_port = 0; |
Anthony Liguori | 292444c | 2010-01-21 10:57:58 -0600 | [diff] [blame] | 4762 | int defconfig = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4763 | |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 4764 | init_clocks(); |
| 4765 | |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 4766 | qemu_errors_to_file(stderr); |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4767 | qemu_cache_utils_init(envp); |
| 4768 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4769 | QLIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4770 | #ifndef _WIN32 |
| 4771 | { |
| 4772 | struct sigaction act; |
| 4773 | sigfillset(&act.sa_mask); |
| 4774 | act.sa_flags = 0; |
| 4775 | act.sa_handler = SIG_IGN; |
| 4776 | sigaction(SIGPIPE, &act, NULL); |
| 4777 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4778 | #else |
| 4779 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4780 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4781 | QEMU to run on a single CPU */ |
| 4782 | { |
| 4783 | HANDLE h; |
| 4784 | DWORD mask, smask; |
| 4785 | int i; |
| 4786 | h = GetCurrentProcess(); |
| 4787 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4788 | for(i = 0; i < 32; i++) { |
| 4789 | if (mask & (1 << i)) |
| 4790 | break; |
| 4791 | } |
| 4792 | if (i != 32) { |
| 4793 | mask = 1 << i; |
| 4794 | SetProcessAffinityMask(h, mask); |
| 4795 | } |
| 4796 | } |
| 4797 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4798 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4799 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4800 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4801 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4802 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4803 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4804 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4805 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4806 | kernel_filename = NULL; |
| 4807 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4808 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4809 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4810 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4811 | for (i = 0; i < MAX_NODES; i++) { |
| 4812 | node_mem[i] = 0; |
| 4813 | node_cpumask[i] = 0; |
| 4814 | } |
| 4815 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4816 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4817 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4818 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4819 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4820 | autostart= 1; |
| 4821 | |
Anthony Liguori | 292444c | 2010-01-21 10:57:58 -0600 | [diff] [blame] | 4822 | /* first pass of option parsing */ |
| 4823 | optind = 1; |
| 4824 | while (optind < argc) { |
| 4825 | if (argv[optind][0] != '-') { |
| 4826 | /* disk image */ |
Anthony Liguori | 28e68d6 | 2010-01-27 10:46:00 -0600 | [diff] [blame] | 4827 | optind++; |
Anthony Liguori | 292444c | 2010-01-21 10:57:58 -0600 | [diff] [blame] | 4828 | continue; |
| 4829 | } else { |
| 4830 | const QEMUOption *popt; |
| 4831 | |
| 4832 | popt = lookup_opt(argc, argv, &optarg, &optind); |
| 4833 | switch (popt->index) { |
| 4834 | case QEMU_OPTION_nodefconfig: |
| 4835 | defconfig=0; |
| 4836 | break; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | if (defconfig) { |
| 4842 | FILE *fp; |
| 4843 | fp = fopen(CONFIG_QEMU_CONFDIR "/qemu.conf", "r"); |
| 4844 | if (fp) { |
| 4845 | if (qemu_config_parse(fp) != 0) { |
| 4846 | exit(1); |
| 4847 | } |
| 4848 | fclose(fp); |
| 4849 | } |
| 4850 | |
| 4851 | fp = fopen(CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", "r"); |
| 4852 | if (fp) { |
| 4853 | if (qemu_config_parse(fp) != 0) { |
| 4854 | exit(1); |
| 4855 | } |
| 4856 | fclose(fp); |
| 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | /* second pass of option parsing */ |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4861 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4862 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4863 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4864 | break; |
Anthony Liguori | 6530a97 | 2010-01-22 09:18:06 -0600 | [diff] [blame] | 4865 | if (argv[optind][0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4866 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4867 | } else { |
| 4868 | const QEMUOption *popt; |
| 4869 | |
Anthony Liguori | 6530a97 | 2010-01-22 09:18:06 -0600 | [diff] [blame] | 4870 | popt = lookup_opt(argc, argv, &optarg, &optind); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4871 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4872 | case QEMU_OPTION_M: |
| 4873 | machine = find_machine(optarg); |
| 4874 | if (!machine) { |
| 4875 | QEMUMachine *m; |
| 4876 | printf("Supported machines are:\n"); |
| 4877 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4878 | if (m->alias) |
| 4879 | printf("%-10s %s (alias of %s)\n", |
| 4880 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4881 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4882 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4883 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4884 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4885 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4886 | } |
| 4887 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4888 | case QEMU_OPTION_cpu: |
| 4889 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4890 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 4891 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 4892 | #if defined(cpu_list) |
| 4893 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4894 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4895 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4896 | } else { |
| 4897 | cpu_model = optarg; |
| 4898 | } |
| 4899 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4900 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4901 | initrd_filename = optarg; |
| 4902 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4903 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4904 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4905 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4906 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4907 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4908 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4909 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4910 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 4911 | ",trans=lba" : |
| 4912 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 4913 | ",trans=none" : ""); |
| 4914 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4915 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4916 | case QEMU_OPTION_hdc: |
| 4917 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4918 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4919 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4920 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4921 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4922 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 4923 | case QEMU_OPTION_set: |
| 4924 | if (qemu_set_option(optarg) != 0) |
| 4925 | exit(1); |
| 4926 | break; |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 4927 | case QEMU_OPTION_global: |
| 4928 | if (qemu_global_option(optarg) != 0) |
| 4929 | exit(1); |
| 4930 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4931 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4932 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4933 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4934 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4935 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4936 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4937 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4938 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4939 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4940 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4941 | snapshot = 1; |
| 4942 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4943 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4944 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4945 | const char *p; |
| 4946 | p = optarg; |
| 4947 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4948 | if (cyls < 1 || cyls > 16383) |
| 4949 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4950 | if (*p != ',') |
| 4951 | goto chs_fail; |
| 4952 | p++; |
| 4953 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4954 | if (heads < 1 || heads > 16) |
| 4955 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4956 | if (*p != ',') |
| 4957 | goto chs_fail; |
| 4958 | p++; |
| 4959 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4960 | if (secs < 1 || secs > 63) |
| 4961 | goto chs_fail; |
| 4962 | if (*p == ',') { |
| 4963 | p++; |
| 4964 | if (!strcmp(p, "none")) |
| 4965 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 4966 | else if (!strcmp(p, "lba")) |
| 4967 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 4968 | else if (!strcmp(p, "auto")) |
| 4969 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 4970 | else |
| 4971 | goto chs_fail; |
| 4972 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4973 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4974 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 4975 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4976 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4977 | if (hda_opts != NULL) { |
| 4978 | char num[16]; |
| 4979 | snprintf(num, sizeof(num), "%d", cyls); |
| 4980 | qemu_opt_set(hda_opts, "cyls", num); |
| 4981 | snprintf(num, sizeof(num), "%d", heads); |
| 4982 | qemu_opt_set(hda_opts, "heads", num); |
| 4983 | snprintf(num, sizeof(num), "%d", secs); |
| 4984 | qemu_opt_set(hda_opts, "secs", num); |
| 4985 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 4986 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 4987 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 4988 | qemu_opt_set(hda_opts, "trans", "none"); |
| 4989 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4990 | } |
| 4991 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4992 | case QEMU_OPTION_numa: |
| 4993 | if (nb_numa_nodes >= MAX_NODES) { |
| 4994 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 4995 | exit(1); |
| 4996 | } |
| 4997 | numa_add(optarg); |
| 4998 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4999 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5000 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5001 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5002 | #ifdef CONFIG_CURSES |
| 5003 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5004 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5005 | break; |
| 5006 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5007 | case QEMU_OPTION_portrait: |
| 5008 | graphic_rotate = 1; |
| 5009 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5010 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5011 | kernel_filename = optarg; |
| 5012 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5013 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5014 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5015 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5016 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5017 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5018 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5019 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5020 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5021 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5022 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5023 | }; |
| 5024 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5025 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5026 | int legacy = 0; |
| 5027 | |
| 5028 | if (!strchr(optarg, '=')) { |
| 5029 | legacy = 1; |
| 5030 | pstrcpy(buf, sizeof(buf), optarg); |
| 5031 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5032 | fprintf(stderr, |
| 5033 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5034 | buf, optarg); |
| 5035 | exit(1); |
| 5036 | } |
| 5037 | |
| 5038 | if (legacy || |
| 5039 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5040 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5041 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5042 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5043 | if (!legacy) { |
| 5044 | if (get_param_value(buf, sizeof(buf), |
| 5045 | "once", optarg)) { |
| 5046 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5047 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5048 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5049 | qemu_register_reset(restore_boot_devices, |
| 5050 | standard_boot_devices); |
| 5051 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5052 | if (get_param_value(buf, sizeof(buf), |
| 5053 | "menu", optarg)) { |
| 5054 | if (!strcmp(buf, "on")) { |
| 5055 | boot_menu = 1; |
| 5056 | } else if (!strcmp(buf, "off")) { |
| 5057 | boot_menu = 0; |
| 5058 | } else { |
| 5059 | fprintf(stderr, |
| 5060 | "qemu: invalid option value '%s'\n", |
| 5061 | buf); |
| 5062 | exit(1); |
| 5063 | } |
| 5064 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5065 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5066 | } |
| 5067 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5068 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5069 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5070 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5071 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5072 | #ifdef TARGET_I386 |
| 5073 | case QEMU_OPTION_no_fd_bootchk: |
| 5074 | fd_bootchk = 0; |
| 5075 | break; |
| 5076 | #endif |
Mark McLoughlin | a1ea458 | 2009-10-08 19:58:26 +0100 | [diff] [blame] | 5077 | case QEMU_OPTION_netdev: |
| 5078 | if (net_client_parse(&qemu_netdev_opts, optarg) == -1) { |
| 5079 | exit(1); |
| 5080 | } |
| 5081 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5082 | case QEMU_OPTION_net: |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 5083 | if (net_client_parse(&qemu_net_opts, optarg) == -1) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5084 | exit(1); |
| 5085 | } |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5086 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5087 | #ifdef CONFIG_SLIRP |
| 5088 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5089 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5090 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5091 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5092 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5093 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5094 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5095 | case QEMU_OPTION_smb: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5096 | if (net_slirp_smb(optarg) < 0) |
| 5097 | exit(1); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5098 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5099 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5100 | case QEMU_OPTION_redir: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5101 | if (net_slirp_redir(optarg) < 0) |
| 5102 | exit(1); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5103 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5104 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5105 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5106 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5107 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5108 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5109 | case QEMU_OPTION_audio_help: |
| 5110 | AUD_help (); |
| 5111 | exit (0); |
| 5112 | break; |
| 5113 | case QEMU_OPTION_soundhw: |
| 5114 | select_soundhw (optarg); |
| 5115 | break; |
| 5116 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5117 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5118 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5119 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5120 | case QEMU_OPTION_version: |
| 5121 | version(); |
| 5122 | exit(0); |
| 5123 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5124 | case QEMU_OPTION_m: { |
| 5125 | uint64_t value; |
| 5126 | char *ptr; |
| 5127 | |
| 5128 | value = strtoul(optarg, &ptr, 10); |
| 5129 | switch (*ptr) { |
| 5130 | case 0: case 'M': case 'm': |
| 5131 | value <<= 20; |
| 5132 | break; |
| 5133 | case 'G': case 'g': |
| 5134 | value <<= 30; |
| 5135 | break; |
| 5136 | default: |
| 5137 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5138 | exit(1); |
| 5139 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5140 | |
| 5141 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5142 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5143 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5144 | exit(1); |
| 5145 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5146 | if (value != (uint64_t)(ram_addr_t)value) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5147 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5148 | exit(1); |
| 5149 | } |
| 5150 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5151 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5152 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5153 | case QEMU_OPTION_d: |
| 5154 | { |
| 5155 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5156 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5157 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5158 | mask = cpu_str_to_log_mask(optarg); |
| 5159 | if (!mask) { |
| 5160 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5161 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5162 | printf("%-10s %s\n", item->name, item->help); |
| 5163 | } |
| 5164 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5165 | } |
| 5166 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5167 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5168 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5169 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5170 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5171 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5172 | case QEMU_OPTION_gdb: |
| 5173 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5174 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5175 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5176 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5177 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5178 | case QEMU_OPTION_bios: |
| 5179 | bios_name = optarg; |
| 5180 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5181 | case QEMU_OPTION_singlestep: |
| 5182 | singlestep = 1; |
| 5183 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5184 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5185 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5186 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5187 | case QEMU_OPTION_k: |
| 5188 | keyboard_layout = optarg; |
| 5189 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5190 | case QEMU_OPTION_localtime: |
| 5191 | rtc_utc = 0; |
| 5192 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5193 | case QEMU_OPTION_vga: |
| 5194 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5195 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5196 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5197 | case QEMU_OPTION_g: |
| 5198 | { |
| 5199 | const char *p; |
| 5200 | int w, h, depth; |
| 5201 | p = optarg; |
| 5202 | w = strtol(p, (char **)&p, 10); |
| 5203 | if (w <= 0) { |
| 5204 | graphic_error: |
| 5205 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5206 | exit(1); |
| 5207 | } |
| 5208 | if (*p != 'x') |
| 5209 | goto graphic_error; |
| 5210 | p++; |
| 5211 | h = strtol(p, (char **)&p, 10); |
| 5212 | if (h <= 0) |
| 5213 | goto graphic_error; |
| 5214 | if (*p == 'x') { |
| 5215 | p++; |
| 5216 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5217 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5218 | depth != 24 && depth != 32) |
| 5219 | goto graphic_error; |
| 5220 | } else if (*p == '\0') { |
| 5221 | depth = graphic_depth; |
| 5222 | } else { |
| 5223 | goto graphic_error; |
| 5224 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5225 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5226 | graphic_width = w; |
| 5227 | graphic_height = h; |
| 5228 | graphic_depth = depth; |
| 5229 | } |
| 5230 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5231 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5232 | case QEMU_OPTION_echr: |
| 5233 | { |
| 5234 | char *r; |
| 5235 | term_escape_char = strtol(optarg, &r, 0); |
| 5236 | if (r == optarg) |
| 5237 | printf("Bad argument to echr\n"); |
| 5238 | break; |
| 5239 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5240 | case QEMU_OPTION_monitor: |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5241 | monitor_parse(optarg, "readline"); |
| 5242 | default_monitor = 0; |
| 5243 | break; |
| 5244 | case QEMU_OPTION_qmp: |
| 5245 | monitor_parse(optarg, "control"); |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5246 | default_monitor = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5247 | break; |
Gerd Hoffmann | 22a0e04 | 2009-12-08 13:11:51 +0100 | [diff] [blame] | 5248 | case QEMU_OPTION_mon: |
| 5249 | opts = qemu_opts_parse(&qemu_mon_opts, optarg, "chardev"); |
| 5250 | if (!opts) { |
| 5251 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5252 | exit(1); |
| 5253 | } |
| 5254 | default_monitor = 0; |
| 5255 | break; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5256 | case QEMU_OPTION_chardev: |
| 5257 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend"); |
| 5258 | if (!opts) { |
| 5259 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5260 | exit(1); |
| 5261 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5262 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5263 | case QEMU_OPTION_serial: |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5264 | add_device_config(DEV_SERIAL, optarg); |
| 5265 | default_serial = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5266 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5267 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5268 | if (watchdog) { |
| 5269 | fprintf(stderr, |
| 5270 | "qemu: only one watchdog option may be given\n"); |
| 5271 | return 1; |
| 5272 | } |
| 5273 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5274 | break; |
| 5275 | case QEMU_OPTION_watchdog_action: |
| 5276 | if (select_watchdog_action(optarg) == -1) { |
| 5277 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5278 | exit(1); |
| 5279 | } |
| 5280 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5281 | case QEMU_OPTION_virtiocon: |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5282 | add_device_config(DEV_VIRTCON, optarg); |
| 5283 | default_virtcon = 0; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5284 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5285 | case QEMU_OPTION_parallel: |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5286 | add_device_config(DEV_PARALLEL, optarg); |
| 5287 | default_parallel = 0; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5288 | break; |
H. Peter Anvin | c9f398e | 2009-12-29 13:51:36 -0800 | [diff] [blame] | 5289 | case QEMU_OPTION_debugcon: |
| 5290 | add_device_config(DEV_DEBUGCON, optarg); |
| 5291 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5292 | case QEMU_OPTION_loadvm: |
| 5293 | loadvm = optarg; |
| 5294 | break; |
| 5295 | case QEMU_OPTION_full_screen: |
| 5296 | full_screen = 1; |
| 5297 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5298 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5299 | case QEMU_OPTION_no_frame: |
| 5300 | no_frame = 1; |
| 5301 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5302 | case QEMU_OPTION_alt_grab: |
| 5303 | alt_grab = 1; |
| 5304 | break; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 5305 | case QEMU_OPTION_ctrl_grab: |
| 5306 | ctrl_grab = 1; |
| 5307 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5308 | case QEMU_OPTION_no_quit: |
| 5309 | no_quit = 1; |
| 5310 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5311 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5312 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5313 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5314 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5315 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5316 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5317 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5318 | #ifdef TARGET_I386 |
| 5319 | case QEMU_OPTION_win2k_hack: |
| 5320 | win2k_install_hack = 1; |
| 5321 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5322 | case QEMU_OPTION_rtc_td_hack: |
| 5323 | rtc_td_hack = 1; |
| 5324 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5325 | case QEMU_OPTION_acpitable: |
| 5326 | if(acpi_table_add(optarg) < 0) { |
| 5327 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5328 | exit(1); |
| 5329 | } |
| 5330 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5331 | case QEMU_OPTION_smbios: |
| 5332 | if(smbios_entry_add(optarg) < 0) { |
| 5333 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5334 | exit(1); |
| 5335 | } |
| 5336 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5337 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5338 | #ifdef CONFIG_KVM |
| 5339 | case QEMU_OPTION_enable_kvm: |
| 5340 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5341 | break; |
| 5342 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5343 | case QEMU_OPTION_usb: |
| 5344 | usb_enabled = 1; |
| 5345 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5346 | case QEMU_OPTION_usbdevice: |
| 5347 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5348 | add_device_config(DEV_USB, optarg); |
| 5349 | break; |
| 5350 | case QEMU_OPTION_device: |
Mark McLoughlin | b386bec | 2009-10-06 12:17:01 +0100 | [diff] [blame] | 5351 | if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) { |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5352 | exit(1); |
| 5353 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5354 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5355 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5356 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5357 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5358 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5359 | exit(1); |
| 5360 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5361 | if (max_cpus < smp_cpus) { |
| 5362 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5363 | "smp\n"); |
| 5364 | exit(1); |
| 5365 | } |
| 5366 | if (max_cpus > 255) { |
| 5367 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5368 | exit(1); |
| 5369 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5370 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5371 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5372 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5373 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5374 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5375 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5376 | case QEMU_OPTION_no_acpi: |
| 5377 | acpi_enabled = 0; |
| 5378 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5379 | case QEMU_OPTION_no_hpet: |
| 5380 | no_hpet = 1; |
| 5381 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5382 | case QEMU_OPTION_balloon: |
| 5383 | if (balloon_parse(optarg) < 0) { |
| 5384 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5385 | exit(1); |
| 5386 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5387 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5388 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5389 | case QEMU_OPTION_no_reboot: |
| 5390 | no_reboot = 1; |
| 5391 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5392 | case QEMU_OPTION_no_shutdown: |
| 5393 | no_shutdown = 1; |
| 5394 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5395 | case QEMU_OPTION_show_cursor: |
| 5396 | cursor_hide = 0; |
| 5397 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5398 | case QEMU_OPTION_uuid: |
| 5399 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5400 | fprintf(stderr, "Fail to parse UUID string." |
| 5401 | " Wrong format.\n"); |
| 5402 | exit(1); |
| 5403 | } |
| 5404 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5405 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5406 | case QEMU_OPTION_daemonize: |
| 5407 | daemonize = 1; |
| 5408 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5409 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5410 | case QEMU_OPTION_option_rom: |
| 5411 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5412 | fprintf(stderr, "Too many option ROMs\n"); |
| 5413 | exit(1); |
| 5414 | } |
| 5415 | option_rom[nb_option_roms] = optarg; |
| 5416 | nb_option_roms++; |
| 5417 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5418 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5419 | case QEMU_OPTION_semihosting: |
| 5420 | semihosting_enabled = 1; |
| 5421 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5422 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5423 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5424 | qemu_name = qemu_strdup(optarg); |
| 5425 | { |
| 5426 | char *p = strchr(qemu_name, ','); |
| 5427 | if (p != NULL) { |
| 5428 | *p++ = 0; |
| 5429 | if (strncmp(p, "process=", 8)) { |
| 5430 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5431 | exit(1); |
| 5432 | } |
| 5433 | p += 8; |
| 5434 | set_proc_name(p); |
| 5435 | } |
| 5436 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5437 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5438 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5439 | case QEMU_OPTION_prom_env: |
| 5440 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5441 | fprintf(stderr, "Too many prom variables\n"); |
| 5442 | exit(1); |
| 5443 | } |
| 5444 | prom_envs[nb_prom_envs] = optarg; |
| 5445 | nb_prom_envs++; |
| 5446 | break; |
| 5447 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5448 | #ifdef TARGET_ARM |
| 5449 | case QEMU_OPTION_old_param: |
| 5450 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5451 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5452 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5453 | case QEMU_OPTION_clock: |
| 5454 | configure_alarms(optarg); |
| 5455 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5456 | case QEMU_OPTION_startdate: |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5457 | configure_rtc_date_offset(optarg, 1); |
| 5458 | break; |
| 5459 | case QEMU_OPTION_rtc: |
| 5460 | opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL); |
| 5461 | if (!opts) { |
| 5462 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5463 | exit(1); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5464 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5465 | configure_rtc(opts); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5466 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5467 | case QEMU_OPTION_tb_size: |
| 5468 | tb_size = strtol(optarg, NULL, 0); |
| 5469 | if (tb_size < 0) |
| 5470 | tb_size = 0; |
| 5471 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5472 | case QEMU_OPTION_icount: |
| 5473 | use_icount = 1; |
| 5474 | if (strcmp(optarg, "auto") == 0) { |
| 5475 | icount_time_shift = -1; |
| 5476 | } else { |
| 5477 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5478 | } |
| 5479 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5480 | case QEMU_OPTION_incoming: |
| 5481 | incoming = optarg; |
| 5482 | break; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5483 | case QEMU_OPTION_nodefaults: |
| 5484 | default_serial = 0; |
| 5485 | default_parallel = 0; |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5486 | default_virtcon = 0; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5487 | default_monitor = 0; |
| 5488 | default_vga = 0; |
Gerd Hoffmann | cb4522c | 2009-12-08 13:11:47 +0100 | [diff] [blame] | 5489 | default_net = 0; |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5490 | default_floppy = 0; |
| 5491 | default_cdrom = 0; |
| 5492 | default_sdcard = 0; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5493 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5494 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5495 | case QEMU_OPTION_chroot: |
| 5496 | chroot_dir = optarg; |
| 5497 | break; |
| 5498 | case QEMU_OPTION_runas: |
| 5499 | run_as = optarg; |
| 5500 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5501 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5502 | #ifdef CONFIG_XEN |
| 5503 | case QEMU_OPTION_xen_domid: |
| 5504 | xen_domid = atoi(optarg); |
| 5505 | break; |
| 5506 | case QEMU_OPTION_xen_create: |
| 5507 | xen_mode = XEN_CREATE; |
| 5508 | break; |
| 5509 | case QEMU_OPTION_xen_attach: |
| 5510 | xen_mode = XEN_ATTACH; |
| 5511 | break; |
| 5512 | #endif |
Gerd Hoffmann | 715a664 | 2009-10-14 10:39:28 +0200 | [diff] [blame] | 5513 | case QEMU_OPTION_readconfig: |
| 5514 | { |
| 5515 | FILE *fp; |
| 5516 | fp = fopen(optarg, "r"); |
| 5517 | if (fp == NULL) { |
| 5518 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5519 | exit(1); |
| 5520 | } |
| 5521 | if (qemu_config_parse(fp) != 0) { |
| 5522 | exit(1); |
| 5523 | } |
| 5524 | fclose(fp); |
| 5525 | break; |
| 5526 | } |
| 5527 | case QEMU_OPTION_writeconfig: |
| 5528 | { |
| 5529 | FILE *fp; |
| 5530 | if (strcmp(optarg, "-") == 0) { |
| 5531 | fp = stdout; |
| 5532 | } else { |
| 5533 | fp = fopen(optarg, "w"); |
| 5534 | if (fp == NULL) { |
| 5535 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5536 | exit(1); |
| 5537 | } |
| 5538 | } |
| 5539 | qemu_config_write(fp); |
| 5540 | fclose(fp); |
| 5541 | break; |
| 5542 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5543 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5544 | } |
| 5545 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5546 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5547 | /* If no data_dir is specified then try to find it relative to the |
| 5548 | executable path. */ |
| 5549 | if (!data_dir) { |
| 5550 | data_dir = find_datadir(argv[0]); |
| 5551 | } |
| 5552 | /* If all else fails use the install patch specified when building. */ |
| 5553 | if (!data_dir) { |
| 5554 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5555 | } |
| 5556 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5557 | /* |
| 5558 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5559 | * specify a max_cpus value. |
| 5560 | */ |
| 5561 | if (!max_cpus) |
| 5562 | max_cpus = smp_cpus; |
| 5563 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5564 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5565 | if (smp_cpus > machine->max_cpus) { |
| 5566 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5567 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5568 | machine->max_cpus); |
| 5569 | exit(1); |
| 5570 | } |
| 5571 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5572 | qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0); |
Gerd Hoffmann | d8bcbab | 2009-12-16 14:25:40 +0100 | [diff] [blame] | 5573 | qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0); |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5574 | |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5575 | if (machine->no_serial) { |
| 5576 | default_serial = 0; |
| 5577 | } |
| 5578 | if (machine->no_parallel) { |
| 5579 | default_parallel = 0; |
| 5580 | } |
| 5581 | if (!machine->use_virtcon) { |
| 5582 | default_virtcon = 0; |
| 5583 | } |
| 5584 | if (machine->no_vga) { |
| 5585 | default_vga = 0; |
| 5586 | } |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5587 | if (machine->no_floppy) { |
| 5588 | default_floppy = 0; |
| 5589 | } |
| 5590 | if (machine->no_cdrom) { |
| 5591 | default_cdrom = 0; |
| 5592 | } |
| 5593 | if (machine->no_sdcard) { |
| 5594 | default_sdcard = 0; |
| 5595 | } |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5596 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5597 | if (display_type == DT_NOGRAPHIC) { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5598 | if (default_parallel) |
| 5599 | add_device_config(DEV_PARALLEL, "null"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5600 | if (default_serial && default_monitor) { |
| 5601 | add_device_config(DEV_SERIAL, "mon:stdio"); |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5602 | } else if (default_virtcon && default_monitor) { |
| 5603 | add_device_config(DEV_VIRTCON, "mon:stdio"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5604 | } else { |
| 5605 | if (default_serial) |
| 5606 | add_device_config(DEV_SERIAL, "stdio"); |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5607 | if (default_virtcon) |
| 5608 | add_device_config(DEV_VIRTCON, "stdio"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5609 | if (default_monitor) |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5610 | monitor_parse("stdio", "readline"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5611 | } |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5612 | } else { |
| 5613 | if (default_serial) |
| 5614 | add_device_config(DEV_SERIAL, "vc:80Cx24C"); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5615 | if (default_parallel) |
| 5616 | add_device_config(DEV_PARALLEL, "vc:80Cx24C"); |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5617 | if (default_monitor) |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5618 | monitor_parse("vc:80Cx24C", "readline"); |
Alexander Graf | 38536da | 2009-12-17 13:06:08 +0100 | [diff] [blame] | 5619 | if (default_virtcon) |
| 5620 | add_device_config(DEV_VIRTCON, "vc:80Cx24C"); |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5621 | } |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 5622 | if (default_vga) |
| 5623 | vga_interface_type = VGA_CIRRUS; |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5624 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 5625 | if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0) |
| 5626 | exit(1); |
| 5627 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5628 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5629 | if (daemonize) { |
| 5630 | pid_t pid; |
| 5631 | |
| 5632 | if (pipe(fds) == -1) |
| 5633 | exit(1); |
| 5634 | |
| 5635 | pid = fork(); |
| 5636 | if (pid > 0) { |
| 5637 | uint8_t status; |
| 5638 | ssize_t len; |
| 5639 | |
| 5640 | close(fds[1]); |
| 5641 | |
| 5642 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5643 | len = read(fds[0], &status, 1); |
| 5644 | if (len == -1 && (errno == EINTR)) |
| 5645 | goto again; |
| 5646 | |
| 5647 | if (len != 1) |
| 5648 | exit(1); |
| 5649 | else if (status == 1) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5650 | fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5651 | exit(1); |
| 5652 | } else |
| 5653 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5654 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5655 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5656 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 5657 | close(fds[0]); |
| 5658 | qemu_set_cloexec(fds[1]); |
| 5659 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5660 | setsid(); |
| 5661 | |
| 5662 | pid = fork(); |
| 5663 | if (pid > 0) |
| 5664 | exit(0); |
| 5665 | else if (pid < 0) |
| 5666 | exit(1); |
| 5667 | |
| 5668 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5669 | |
| 5670 | signal(SIGTSTP, SIG_IGN); |
| 5671 | signal(SIGTTOU, SIG_IGN); |
| 5672 | signal(SIGTTIN, SIG_IGN); |
| 5673 | } |
Juha Riihimäki | 099fe23 | 2009-12-03 15:56:03 +0200 | [diff] [blame] | 5674 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5675 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5676 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
Juha Riihimäki | 099fe23 | 2009-12-03 15:56:03 +0200 | [diff] [blame] | 5677 | #ifndef _WIN32 |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5678 | if (daemonize) { |
| 5679 | uint8_t status = 1; |
Kirill A. Shutemov | dc330e2 | 2010-01-20 00:56:18 +0100 | [diff] [blame] | 5680 | if (write(fds[1], &status, 1) != 1) { |
| 5681 | perror("daemonize. Writing to pipe\n"); |
| 5682 | } |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5683 | } else |
Juha Riihimäki | 099fe23 | 2009-12-03 15:56:03 +0200 | [diff] [blame] | 5684 | #endif |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5685 | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5686 | exit(1); |
| 5687 | } |
| 5688 | |
Marcelo Tosatti | 214910a | 2009-09-18 02:41:23 -0300 | [diff] [blame] | 5689 | if (kvm_enabled()) { |
| 5690 | int ret; |
| 5691 | |
| 5692 | ret = kvm_init(smp_cpus); |
| 5693 | if (ret < 0) { |
| 5694 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5695 | exit(1); |
| 5696 | } |
| 5697 | } |
| 5698 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5699 | if (qemu_init_main_loop()) { |
| 5700 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5701 | exit(1); |
| 5702 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5703 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5704 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5705 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5706 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5707 | exit(1); |
| 5708 | } |
| 5709 | |
| 5710 | if (!linux_boot && initrd_filename != NULL) { |
| 5711 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5712 | exit(1); |
| 5713 | } |
| 5714 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5715 | #ifndef _WIN32 |
| 5716 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5717 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5718 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5719 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5720 | if (init_timer_alarm() < 0) { |
| 5721 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5722 | exit(1); |
| 5723 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5724 | if (use_icount && icount_time_shift < 0) { |
| 5725 | use_icount = 2; |
| 5726 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5727 | It will be corrected fairly quickly anyway. */ |
| 5728 | icount_time_shift = 3; |
| 5729 | init_icount_adjust(); |
| 5730 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5731 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5732 | #ifdef _WIN32 |
| 5733 | socket_init(); |
| 5734 | #endif |
| 5735 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 5736 | if (net_init_clients() < 0) { |
| 5737 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5738 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5739 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5740 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5741 | net_set_boot_mask(net_boot); |
| 5742 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5743 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5744 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5745 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5746 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5747 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5748 | if (ram_size == 0) |
| 5749 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5750 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5751 | /* init the dynamic translator */ |
| 5752 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5753 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 5754 | bdrv_init_with_whitelist(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5755 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5756 | blk_mig_init(); |
| 5757 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5758 | if (default_cdrom) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5759 | /* we always create the cdrom drive, even if no disk is there */ |
| 5760 | drive_add(NULL, CDROM_ALIAS); |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5761 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5762 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5763 | if (default_floppy) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5764 | /* we always create at least one floppy */ |
| 5765 | drive_add(NULL, FD_ALIAS, 0); |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5766 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5767 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5768 | if (default_sdcard) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5769 | /* we always create one sd slot, even if no card is in it */ |
| 5770 | drive_add(NULL, SD_ALIAS); |
| 5771 | } |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5772 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5773 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5774 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5775 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5776 | 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] | 5777 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5778 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 5779 | vmstate_register(0, &vmstate_timers ,&timers_state); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5780 | register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, |
| 5781 | ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5782 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5783 | if (nb_numa_nodes > 0) { |
| 5784 | int i; |
| 5785 | |
| 5786 | if (nb_numa_nodes > smp_cpus) { |
| 5787 | nb_numa_nodes = smp_cpus; |
| 5788 | } |
| 5789 | |
| 5790 | /* If no memory size if given for any node, assume the default case |
| 5791 | * and distribute the available memory equally across all nodes |
| 5792 | */ |
| 5793 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5794 | if (node_mem[i] != 0) |
| 5795 | break; |
| 5796 | } |
| 5797 | if (i == nb_numa_nodes) { |
| 5798 | uint64_t usedmem = 0; |
| 5799 | |
| 5800 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5801 | * the final node gets the rest. |
| 5802 | */ |
| 5803 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5804 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5805 | usedmem += node_mem[i]; |
| 5806 | } |
| 5807 | node_mem[i] = ram_size - usedmem; |
| 5808 | } |
| 5809 | |
| 5810 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5811 | if (node_cpumask[i] != 0) |
| 5812 | break; |
| 5813 | } |
| 5814 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5815 | * must cope with this anyway, because there are BIOSes out there in |
| 5816 | * real machines which also use this scheme. |
| 5817 | */ |
| 5818 | if (i == nb_numa_nodes) { |
| 5819 | for (i = 0; i < smp_cpus; i++) { |
| 5820 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5825 | if (foreach_device_config(DEV_SERIAL, serial_parse) < 0) |
| 5826 | exit(1); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5827 | if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0) |
| 5828 | exit(1); |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5829 | if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0) |
| 5830 | exit(1); |
H. Peter Anvin | c9f398e | 2009-12-29 13:51:36 -0800 | [diff] [blame] | 5831 | if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0) |
| 5832 | exit(1); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5833 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5834 | module_call_init(MODULE_INIT_DEVICE); |
| 5835 | |
Markus Armbruster | ff952ba | 2010-01-29 19:48:57 +0100 | [diff] [blame] | 5836 | if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0) |
| 5837 | exit(0); |
| 5838 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5839 | if (watchdog) { |
| 5840 | i = select_watchdog(watchdog); |
| 5841 | if (i > 0) |
| 5842 | exit (i == 1 ? 1 : 0); |
| 5843 | } |
| 5844 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5845 | if (machine->compat_props) { |
Gerd Hoffmann | 458fb67 | 2009-12-08 13:11:33 +0100 | [diff] [blame] | 5846 | qdev_prop_register_global_list(machine->compat_props); |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5847 | } |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 5848 | qemu_add_globals(); |
| 5849 | |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5850 | machine->init(ram_size, boot_devices, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5851 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5852 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5853 | |
Juan Quintela | 67b3b71 | 2009-08-28 19:25:15 +0200 | [diff] [blame] | 5854 | #ifndef _WIN32 |
| 5855 | /* must be after terminal init, SDL library changes signal handlers */ |
| 5856 | sighandler_setup(); |
| 5857 | #endif |
| 5858 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5859 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5860 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5861 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5862 | env->numa_node = i; |
| 5863 | } |
| 5864 | } |
| 5865 | } |
| 5866 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5867 | current_machine = machine; |
| 5868 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5869 | /* init USB devices */ |
| 5870 | if (usb_enabled) { |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5871 | if (foreach_device_config(DEV_USB, usb_parse) < 0) |
| 5872 | exit(1); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5875 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5876 | 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] | 5877 | exit(1); |
| 5878 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5879 | if (!display_state) |
| 5880 | dumb_display_init(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5881 | /* just use the first displaystate for the moment */ |
| 5882 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5883 | |
| 5884 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5885 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5886 | display_type = DT_SDL; |
| 5887 | #else |
| 5888 | display_type = DT_VNC; |
| 5889 | vnc_display = "localhost:0,to=99"; |
| 5890 | show_vnc_port = 1; |
| 5891 | #endif |
| 5892 | } |
| 5893 | |
| 5894 | |
| 5895 | switch (display_type) { |
| 5896 | case DT_NOGRAPHIC: |
| 5897 | break; |
| 5898 | #if defined(CONFIG_CURSES) |
| 5899 | case DT_CURSES: |
| 5900 | curses_display_init(ds, full_screen); |
| 5901 | break; |
| 5902 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5903 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5904 | case DT_SDL: |
| 5905 | sdl_display_init(ds, full_screen, no_frame); |
| 5906 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5907 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5908 | case DT_SDL: |
| 5909 | cocoa_display_init(ds, full_screen); |
| 5910 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5911 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5912 | case DT_VNC: |
| 5913 | vnc_display_init(ds); |
| 5914 | if (vnc_display_open(ds, vnc_display) < 0) |
| 5915 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5916 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5917 | if (show_vnc_port) { |
| 5918 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5919 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5920 | break; |
| 5921 | default: |
| 5922 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5923 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5924 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 5925 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5926 | dcl = ds->listeners; |
| 5927 | while (dcl != NULL) { |
| 5928 | if (dcl->dpy_refresh != NULL) { |
| 5929 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 5930 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5931 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5932 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5933 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5934 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5935 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 5936 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 5937 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 5938 | } |
| 5939 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5940 | text_consoles_set_display(display_state); |
| 5941 | |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 5942 | if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) |
| 5943 | exit(1); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5944 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5945 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 5946 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 5947 | gdbstub_dev); |
| 5948 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 5949 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 5950 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 5951 | qdev_machine_creation_done(); |
| 5952 | |
Gerd Hoffmann | 15ff770 | 2009-12-14 16:07:35 +0100 | [diff] [blame] | 5953 | if (rom_load_all() != 0) { |
| 5954 | fprintf(stderr, "rom loading failed\n"); |
| 5955 | exit(1); |
| 5956 | } |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 5957 | |
Juan Quintela | 504c294 | 2009-11-12 00:39:13 +0100 | [diff] [blame] | 5958 | qemu_system_reset(); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 5959 | if (loadvm) { |
| 5960 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 5961 | autostart = 0; |
| 5962 | } |
| 5963 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5964 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 5965 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5966 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 5967 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 5968 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 5969 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 5970 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5971 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5972 | if (daemonize) { |
| 5973 | uint8_t status = 0; |
| 5974 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5975 | |
| 5976 | again1: |
| 5977 | len = write(fds[1], &status, 1); |
| 5978 | if (len == -1 && (errno == EINTR)) |
| 5979 | goto again1; |
| 5980 | |
| 5981 | if (len != 1) |
| 5982 | exit(1); |
| 5983 | |
Kirill A. Shutemov | dc330e2 | 2010-01-20 00:56:18 +0100 | [diff] [blame] | 5984 | if (chdir("/")) { |
| 5985 | perror("not able to chdir to /"); |
| 5986 | exit(1); |
| 5987 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 5988 | TFR(fd = qemu_open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5989 | if (fd == -1) |
| 5990 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5991 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5992 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5993 | if (run_as) { |
| 5994 | pwd = getpwnam(run_as); |
| 5995 | if (!pwd) { |
| 5996 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 5997 | exit(1); |
| 5998 | } |
| 5999 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6000 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6001 | if (chroot_dir) { |
| 6002 | if (chroot(chroot_dir) < 0) { |
| 6003 | fprintf(stderr, "chroot failed\n"); |
| 6004 | exit(1); |
| 6005 | } |
Kirill A. Shutemov | dc330e2 | 2010-01-20 00:56:18 +0100 | [diff] [blame] | 6006 | if (chdir("/")) { |
| 6007 | perror("not able to chdir to /"); |
| 6008 | exit(1); |
| 6009 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | if (run_as) { |
| 6013 | if (setgid(pwd->pw_gid) < 0) { |
| 6014 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 6015 | exit(1); |
| 6016 | } |
| 6017 | if (setuid(pwd->pw_uid) < 0) { |
| 6018 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 6019 | exit(1); |
| 6020 | } |
| 6021 | if (setuid(0) != -1) { |
| 6022 | fprintf(stderr, "Dropping privileges failed\n"); |
| 6023 | exit(1); |
| 6024 | } |
| 6025 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6026 | |
| 6027 | if (daemonize) { |
| 6028 | dup2(fd, 0); |
| 6029 | dup2(fd, 1); |
| 6030 | dup2(fd, 2); |
| 6031 | |
| 6032 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6033 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6034 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6035 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6036 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 6037 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 6038 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 6039 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6040 | return 0; |
| 6041 | } |