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