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 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3 | * |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2007 Fabrice Bellard |
bellard | 0824d6f | 2003-06-24 13:42:40 +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 | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 24 | #include "vl.h" |
| 25 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <signal.h> |
| 29 | #include <time.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 30 | #include <errno.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 32 | #include <zlib.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 33 | |
| 34 | #ifndef _WIN32 |
| 35 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 36 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 37 | #include <termios.h> |
| 38 | #include <sys/poll.h> |
| 39 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 42 | #include <netinet/in.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 43 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 44 | #include <netdb.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 45 | #ifdef _BSD |
| 46 | #include <sys/stat.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 47 | #ifndef __APPLE__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 48 | #include <libutil.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 49 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 50 | #else |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 51 | #ifndef __sun__ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 52 | #include <linux/if.h> |
| 53 | #include <linux/if_tun.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 54 | #include <pty.h> |
| 55 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 56 | #include <linux/rtc.h> |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 57 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 58 | #include <linux/parport.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 59 | #else |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/ethernet.h> |
| 62 | #include <sys/sockio.h> |
| 63 | #include <arpa/inet.h> |
| 64 | #include <netinet/arp.h> |
| 65 | #include <netinet/in.h> |
| 66 | #include <netinet/in_systm.h> |
| 67 | #include <netinet/ip.h> |
| 68 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 69 | #include <netinet/udp.h> |
| 70 | #include <netinet/tcp.h> |
| 71 | #include <net/if.h> |
| 72 | #include <syslog.h> |
| 73 | #include <stropts.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 74 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 75 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 76 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 77 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 78 | #if defined(CONFIG_SLIRP) |
| 79 | #include "libslirp.h" |
| 80 | #endif |
| 81 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 82 | #ifdef _WIN32 |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 83 | #include <malloc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 84 | #include <sys/timeb.h> |
| 85 | #include <windows.h> |
| 86 | #define getopt_long_only getopt_long |
| 87 | #define memalign(align, size) malloc(size) |
| 88 | #endif |
| 89 | |
bellard | 6ca957f | 2006-04-30 22:53:25 +0000 | [diff] [blame] | 90 | #include "qemu_socket.h" |
| 91 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 92 | #ifdef CONFIG_SDL |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 93 | #ifdef __APPLE__ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 94 | #include <SDL/SDL.h> |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 95 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 96 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 97 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 98 | #ifdef CONFIG_COCOA |
| 99 | #undef main |
| 100 | #define main qemu_main |
| 101 | #endif /* CONFIG_COCOA */ |
| 102 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 103 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 104 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 105 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 106 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 107 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 108 | #ifdef __sun__ |
| 109 | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
| 110 | #else |
| 111 | #define SMBD_COMMAND "/usr/sbin/smbd" |
| 112 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 113 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 114 | //#define DEBUG_UNUSED_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 115 | //#define DEBUG_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 116 | |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 117 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 118 | |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 119 | #ifdef TARGET_PPC |
| 120 | #define DEFAULT_RAM_SIZE 144 |
| 121 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 122 | #define DEFAULT_RAM_SIZE 128 |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 123 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 124 | /* in ms */ |
| 125 | #define GUI_REFRESH_INTERVAL 30 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 126 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 127 | /* Max number of USB devices that can be specified on the commandline. */ |
| 128 | #define MAX_USB_CMDLINE 8 |
| 129 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 130 | /* XXX: use a two level table to limit memory usage */ |
| 131 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 132 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 133 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 134 | char phys_ram_file[1024]; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 135 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 136 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 137 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 138 | /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available |
| 139 | to store the VM snapshots */ |
| 140 | BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD]; |
| 141 | /* point to the block driver where the snapshots are managed */ |
| 142 | BlockDriverState *bs_snapshots; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 143 | int vga_ram_size; |
| 144 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 145 | int nographic; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 146 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 147 | int64_t ticks_per_sec; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 148 | int boot_device = 'c'; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 149 | int ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 150 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 151 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 152 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 153 | QEMUTimer *gui_timer; |
| 154 | int vm_running; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 155 | int rtc_utc = 1; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 156 | int cirrus_vga_enabled = 1; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 157 | #ifdef TARGET_SPARC |
| 158 | int graphic_width = 1024; |
| 159 | int graphic_height = 768; |
| 160 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 161 | int graphic_width = 800; |
| 162 | int graphic_height = 600; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 163 | #endif |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 164 | int graphic_depth = 15; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 165 | int full_screen = 0; |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 166 | int no_frame = 0; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 167 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 168 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 169 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 170 | #ifdef TARGET_I386 |
| 171 | int win2k_install_hack = 0; |
| 172 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 173 | int usb_enabled = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 174 | static VLANState *first_vlan; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 175 | int smp_cpus = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 176 | const char *vnc_display; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 177 | #if defined(TARGET_SPARC) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 178 | #define MAX_CPUS 16 |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 179 | #elif defined(TARGET_I386) |
| 180 | #define MAX_CPUS 255 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 181 | #else |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 182 | #define MAX_CPUS 1 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 183 | #endif |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 184 | int acpi_enabled = 1; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 185 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 186 | int no_reboot = 0; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 187 | int daemonize = 0; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 188 | const char *option_rom[MAX_OPTION_ROMS]; |
| 189 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 190 | int semihosting_enabled = 0; |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 191 | int autostart = 1; |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 192 | const char *qemu_name; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 193 | |
| 194 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 195 | /* x86 ISA bus support */ |
| 196 | |
| 197 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 198 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 199 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 200 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 201 | { |
| 202 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 203 | fprintf(stderr, "unused inb: port=0x%04x\n", address); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 204 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 205 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 206 | } |
| 207 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 208 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 209 | { |
| 210 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 211 | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 212 | #endif |
| 213 | } |
| 214 | |
| 215 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 216 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 217 | { |
| 218 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 219 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 220 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 221 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 222 | return data; |
| 223 | } |
| 224 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 225 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 226 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 227 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 228 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 229 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 232 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 233 | { |
| 234 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 235 | fprintf(stderr, "unused inl: port=0x%04x\n", address); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 236 | #endif |
| 237 | return 0xffffffff; |
| 238 | } |
| 239 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 240 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 241 | { |
| 242 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 243 | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 244 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void init_ioports(void) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 252 | ioport_read_table[0][i] = default_ioport_readb; |
| 253 | ioport_write_table[0][i] = default_ioport_writeb; |
| 254 | ioport_read_table[1][i] = default_ioport_readw; |
| 255 | ioport_write_table[1][i] = default_ioport_writew; |
| 256 | ioport_read_table[2][i] = default_ioport_readl; |
| 257 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 261 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 262 | int register_ioport_read(int start, int length, int size, |
| 263 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 264 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 265 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 266 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 267 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 268 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 269 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 270 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 271 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 272 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 273 | } else { |
| 274 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 275 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 276 | } |
| 277 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 278 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 279 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 280 | hw_error("register_ioport_read: invalid opaque"); |
| 281 | ioport_opaque[i] = opaque; |
| 282 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 286 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 287 | int register_ioport_write(int start, int length, int size, |
| 288 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 289 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 290 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 291 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 292 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 293 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 294 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 295 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 296 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 297 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 298 | } else { |
| 299 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 300 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 301 | } |
| 302 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 303 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 304 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 305 | hw_error("register_ioport_write: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 306 | ioport_opaque[i] = opaque; |
| 307 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 311 | void isa_unassign_ioport(int start, int length) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | for(i = start; i < start + length; i++) { |
| 316 | ioport_read_table[0][i] = default_ioport_readb; |
| 317 | ioport_read_table[1][i] = default_ioport_readw; |
| 318 | ioport_read_table[2][i] = default_ioport_readl; |
| 319 | |
| 320 | ioport_write_table[0][i] = default_ioport_writeb; |
| 321 | ioport_write_table[1][i] = default_ioport_writew; |
| 322 | ioport_write_table[2][i] = default_ioport_writel; |
| 323 | } |
| 324 | } |
| 325 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 326 | /***********************************************************/ |
| 327 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 328 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 329 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 330 | #ifdef DEBUG_IOPORT |
| 331 | if (loglevel & CPU_LOG_IOPORT) |
| 332 | fprintf(logfile, "outb: %04x %02x\n", addr, val); |
| 333 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 334 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 335 | #ifdef USE_KQEMU |
| 336 | if (env) |
| 337 | env->last_io_time = cpu_get_time_fast(); |
| 338 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 339 | } |
| 340 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 341 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 342 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 343 | #ifdef DEBUG_IOPORT |
| 344 | if (loglevel & CPU_LOG_IOPORT) |
| 345 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
| 346 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 347 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 348 | #ifdef USE_KQEMU |
| 349 | if (env) |
| 350 | env->last_io_time = cpu_get_time_fast(); |
| 351 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 352 | } |
| 353 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 354 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 355 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 356 | #ifdef DEBUG_IOPORT |
| 357 | if (loglevel & CPU_LOG_IOPORT) |
| 358 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 359 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 360 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 361 | #ifdef USE_KQEMU |
| 362 | if (env) |
| 363 | env->last_io_time = cpu_get_time_fast(); |
| 364 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 365 | } |
| 366 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 367 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 368 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 369 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 370 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 371 | #ifdef DEBUG_IOPORT |
| 372 | if (loglevel & CPU_LOG_IOPORT) |
| 373 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 374 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 375 | #ifdef USE_KQEMU |
| 376 | if (env) |
| 377 | env->last_io_time = cpu_get_time_fast(); |
| 378 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 379 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 380 | } |
| 381 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 382 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 383 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 384 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 385 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 386 | #ifdef DEBUG_IOPORT |
| 387 | if (loglevel & CPU_LOG_IOPORT) |
| 388 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 389 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 390 | #ifdef USE_KQEMU |
| 391 | if (env) |
| 392 | env->last_io_time = cpu_get_time_fast(); |
| 393 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 394 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 395 | } |
| 396 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 397 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 398 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 399 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 400 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 401 | #ifdef DEBUG_IOPORT |
| 402 | if (loglevel & CPU_LOG_IOPORT) |
| 403 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 404 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 405 | #ifdef USE_KQEMU |
| 406 | if (env) |
| 407 | env->last_io_time = cpu_get_time_fast(); |
| 408 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 409 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 413 | void hw_error(const char *fmt, ...) |
| 414 | { |
| 415 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 416 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 417 | |
| 418 | va_start(ap, fmt); |
| 419 | fprintf(stderr, "qemu: hardware error: "); |
| 420 | vfprintf(stderr, fmt, ap); |
| 421 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 422 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 423 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 424 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 425 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 426 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 427 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 428 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 429 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 430 | va_end(ap); |
| 431 | abort(); |
| 432 | } |
| 433 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 434 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 435 | /* keyboard/mouse */ |
| 436 | |
| 437 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 438 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 439 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 440 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 441 | |
| 442 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 443 | { |
| 444 | qemu_put_kbd_event_opaque = opaque; |
| 445 | qemu_put_kbd_event = func; |
| 446 | } |
| 447 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 448 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 449 | void *opaque, int absolute, |
| 450 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 451 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 452 | QEMUPutMouseEntry *s, *cursor; |
| 453 | |
| 454 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 455 | if (!s) |
| 456 | return NULL; |
| 457 | |
| 458 | s->qemu_put_mouse_event = func; |
| 459 | s->qemu_put_mouse_event_opaque = opaque; |
| 460 | s->qemu_put_mouse_event_absolute = absolute; |
| 461 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 462 | s->next = NULL; |
| 463 | |
| 464 | if (!qemu_put_mouse_event_head) { |
| 465 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 466 | return s; |
| 467 | } |
| 468 | |
| 469 | cursor = qemu_put_mouse_event_head; |
| 470 | while (cursor->next != NULL) |
| 471 | cursor = cursor->next; |
| 472 | |
| 473 | cursor->next = s; |
| 474 | qemu_put_mouse_event_current = s; |
| 475 | |
| 476 | return s; |
| 477 | } |
| 478 | |
| 479 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 480 | { |
| 481 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 482 | |
| 483 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 484 | return; |
| 485 | |
| 486 | cursor = qemu_put_mouse_event_head; |
| 487 | while (cursor != NULL && cursor != entry) { |
| 488 | prev = cursor; |
| 489 | cursor = cursor->next; |
| 490 | } |
| 491 | |
| 492 | if (cursor == NULL) // does not exist or list empty |
| 493 | return; |
| 494 | else if (prev == NULL) { // entry is head |
| 495 | qemu_put_mouse_event_head = cursor->next; |
| 496 | if (qemu_put_mouse_event_current == entry) |
| 497 | qemu_put_mouse_event_current = cursor->next; |
| 498 | qemu_free(entry->qemu_put_mouse_event_name); |
| 499 | qemu_free(entry); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | prev->next = entry->next; |
| 504 | |
| 505 | if (qemu_put_mouse_event_current == entry) |
| 506 | qemu_put_mouse_event_current = prev; |
| 507 | |
| 508 | qemu_free(entry->qemu_put_mouse_event_name); |
| 509 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void kbd_put_keycode(int keycode) |
| 513 | { |
| 514 | if (qemu_put_kbd_event) { |
| 515 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 520 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 521 | QEMUPutMouseEvent *mouse_event; |
| 522 | void *mouse_event_opaque; |
| 523 | |
| 524 | if (!qemu_put_mouse_event_current) { |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | mouse_event = |
| 529 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 530 | mouse_event_opaque = |
| 531 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 532 | |
| 533 | if (mouse_event) { |
| 534 | mouse_event(mouse_event_opaque, dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 538 | int kbd_mouse_is_absolute(void) |
| 539 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 540 | if (!qemu_put_mouse_event_current) |
| 541 | return 0; |
| 542 | |
| 543 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 544 | } |
| 545 | |
| 546 | void do_info_mice(void) |
| 547 | { |
| 548 | QEMUPutMouseEntry *cursor; |
| 549 | int index = 0; |
| 550 | |
| 551 | if (!qemu_put_mouse_event_head) { |
| 552 | term_printf("No mouse devices connected\n"); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | term_printf("Mouse devices available:\n"); |
| 557 | cursor = qemu_put_mouse_event_head; |
| 558 | while (cursor != NULL) { |
| 559 | term_printf("%c Mouse #%d: %s\n", |
| 560 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 561 | index, cursor->qemu_put_mouse_event_name); |
| 562 | index++; |
| 563 | cursor = cursor->next; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void do_mouse_set(int index) |
| 568 | { |
| 569 | QEMUPutMouseEntry *cursor; |
| 570 | int i = 0; |
| 571 | |
| 572 | if (!qemu_put_mouse_event_head) { |
| 573 | term_printf("No mouse devices connected\n"); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | cursor = qemu_put_mouse_event_head; |
| 578 | while (cursor != NULL && index != i) { |
| 579 | i++; |
| 580 | cursor = cursor->next; |
| 581 | } |
| 582 | |
| 583 | if (cursor != NULL) |
| 584 | qemu_put_mouse_event_current = cursor; |
| 585 | else |
| 586 | term_printf("Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 587 | } |
| 588 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 589 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 590 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 591 | { |
| 592 | union { |
| 593 | uint64_t ll; |
| 594 | struct { |
| 595 | #ifdef WORDS_BIGENDIAN |
| 596 | uint32_t high, low; |
| 597 | #else |
| 598 | uint32_t low, high; |
| 599 | #endif |
| 600 | } l; |
| 601 | } u, res; |
| 602 | uint64_t rl, rh; |
| 603 | |
| 604 | u.ll = a; |
| 605 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 606 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 607 | rh += (rl >> 32); |
| 608 | res.l.high = rh / c; |
| 609 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 610 | return res.ll; |
| 611 | } |
| 612 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 613 | /***********************************************************/ |
| 614 | /* real time host monotonic timer */ |
| 615 | |
| 616 | #define QEMU_TIMER_BASE 1000000000LL |
| 617 | |
| 618 | #ifdef WIN32 |
| 619 | |
| 620 | static int64_t clock_freq; |
| 621 | |
| 622 | static void init_get_clock(void) |
| 623 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 624 | LARGE_INTEGER freq; |
| 625 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 626 | ret = QueryPerformanceFrequency(&freq); |
| 627 | if (ret == 0) { |
| 628 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 629 | exit(1); |
| 630 | } |
| 631 | clock_freq = freq.QuadPart; |
| 632 | } |
| 633 | |
| 634 | static int64_t get_clock(void) |
| 635 | { |
| 636 | LARGE_INTEGER ti; |
| 637 | QueryPerformanceCounter(&ti); |
| 638 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 639 | } |
| 640 | |
| 641 | #else |
| 642 | |
| 643 | static int use_rt_clock; |
| 644 | |
| 645 | static void init_get_clock(void) |
| 646 | { |
| 647 | use_rt_clock = 0; |
| 648 | #if defined(__linux__) |
| 649 | { |
| 650 | struct timespec ts; |
| 651 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 652 | use_rt_clock = 1; |
| 653 | } |
| 654 | } |
| 655 | #endif |
| 656 | } |
| 657 | |
| 658 | static int64_t get_clock(void) |
| 659 | { |
| 660 | #if defined(__linux__) |
| 661 | if (use_rt_clock) { |
| 662 | struct timespec ts; |
| 663 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 664 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| 665 | } else |
| 666 | #endif |
| 667 | { |
| 668 | /* XXX: using gettimeofday leads to problems if the date |
| 669 | changes, so it should be avoided. */ |
| 670 | struct timeval tv; |
| 671 | gettimeofday(&tv, NULL); |
| 672 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | #endif |
| 677 | |
| 678 | /***********************************************************/ |
| 679 | /* guest cycle counter */ |
| 680 | |
| 681 | static int64_t cpu_ticks_prev; |
| 682 | static int64_t cpu_ticks_offset; |
| 683 | static int64_t cpu_clock_offset; |
| 684 | static int cpu_ticks_enabled; |
| 685 | |
| 686 | /* return the host CPU cycle counter and handle stop/restart */ |
| 687 | int64_t cpu_get_ticks(void) |
| 688 | { |
| 689 | if (!cpu_ticks_enabled) { |
| 690 | return cpu_ticks_offset; |
| 691 | } else { |
| 692 | int64_t ticks; |
| 693 | ticks = cpu_get_real_ticks(); |
| 694 | if (cpu_ticks_prev > ticks) { |
| 695 | /* Note: non increasing ticks may happen if the host uses |
| 696 | software suspend */ |
| 697 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 698 | } |
| 699 | cpu_ticks_prev = ticks; |
| 700 | return ticks + cpu_ticks_offset; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 705 | static int64_t cpu_get_clock(void) |
| 706 | { |
| 707 | int64_t ti; |
| 708 | if (!cpu_ticks_enabled) { |
| 709 | return cpu_clock_offset; |
| 710 | } else { |
| 711 | ti = get_clock(); |
| 712 | return ti + cpu_clock_offset; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* enable cpu_get_ticks() */ |
| 717 | void cpu_enable_ticks(void) |
| 718 | { |
| 719 | if (!cpu_ticks_enabled) { |
| 720 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 721 | cpu_clock_offset -= get_clock(); |
| 722 | cpu_ticks_enabled = 1; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 727 | cpu_get_ticks() after that. */ |
| 728 | void cpu_disable_ticks(void) |
| 729 | { |
| 730 | if (cpu_ticks_enabled) { |
| 731 | cpu_ticks_offset = cpu_get_ticks(); |
| 732 | cpu_clock_offset = cpu_get_clock(); |
| 733 | cpu_ticks_enabled = 0; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /***********************************************************/ |
| 738 | /* timers */ |
| 739 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 740 | #define QEMU_TIMER_REALTIME 0 |
| 741 | #define QEMU_TIMER_VIRTUAL 1 |
| 742 | |
| 743 | struct QEMUClock { |
| 744 | int type; |
| 745 | /* XXX: add frequency */ |
| 746 | }; |
| 747 | |
| 748 | struct QEMUTimer { |
| 749 | QEMUClock *clock; |
| 750 | int64_t expire_time; |
| 751 | QEMUTimerCB *cb; |
| 752 | void *opaque; |
| 753 | struct QEMUTimer *next; |
| 754 | }; |
| 755 | |
| 756 | QEMUClock *rt_clock; |
| 757 | QEMUClock *vm_clock; |
| 758 | |
| 759 | static QEMUTimer *active_timers[2]; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 760 | #ifdef _WIN32 |
| 761 | static MMRESULT timerID; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 762 | static HANDLE host_alarm = NULL; |
| 763 | static unsigned int period = 1; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 764 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 765 | /* frequency of the times() clock tick */ |
| 766 | static int timer_freq; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 767 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 768 | |
| 769 | QEMUClock *qemu_new_clock(int type) |
| 770 | { |
| 771 | QEMUClock *clock; |
| 772 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 773 | if (!clock) |
| 774 | return NULL; |
| 775 | clock->type = type; |
| 776 | return clock; |
| 777 | } |
| 778 | |
| 779 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 780 | { |
| 781 | QEMUTimer *ts; |
| 782 | |
| 783 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 784 | ts->clock = clock; |
| 785 | ts->cb = cb; |
| 786 | ts->opaque = opaque; |
| 787 | return ts; |
| 788 | } |
| 789 | |
| 790 | void qemu_free_timer(QEMUTimer *ts) |
| 791 | { |
| 792 | qemu_free(ts); |
| 793 | } |
| 794 | |
| 795 | /* stop a timer, but do not dealloc it */ |
| 796 | void qemu_del_timer(QEMUTimer *ts) |
| 797 | { |
| 798 | QEMUTimer **pt, *t; |
| 799 | |
| 800 | /* NOTE: this code must be signal safe because |
| 801 | qemu_timer_expired() can be called from a signal. */ |
| 802 | pt = &active_timers[ts->clock->type]; |
| 803 | for(;;) { |
| 804 | t = *pt; |
| 805 | if (!t) |
| 806 | break; |
| 807 | if (t == ts) { |
| 808 | *pt = t->next; |
| 809 | break; |
| 810 | } |
| 811 | pt = &t->next; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /* modify the current timer so that it will be fired when current_time |
| 816 | >= expire_time. The corresponding callback will be called. */ |
| 817 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 818 | { |
| 819 | QEMUTimer **pt, *t; |
| 820 | |
| 821 | qemu_del_timer(ts); |
| 822 | |
| 823 | /* add the timer in the sorted list */ |
| 824 | /* NOTE: this code must be signal safe because |
| 825 | qemu_timer_expired() can be called from a signal. */ |
| 826 | pt = &active_timers[ts->clock->type]; |
| 827 | for(;;) { |
| 828 | t = *pt; |
| 829 | if (!t) |
| 830 | break; |
| 831 | if (t->expire_time > expire_time) |
| 832 | break; |
| 833 | pt = &t->next; |
| 834 | } |
| 835 | ts->expire_time = expire_time; |
| 836 | ts->next = *pt; |
| 837 | *pt = ts; |
| 838 | } |
| 839 | |
| 840 | int qemu_timer_pending(QEMUTimer *ts) |
| 841 | { |
| 842 | QEMUTimer *t; |
| 843 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 844 | if (t == ts) |
| 845 | return 1; |
| 846 | } |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 851 | { |
| 852 | if (!timer_head) |
| 853 | return 0; |
| 854 | return (timer_head->expire_time <= current_time); |
| 855 | } |
| 856 | |
| 857 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 858 | { |
| 859 | QEMUTimer *ts; |
| 860 | |
| 861 | for(;;) { |
| 862 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 863 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 864 | break; |
| 865 | /* remove timer from the list before calling the callback */ |
| 866 | *ptimer_head = ts->next; |
| 867 | ts->next = NULL; |
| 868 | |
| 869 | /* run the callback (the timer list can be modified) */ |
| 870 | ts->cb(ts->opaque); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | int64_t qemu_get_clock(QEMUClock *clock) |
| 875 | { |
| 876 | switch(clock->type) { |
| 877 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 878 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 879 | default: |
| 880 | case QEMU_TIMER_VIRTUAL: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 881 | return cpu_get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 885 | static void init_timers(void) |
| 886 | { |
| 887 | init_get_clock(); |
| 888 | ticks_per_sec = QEMU_TIMER_BASE; |
| 889 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 890 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 891 | } |
| 892 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 893 | /* save a timer */ |
| 894 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 895 | { |
| 896 | uint64_t expire_time; |
| 897 | |
| 898 | if (qemu_timer_pending(ts)) { |
| 899 | expire_time = ts->expire_time; |
| 900 | } else { |
| 901 | expire_time = -1; |
| 902 | } |
| 903 | qemu_put_be64(f, expire_time); |
| 904 | } |
| 905 | |
| 906 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 907 | { |
| 908 | uint64_t expire_time; |
| 909 | |
| 910 | expire_time = qemu_get_be64(f); |
| 911 | if (expire_time != -1) { |
| 912 | qemu_mod_timer(ts, expire_time); |
| 913 | } else { |
| 914 | qemu_del_timer(ts); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static void timer_save(QEMUFile *f, void *opaque) |
| 919 | { |
| 920 | if (cpu_ticks_enabled) { |
| 921 | hw_error("cannot save state if virtual timers are running"); |
| 922 | } |
| 923 | qemu_put_be64s(f, &cpu_ticks_offset); |
| 924 | qemu_put_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 925 | qemu_put_be64s(f, &cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 929 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 930 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 931 | return -EINVAL; |
| 932 | if (cpu_ticks_enabled) { |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | qemu_get_be64s(f, &cpu_ticks_offset); |
| 936 | qemu_get_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 937 | if (version_id == 2) { |
| 938 | qemu_get_be64s(f, &cpu_clock_offset); |
| 939 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 943 | #ifdef _WIN32 |
| 944 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 945 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 946 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 947 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 948 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 949 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 950 | #if 0 |
| 951 | #define DISP_FREQ 1000 |
| 952 | { |
| 953 | static int64_t delta_min = INT64_MAX; |
| 954 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 955 | static int count; |
| 956 | ti = qemu_get_clock(vm_clock); |
| 957 | if (last_clock != 0) { |
| 958 | delta = ti - last_clock; |
| 959 | if (delta < delta_min) |
| 960 | delta_min = delta; |
| 961 | if (delta > delta_max) |
| 962 | delta_max = delta; |
| 963 | delta_cum += delta; |
| 964 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 965 | 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] | 966 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 967 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 968 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 969 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 970 | count = 0; |
| 971 | delta_min = INT64_MAX; |
| 972 | delta_max = 0; |
| 973 | delta_cum = 0; |
| 974 | } |
| 975 | } |
| 976 | last_clock = ti; |
| 977 | } |
| 978 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 979 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 980 | qemu_get_clock(vm_clock)) || |
| 981 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 982 | qemu_get_clock(rt_clock))) { |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 983 | #ifdef _WIN32 |
| 984 | SetEvent(host_alarm); |
| 985 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 986 | CPUState *env = cpu_single_env; |
| 987 | if (env) { |
| 988 | /* stop the currently executing cpu because a timer occured */ |
| 989 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 990 | #ifdef USE_KQEMU |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 991 | if (env->kqemu_enabled) { |
| 992 | kqemu_cpu_interrupt(env); |
| 993 | } |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 994 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 995 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 999 | #ifndef _WIN32 |
| 1000 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1001 | #if defined(__linux__) |
| 1002 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1003 | #define RTC_FREQ 1024 |
| 1004 | |
| 1005 | static int rtc_fd; |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1006 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1007 | static int start_rtc_timer(void) |
| 1008 | { |
| 1009 | rtc_fd = open("/dev/rtc", O_RDONLY); |
| 1010 | if (rtc_fd < 0) |
| 1011 | return -1; |
| 1012 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
| 1013 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1014 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1015 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1016 | goto fail; |
| 1017 | } |
| 1018 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1019 | fail: |
| 1020 | close(rtc_fd); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1027 | #else |
| 1028 | |
| 1029 | static int start_rtc_timer(void) |
| 1030 | { |
| 1031 | return -1; |
| 1032 | } |
| 1033 | |
| 1034 | #endif /* !defined(__linux__) */ |
| 1035 | |
| 1036 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1037 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1038 | static void init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1039 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1040 | #ifdef _WIN32 |
| 1041 | { |
| 1042 | int count=0; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1043 | TIMECAPS tc; |
| 1044 | |
| 1045 | ZeroMemory(&tc, sizeof(TIMECAPS)); |
| 1046 | timeGetDevCaps(&tc, sizeof(TIMECAPS)); |
| 1047 | if (period < tc.wPeriodMin) |
| 1048 | period = tc.wPeriodMin; |
| 1049 | timeBeginPeriod(period); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1050 | timerID = timeSetEvent(1, // interval (ms) |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1051 | period, // resolution |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1052 | host_alarm_handler, // function |
| 1053 | (DWORD)&count, // user parameter |
| 1054 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1055 | if( !timerID ) { |
| 1056 | perror("failed timer alarm"); |
| 1057 | exit(1); |
| 1058 | } |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1059 | host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1060 | if (!host_alarm) { |
| 1061 | perror("failed CreateEvent"); |
| 1062 | exit(1); |
| 1063 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 1064 | qemu_add_wait_object(host_alarm, NULL, NULL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1065 | } |
| 1066 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
| 1067 | #else |
| 1068 | { |
| 1069 | struct sigaction act; |
| 1070 | struct itimerval itv; |
| 1071 | |
| 1072 | /* get times() syscall frequency */ |
| 1073 | timer_freq = sysconf(_SC_CLK_TCK); |
| 1074 | |
| 1075 | /* timer signal */ |
| 1076 | sigfillset(&act.sa_mask); |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 1077 | act.sa_flags = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1078 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1079 | act.sa_flags |= SA_ONSTACK; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1080 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1081 | act.sa_handler = host_alarm_handler; |
| 1082 | sigaction(SIGALRM, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1083 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1084 | itv.it_interval.tv_sec = 0; |
bellard | d79284e | 2005-01-26 21:56:26 +0000 | [diff] [blame] | 1085 | itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1086 | itv.it_value.tv_sec = 0; |
| 1087 | itv.it_value.tv_usec = 10 * 1000; |
| 1088 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1089 | /* we probe the tick duration of the kernel to inform the user if |
| 1090 | the emulated kernel requested a too high timer frequency */ |
| 1091 | getitimer(ITIMER_REAL, &itv); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1092 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1093 | #if defined(__linux__) |
bellard | 29e3055 | 2006-05-01 13:28:36 +0000 | [diff] [blame] | 1094 | /* XXX: force /dev/rtc usage because even 2.6 kernels may not |
| 1095 | have timers with 1 ms resolution. The correct solution will |
| 1096 | be to use the POSIX real time timers available in recent |
| 1097 | 2.6 kernels */ |
| 1098 | if (itv.it_interval.tv_usec > 1000 || 1) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1099 | /* try to use /dev/rtc to have a faster timer */ |
| 1100 | if (start_rtc_timer() < 0) |
| 1101 | goto use_itimer; |
| 1102 | /* disable itimer */ |
| 1103 | itv.it_interval.tv_sec = 0; |
| 1104 | itv.it_interval.tv_usec = 0; |
| 1105 | itv.it_value.tv_sec = 0; |
| 1106 | itv.it_value.tv_usec = 0; |
| 1107 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1108 | |
| 1109 | /* use the RTC */ |
bellard | a1968d7 | 2004-06-25 17:06:27 +0000 | [diff] [blame] | 1110 | sigaction(SIGIO, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1111 | fcntl(rtc_fd, F_SETFL, O_ASYNC); |
| 1112 | fcntl(rtc_fd, F_SETOWN, getpid()); |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1113 | } else |
| 1114 | #endif /* defined(__linux__) */ |
| 1115 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1116 | use_itimer: |
| 1117 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
| 1118 | PIT_FREQ) / 1000000; |
| 1119 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1120 | } |
| 1121 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1124 | void quit_timers(void) |
| 1125 | { |
| 1126 | #ifdef _WIN32 |
| 1127 | timeKillEvent(timerID); |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1128 | timeEndPeriod(period); |
| 1129 | if (host_alarm) { |
| 1130 | CloseHandle(host_alarm); |
| 1131 | host_alarm = NULL; |
| 1132 | } |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1133 | #endif |
| 1134 | } |
| 1135 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1136 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1137 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1138 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1139 | static void qemu_chr_event(CharDriverState *s, int event) |
| 1140 | { |
| 1141 | if (!s->chr_event) |
| 1142 | return; |
| 1143 | s->chr_event(s->handler_opaque, event); |
| 1144 | } |
| 1145 | |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1146 | static void qemu_chr_reset_bh(void *opaque) |
| 1147 | { |
| 1148 | CharDriverState *s = opaque; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1149 | qemu_chr_event(s, CHR_EVENT_RESET); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1150 | qemu_bh_delete(s->bh); |
| 1151 | s->bh = NULL; |
| 1152 | } |
| 1153 | |
| 1154 | void qemu_chr_reset(CharDriverState *s) |
| 1155 | { |
| 1156 | if (s->bh == NULL) { |
| 1157 | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
| 1158 | qemu_bh_schedule(s->bh); |
| 1159 | } |
| 1160 | } |
| 1161 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1162 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1163 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1164 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1167 | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1168 | { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1169 | if (!s->chr_ioctl) |
| 1170 | return -ENOTSUP; |
| 1171 | return s->chr_ioctl(s, cmd, arg); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1174 | int qemu_chr_can_read(CharDriverState *s) |
| 1175 | { |
| 1176 | if (!s->chr_can_read) |
| 1177 | return 0; |
| 1178 | return s->chr_can_read(s->handler_opaque); |
| 1179 | } |
| 1180 | |
| 1181 | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
| 1182 | { |
| 1183 | s->chr_read(s->handler_opaque, buf, len); |
| 1184 | } |
| 1185 | |
| 1186 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1187 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1188 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1189 | char buf[4096]; |
| 1190 | va_list ap; |
| 1191 | va_start(ap, fmt); |
| 1192 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1193 | qemu_chr_write(s, buf, strlen(buf)); |
| 1194 | va_end(ap); |
| 1195 | } |
| 1196 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1197 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1198 | { |
| 1199 | if (s->chr_send_event) |
| 1200 | s->chr_send_event(s, event); |
| 1201 | } |
| 1202 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1203 | void qemu_chr_add_handlers(CharDriverState *s, |
| 1204 | IOCanRWHandler *fd_can_read, |
| 1205 | IOReadHandler *fd_read, |
| 1206 | IOEventHandler *fd_event, |
| 1207 | void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1208 | { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1209 | s->chr_can_read = fd_can_read; |
| 1210 | s->chr_read = fd_read; |
| 1211 | s->chr_event = fd_event; |
| 1212 | s->handler_opaque = opaque; |
| 1213 | if (s->chr_update_read_handler) |
| 1214 | s->chr_update_read_handler(s); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1217 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1218 | { |
| 1219 | return len; |
| 1220 | } |
| 1221 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1222 | static CharDriverState *qemu_chr_open_null(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1223 | { |
| 1224 | CharDriverState *chr; |
| 1225 | |
| 1226 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1227 | if (!chr) |
| 1228 | return NULL; |
| 1229 | chr->chr_write = null_chr_write; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1230 | return chr; |
| 1231 | } |
| 1232 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1233 | /* MUX driver for serial I/O splitting */ |
| 1234 | static int term_timestamps; |
| 1235 | static int64_t term_timestamps_start; |
ths | 9c1de61 | 2007-02-21 17:25:30 +0000 | [diff] [blame] | 1236 | #define MAX_MUX 4 |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1237 | typedef struct { |
| 1238 | IOCanRWHandler *chr_can_read[MAX_MUX]; |
| 1239 | IOReadHandler *chr_read[MAX_MUX]; |
| 1240 | IOEventHandler *chr_event[MAX_MUX]; |
| 1241 | void *ext_opaque[MAX_MUX]; |
| 1242 | CharDriverState *drv; |
| 1243 | int mux_cnt; |
| 1244 | int term_got_escape; |
| 1245 | int max_size; |
| 1246 | } MuxDriver; |
| 1247 | |
| 1248 | |
| 1249 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1250 | { |
| 1251 | MuxDriver *d = chr->opaque; |
| 1252 | int ret; |
| 1253 | if (!term_timestamps) { |
| 1254 | ret = d->drv->chr_write(d->drv, buf, len); |
| 1255 | } else { |
| 1256 | int i; |
| 1257 | |
| 1258 | ret = 0; |
| 1259 | for(i = 0; i < len; i++) { |
| 1260 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 1261 | if (buf[i] == '\n') { |
| 1262 | char buf1[64]; |
| 1263 | int64_t ti; |
| 1264 | int secs; |
| 1265 | |
| 1266 | ti = get_clock(); |
| 1267 | if (term_timestamps_start == -1) |
| 1268 | term_timestamps_start = ti; |
| 1269 | ti -= term_timestamps_start; |
| 1270 | secs = ti / 1000000000; |
| 1271 | snprintf(buf1, sizeof(buf1), |
| 1272 | "[%02d:%02d:%02d.%03d] ", |
| 1273 | secs / 3600, |
| 1274 | (secs / 60) % 60, |
| 1275 | secs % 60, |
| 1276 | (int)((ti / 1000000) % 1000)); |
| 1277 | d->drv->chr_write(d->drv, buf1, strlen(buf1)); |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | static char *mux_help[] = { |
| 1285 | "% h print this help\n\r", |
| 1286 | "% x exit emulator\n\r", |
| 1287 | "% s save disk data back to file (if -snapshot)\n\r", |
| 1288 | "% t toggle console timestamps\n\r" |
| 1289 | "% b send break (magic sysrq)\n\r", |
| 1290 | "% c switch between console and monitor\n\r", |
| 1291 | "% % sends %\n\r", |
| 1292 | NULL |
| 1293 | }; |
| 1294 | |
| 1295 | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 1296 | static void mux_print_help(CharDriverState *chr) |
| 1297 | { |
| 1298 | int i, j; |
| 1299 | char ebuf[15] = "Escape-Char"; |
| 1300 | char cbuf[50] = "\n\r"; |
| 1301 | |
| 1302 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 1303 | sprintf(cbuf,"\n\r"); |
| 1304 | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
| 1305 | } else { |
| 1306 | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char); |
| 1307 | } |
| 1308 | chr->chr_write(chr, cbuf, strlen(cbuf)); |
| 1309 | for (i = 0; mux_help[i] != NULL; i++) { |
| 1310 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 1311 | if (mux_help[i][j] == '%') |
| 1312 | chr->chr_write(chr, ebuf, strlen(ebuf)); |
| 1313 | else |
| 1314 | chr->chr_write(chr, &mux_help[i][j], 1); |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 1320 | { |
| 1321 | if (d->term_got_escape) { |
| 1322 | d->term_got_escape = 0; |
| 1323 | if (ch == term_escape_char) |
| 1324 | goto send_char; |
| 1325 | switch(ch) { |
| 1326 | case '?': |
| 1327 | case 'h': |
| 1328 | mux_print_help(chr); |
| 1329 | break; |
| 1330 | case 'x': |
| 1331 | { |
| 1332 | char *term = "QEMU: Terminated\n\r"; |
| 1333 | chr->chr_write(chr,term,strlen(term)); |
| 1334 | exit(0); |
| 1335 | break; |
| 1336 | } |
| 1337 | case 's': |
| 1338 | { |
| 1339 | int i; |
| 1340 | for (i = 0; i < MAX_DISKS; i++) { |
| 1341 | if (bs_table[i]) |
| 1342 | bdrv_commit(bs_table[i]); |
| 1343 | } |
| 1344 | } |
| 1345 | break; |
| 1346 | case 'b': |
| 1347 | if (chr->chr_event) |
| 1348 | chr->chr_event(chr->opaque, CHR_EVENT_BREAK); |
| 1349 | break; |
| 1350 | case 'c': |
| 1351 | /* Switch to the next registered device */ |
| 1352 | chr->focus++; |
| 1353 | if (chr->focus >= d->mux_cnt) |
| 1354 | chr->focus = 0; |
| 1355 | break; |
| 1356 | case 't': |
| 1357 | term_timestamps = !term_timestamps; |
| 1358 | term_timestamps_start = -1; |
| 1359 | break; |
| 1360 | } |
| 1361 | } else if (ch == term_escape_char) { |
| 1362 | d->term_got_escape = 1; |
| 1363 | } else { |
| 1364 | send_char: |
| 1365 | return 1; |
| 1366 | } |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | static int mux_chr_can_read(void *opaque) |
| 1371 | { |
| 1372 | CharDriverState *chr = opaque; |
| 1373 | MuxDriver *d = chr->opaque; |
| 1374 | if (d->chr_can_read[chr->focus]) |
| 1375 | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 1380 | { |
| 1381 | CharDriverState *chr = opaque; |
| 1382 | MuxDriver *d = chr->opaque; |
| 1383 | int i; |
| 1384 | for(i = 0; i < size; i++) |
| 1385 | if (mux_proc_byte(chr, d, buf[i])) |
| 1386 | d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1); |
| 1387 | } |
| 1388 | |
| 1389 | static void mux_chr_event(void *opaque, int event) |
| 1390 | { |
| 1391 | CharDriverState *chr = opaque; |
| 1392 | MuxDriver *d = chr->opaque; |
| 1393 | int i; |
| 1394 | |
| 1395 | /* Send the event to all registered listeners */ |
| 1396 | for (i = 0; i < d->mux_cnt; i++) |
| 1397 | if (d->chr_event[i]) |
| 1398 | d->chr_event[i](d->ext_opaque[i], event); |
| 1399 | } |
| 1400 | |
| 1401 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 1402 | { |
| 1403 | MuxDriver *d = chr->opaque; |
| 1404 | |
| 1405 | if (d->mux_cnt >= MAX_MUX) { |
| 1406 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 1407 | return; |
| 1408 | } |
| 1409 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 1410 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 1411 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 1412 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 1413 | /* Fix up the real driver with mux routines */ |
| 1414 | if (d->mux_cnt == 0) { |
| 1415 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 1416 | mux_chr_event, chr); |
| 1417 | } |
| 1418 | chr->focus = d->mux_cnt; |
| 1419 | d->mux_cnt++; |
| 1420 | } |
| 1421 | |
| 1422 | CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
| 1423 | { |
| 1424 | CharDriverState *chr; |
| 1425 | MuxDriver *d; |
| 1426 | |
| 1427 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1428 | if (!chr) |
| 1429 | return NULL; |
| 1430 | d = qemu_mallocz(sizeof(MuxDriver)); |
| 1431 | if (!d) { |
| 1432 | free(chr); |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | |
| 1436 | chr->opaque = d; |
| 1437 | d->drv = drv; |
| 1438 | chr->focus = -1; |
| 1439 | chr->chr_write = mux_chr_write; |
| 1440 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 1441 | return chr; |
| 1442 | } |
| 1443 | |
| 1444 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1445 | #ifdef _WIN32 |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1446 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1447 | static void socket_cleanup(void) |
| 1448 | { |
| 1449 | WSACleanup(); |
| 1450 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1451 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1452 | static int socket_init(void) |
| 1453 | { |
| 1454 | WSADATA Data; |
| 1455 | int ret, err; |
| 1456 | |
| 1457 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1458 | if (ret != 0) { |
| 1459 | err = WSAGetLastError(); |
| 1460 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | atexit(socket_cleanup); |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static int send_all(int fd, const uint8_t *buf, int len1) |
| 1468 | { |
| 1469 | int ret, len; |
| 1470 | |
| 1471 | len = len1; |
| 1472 | while (len > 0) { |
| 1473 | ret = send(fd, buf, len, 0); |
| 1474 | if (ret < 0) { |
| 1475 | int errno; |
| 1476 | errno = WSAGetLastError(); |
| 1477 | if (errno != WSAEWOULDBLOCK) { |
| 1478 | return -1; |
| 1479 | } |
| 1480 | } else if (ret == 0) { |
| 1481 | break; |
| 1482 | } else { |
| 1483 | buf += ret; |
| 1484 | len -= ret; |
| 1485 | } |
| 1486 | } |
| 1487 | return len1 - len; |
| 1488 | } |
| 1489 | |
| 1490 | void socket_set_nonblock(int fd) |
| 1491 | { |
| 1492 | unsigned long opt = 1; |
| 1493 | ioctlsocket(fd, FIONBIO, &opt); |
| 1494 | } |
| 1495 | |
| 1496 | #else |
| 1497 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1498 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 1499 | { |
| 1500 | int ret, len; |
| 1501 | |
| 1502 | len = len1; |
| 1503 | while (len > 0) { |
| 1504 | ret = write(fd, buf, len); |
| 1505 | if (ret < 0) { |
| 1506 | if (errno != EINTR && errno != EAGAIN) |
| 1507 | return -1; |
| 1508 | } else if (ret == 0) { |
| 1509 | break; |
| 1510 | } else { |
| 1511 | buf += ret; |
| 1512 | len -= ret; |
| 1513 | } |
| 1514 | } |
| 1515 | return len1 - len; |
| 1516 | } |
| 1517 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1518 | static inline int send_all(int fd, const uint8_t *buf, int len1) |
| 1519 | { |
| 1520 | return unix_write(fd, buf, len1); |
| 1521 | } |
| 1522 | |
| 1523 | void socket_set_nonblock(int fd) |
| 1524 | { |
| 1525 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1526 | } |
| 1527 | #endif /* !_WIN32 */ |
| 1528 | |
| 1529 | #ifndef _WIN32 |
| 1530 | |
| 1531 | typedef struct { |
| 1532 | int fd_in, fd_out; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1533 | int max_size; |
| 1534 | } FDCharDriver; |
| 1535 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1536 | #define STDIO_MAX_CLIENTS 1 |
| 1537 | static int stdio_nb_clients = 0; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1538 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1539 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1540 | { |
| 1541 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1542 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1545 | static int fd_chr_read_poll(void *opaque) |
| 1546 | { |
| 1547 | CharDriverState *chr = opaque; |
| 1548 | FDCharDriver *s = chr->opaque; |
| 1549 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1550 | s->max_size = qemu_chr_can_read(chr); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1551 | return s->max_size; |
| 1552 | } |
| 1553 | |
| 1554 | static void fd_chr_read(void *opaque) |
| 1555 | { |
| 1556 | CharDriverState *chr = opaque; |
| 1557 | FDCharDriver *s = chr->opaque; |
| 1558 | int size, len; |
| 1559 | uint8_t buf[1024]; |
| 1560 | |
| 1561 | len = sizeof(buf); |
| 1562 | if (len > s->max_size) |
| 1563 | len = s->max_size; |
| 1564 | if (len == 0) |
| 1565 | return; |
| 1566 | size = read(s->fd_in, buf, len); |
pbrook | 188157f | 2006-11-01 01:44:16 +0000 | [diff] [blame] | 1567 | if (size == 0) { |
| 1568 | /* FD has been closed. Remove it from the active list. */ |
| 1569 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 1570 | return; |
| 1571 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1572 | if (size > 0) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1573 | qemu_chr_read(chr, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1577 | static void fd_chr_update_read_handler(CharDriverState *chr) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1578 | { |
| 1579 | FDCharDriver *s = chr->opaque; |
| 1580 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1581 | if (s->fd_in >= 0) { |
| 1582 | if (nographic && s->fd_in == 0) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1583 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1584 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
| 1585 | fd_chr_read, NULL, chr); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1586 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1590 | /* open a character device to a unix fd */ |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1591 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1592 | { |
| 1593 | CharDriverState *chr; |
| 1594 | FDCharDriver *s; |
| 1595 | |
| 1596 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1597 | if (!chr) |
| 1598 | return NULL; |
| 1599 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 1600 | if (!s) { |
| 1601 | free(chr); |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | s->fd_in = fd_in; |
| 1605 | s->fd_out = fd_out; |
| 1606 | chr->opaque = s; |
| 1607 | chr->chr_write = fd_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1608 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1609 | |
| 1610 | qemu_chr_reset(chr); |
| 1611 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1612 | return chr; |
| 1613 | } |
| 1614 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1615 | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1616 | { |
| 1617 | int fd_out; |
| 1618 | |
pbrook | 89ba1a7 | 2006-03-28 20:20:38 +0000 | [diff] [blame] | 1619 | fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1620 | if (fd_out < 0) |
| 1621 | return NULL; |
| 1622 | return qemu_chr_open_fd(-1, fd_out); |
| 1623 | } |
| 1624 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1625 | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1626 | { |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1627 | int fd_in, fd_out; |
| 1628 | char filename_in[256], filename_out[256]; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1629 | |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1630 | snprintf(filename_in, 256, "%s.in", filename); |
| 1631 | snprintf(filename_out, 256, "%s.out", filename); |
| 1632 | fd_in = open(filename_in, O_RDWR | O_BINARY); |
| 1633 | fd_out = open(filename_out, O_RDWR | O_BINARY); |
| 1634 | if (fd_in < 0 || fd_out < 0) { |
| 1635 | if (fd_in >= 0) |
| 1636 | close(fd_in); |
| 1637 | if (fd_out >= 0) |
| 1638 | close(fd_out); |
| 1639 | fd_in = fd_out = open(filename, O_RDWR | O_BINARY); |
| 1640 | if (fd_in < 0) |
| 1641 | return NULL; |
| 1642 | } |
| 1643 | return qemu_chr_open_fd(fd_in, fd_out); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1647 | /* for STDIO, we handle the case where several clients use it |
| 1648 | (nographic mode) */ |
| 1649 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1650 | #define TERM_FIFO_MAX_SIZE 1 |
| 1651 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1652 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1653 | static int term_fifo_size; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1654 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1655 | static int stdio_read_poll(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1656 | { |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1657 | CharDriverState *chr = opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1658 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1659 | /* try to flush the queue if needed */ |
| 1660 | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
| 1661 | qemu_chr_read(chr, term_fifo, 1); |
| 1662 | term_fifo_size = 0; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1663 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1664 | /* see if we can absorb more chars */ |
| 1665 | if (term_fifo_size == 0) |
| 1666 | return 1; |
| 1667 | else |
| 1668 | return 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1671 | static void stdio_read(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1672 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1673 | int size; |
| 1674 | uint8_t buf[1]; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1675 | CharDriverState *chr = opaque; |
| 1676 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1677 | size = read(0, buf, 1); |
pbrook | 519945d | 2006-09-10 14:39:54 +0000 | [diff] [blame] | 1678 | if (size == 0) { |
| 1679 | /* stdin has been closed. Remove it from the active list. */ |
| 1680 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 1681 | return; |
| 1682 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1683 | if (size > 0) { |
| 1684 | if (qemu_chr_can_read(chr) > 0) { |
| 1685 | qemu_chr_read(chr, buf, 1); |
| 1686 | } else if (term_fifo_size == 0) { |
| 1687 | term_fifo[term_fifo_size++] = buf[0]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1688 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1692 | /* init terminal so that we can grab keys */ |
| 1693 | static struct termios oldtty; |
| 1694 | static int old_fd0_flags; |
| 1695 | |
| 1696 | static void term_exit(void) |
| 1697 | { |
| 1698 | tcsetattr (0, TCSANOW, &oldtty); |
| 1699 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1700 | } |
| 1701 | |
| 1702 | static void term_init(void) |
| 1703 | { |
| 1704 | struct termios tty; |
| 1705 | |
| 1706 | tcgetattr (0, &tty); |
| 1707 | oldtty = tty; |
| 1708 | old_fd0_flags = fcntl(0, F_GETFL); |
| 1709 | |
| 1710 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1711 | |INLCR|IGNCR|ICRNL|IXON); |
| 1712 | tty.c_oflag |= OPOST; |
| 1713 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1714 | /* if graphical mode, we allow Ctrl-C handling */ |
| 1715 | if (nographic) |
| 1716 | tty.c_lflag &= ~ISIG; |
| 1717 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1718 | tty.c_cflag |= CS8; |
| 1719 | tty.c_cc[VMIN] = 1; |
| 1720 | tty.c_cc[VTIME] = 0; |
| 1721 | |
| 1722 | tcsetattr (0, TCSANOW, &tty); |
| 1723 | |
| 1724 | atexit(term_exit); |
| 1725 | |
| 1726 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 1727 | } |
| 1728 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1729 | static CharDriverState *qemu_chr_open_stdio(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1730 | { |
| 1731 | CharDriverState *chr; |
| 1732 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1733 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 1734 | return NULL; |
| 1735 | chr = qemu_chr_open_fd(0, 1); |
| 1736 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
| 1737 | stdio_nb_clients++; |
| 1738 | term_init(); |
| 1739 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1740 | return chr; |
| 1741 | } |
| 1742 | |
| 1743 | #if defined(__linux__) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1744 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1745 | { |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1746 | struct termios tty; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1747 | char slave_name[1024]; |
| 1748 | int master_fd, slave_fd; |
| 1749 | |
| 1750 | /* Not satisfying */ |
| 1751 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 1752 | return NULL; |
| 1753 | } |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1754 | |
| 1755 | /* Disabling local echo and line-buffered output */ |
| 1756 | tcgetattr (master_fd, &tty); |
| 1757 | tty.c_lflag &= ~(ECHO|ICANON|ISIG); |
| 1758 | tty.c_cc[VMIN] = 1; |
| 1759 | tty.c_cc[VTIME] = 0; |
| 1760 | tcsetattr (master_fd, TCSAFLUSH, &tty); |
| 1761 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1762 | fprintf(stderr, "char device redirected to %s\n", slave_name); |
| 1763 | return qemu_chr_open_fd(master_fd, master_fd); |
| 1764 | } |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1765 | |
| 1766 | static void tty_serial_init(int fd, int speed, |
| 1767 | int parity, int data_bits, int stop_bits) |
| 1768 | { |
| 1769 | struct termios tty; |
| 1770 | speed_t spd; |
| 1771 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1772 | #if 0 |
| 1773 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1774 | speed, parity, data_bits, stop_bits); |
| 1775 | #endif |
| 1776 | tcgetattr (fd, &tty); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1777 | |
| 1778 | switch(speed) { |
| 1779 | case 50: |
| 1780 | spd = B50; |
| 1781 | break; |
| 1782 | case 75: |
| 1783 | spd = B75; |
| 1784 | break; |
| 1785 | case 300: |
| 1786 | spd = B300; |
| 1787 | break; |
| 1788 | case 600: |
| 1789 | spd = B600; |
| 1790 | break; |
| 1791 | case 1200: |
| 1792 | spd = B1200; |
| 1793 | break; |
| 1794 | case 2400: |
| 1795 | spd = B2400; |
| 1796 | break; |
| 1797 | case 4800: |
| 1798 | spd = B4800; |
| 1799 | break; |
| 1800 | case 9600: |
| 1801 | spd = B9600; |
| 1802 | break; |
| 1803 | case 19200: |
| 1804 | spd = B19200; |
| 1805 | break; |
| 1806 | case 38400: |
| 1807 | spd = B38400; |
| 1808 | break; |
| 1809 | case 57600: |
| 1810 | spd = B57600; |
| 1811 | break; |
| 1812 | default: |
| 1813 | case 115200: |
| 1814 | spd = B115200; |
| 1815 | break; |
| 1816 | } |
| 1817 | |
| 1818 | cfsetispeed(&tty, spd); |
| 1819 | cfsetospeed(&tty, spd); |
| 1820 | |
| 1821 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1822 | |INLCR|IGNCR|ICRNL|IXON); |
| 1823 | tty.c_oflag |= OPOST; |
| 1824 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1825 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1826 | switch(data_bits) { |
| 1827 | default: |
| 1828 | case 8: |
| 1829 | tty.c_cflag |= CS8; |
| 1830 | break; |
| 1831 | case 7: |
| 1832 | tty.c_cflag |= CS7; |
| 1833 | break; |
| 1834 | case 6: |
| 1835 | tty.c_cflag |= CS6; |
| 1836 | break; |
| 1837 | case 5: |
| 1838 | tty.c_cflag |= CS5; |
| 1839 | break; |
| 1840 | } |
| 1841 | switch(parity) { |
| 1842 | default: |
| 1843 | case 'N': |
| 1844 | break; |
| 1845 | case 'E': |
| 1846 | tty.c_cflag |= PARENB; |
| 1847 | break; |
| 1848 | case 'O': |
| 1849 | tty.c_cflag |= PARENB | PARODD; |
| 1850 | break; |
| 1851 | } |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1852 | if (stop_bits == 2) |
| 1853 | tty.c_cflag |= CSTOPB; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1854 | |
| 1855 | tcsetattr (fd, TCSANOW, &tty); |
| 1856 | } |
| 1857 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1858 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1859 | { |
| 1860 | FDCharDriver *s = chr->opaque; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1861 | |
| 1862 | switch(cmd) { |
| 1863 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1864 | { |
| 1865 | QEMUSerialSetParams *ssp = arg; |
| 1866 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
| 1867 | ssp->data_bits, ssp->stop_bits); |
| 1868 | } |
| 1869 | break; |
| 1870 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1871 | { |
| 1872 | int enable = *(int *)arg; |
| 1873 | if (enable) |
| 1874 | tcsendbreak(s->fd_in, 1); |
| 1875 | } |
| 1876 | break; |
| 1877 | default: |
| 1878 | return -ENOTSUP; |
| 1879 | } |
| 1880 | return 0; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1883 | static CharDriverState *qemu_chr_open_tty(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1884 | { |
| 1885 | CharDriverState *chr; |
| 1886 | int fd; |
| 1887 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1888 | fd = open(filename, O_RDWR | O_NONBLOCK); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1889 | if (fd < 0) |
| 1890 | return NULL; |
| 1891 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1892 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1893 | chr = qemu_chr_open_fd(fd, fd); |
| 1894 | if (!chr) |
| 1895 | return NULL; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1896 | chr->chr_ioctl = tty_serial_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1897 | qemu_chr_reset(chr); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1898 | return chr; |
| 1899 | } |
| 1900 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1901 | typedef struct { |
| 1902 | int fd; |
| 1903 | int mode; |
| 1904 | } ParallelCharDriver; |
| 1905 | |
| 1906 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1907 | { |
| 1908 | if (s->mode != mode) { |
| 1909 | int m = mode; |
| 1910 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1911 | return 0; |
| 1912 | s->mode = mode; |
| 1913 | } |
| 1914 | return 1; |
| 1915 | } |
| 1916 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1917 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1918 | { |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1919 | ParallelCharDriver *drv = chr->opaque; |
| 1920 | int fd = drv->fd; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1921 | uint8_t b; |
| 1922 | |
| 1923 | switch(cmd) { |
| 1924 | case CHR_IOCTL_PP_READ_DATA: |
| 1925 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1926 | return -ENOTSUP; |
| 1927 | *(uint8_t *)arg = b; |
| 1928 | break; |
| 1929 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1930 | b = *(uint8_t *)arg; |
| 1931 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1932 | return -ENOTSUP; |
| 1933 | break; |
| 1934 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1935 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1936 | return -ENOTSUP; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1937 | /* Linux gives only the lowest bits, and no way to know data |
| 1938 | direction! For better compatibility set the fixed upper |
| 1939 | bits. */ |
| 1940 | *(uint8_t *)arg = b | 0xc0; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1941 | break; |
| 1942 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1943 | b = *(uint8_t *)arg; |
| 1944 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1945 | return -ENOTSUP; |
| 1946 | break; |
| 1947 | case CHR_IOCTL_PP_READ_STATUS: |
| 1948 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1949 | return -ENOTSUP; |
| 1950 | *(uint8_t *)arg = b; |
| 1951 | break; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1952 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1953 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1954 | struct ParallelIOArg *parg = arg; |
| 1955 | int n = read(fd, parg->buffer, parg->count); |
| 1956 | if (n != parg->count) { |
| 1957 | return -EIO; |
| 1958 | } |
| 1959 | } |
| 1960 | break; |
| 1961 | case CHR_IOCTL_PP_EPP_READ: |
| 1962 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1963 | struct ParallelIOArg *parg = arg; |
| 1964 | int n = read(fd, parg->buffer, parg->count); |
| 1965 | if (n != parg->count) { |
| 1966 | return -EIO; |
| 1967 | } |
| 1968 | } |
| 1969 | break; |
| 1970 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1971 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1972 | struct ParallelIOArg *parg = arg; |
| 1973 | int n = write(fd, parg->buffer, parg->count); |
| 1974 | if (n != parg->count) { |
| 1975 | return -EIO; |
| 1976 | } |
| 1977 | } |
| 1978 | break; |
| 1979 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1980 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1981 | struct ParallelIOArg *parg = arg; |
| 1982 | int n = write(fd, parg->buffer, parg->count); |
| 1983 | if (n != parg->count) { |
| 1984 | return -EIO; |
| 1985 | } |
| 1986 | } |
| 1987 | break; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1988 | default: |
| 1989 | return -ENOTSUP; |
| 1990 | } |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1994 | static void pp_close(CharDriverState *chr) |
| 1995 | { |
| 1996 | ParallelCharDriver *drv = chr->opaque; |
| 1997 | int fd = drv->fd; |
| 1998 | |
| 1999 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 2000 | ioctl(fd, PPRELEASE); |
| 2001 | close(fd); |
| 2002 | qemu_free(drv); |
| 2003 | } |
| 2004 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2005 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2006 | { |
| 2007 | CharDriverState *chr; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2008 | ParallelCharDriver *drv; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2009 | int fd; |
| 2010 | |
| 2011 | fd = open(filename, O_RDWR); |
| 2012 | if (fd < 0) |
| 2013 | return NULL; |
| 2014 | |
| 2015 | if (ioctl(fd, PPCLAIM) < 0) { |
| 2016 | close(fd); |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2020 | drv = qemu_mallocz(sizeof(ParallelCharDriver)); |
| 2021 | if (!drv) { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2022 | close(fd); |
| 2023 | return NULL; |
| 2024 | } |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2025 | drv->fd = fd; |
| 2026 | drv->mode = IEEE1284_MODE_COMPAT; |
| 2027 | |
| 2028 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2029 | if (!chr) { |
| 2030 | qemu_free(drv); |
| 2031 | close(fd); |
| 2032 | return NULL; |
| 2033 | } |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2034 | chr->chr_write = null_chr_write; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2035 | chr->chr_ioctl = pp_ioctl; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2036 | chr->chr_close = pp_close; |
| 2037 | chr->opaque = drv; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2038 | |
| 2039 | qemu_chr_reset(chr); |
| 2040 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2041 | return chr; |
| 2042 | } |
| 2043 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2044 | #else |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2045 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2046 | { |
| 2047 | return NULL; |
| 2048 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2049 | #endif |
| 2050 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2051 | #endif /* !defined(_WIN32) */ |
| 2052 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2053 | #ifdef _WIN32 |
| 2054 | typedef struct { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2055 | int max_size; |
| 2056 | HANDLE hcom, hrecv, hsend; |
| 2057 | OVERLAPPED orecv, osend; |
| 2058 | BOOL fpipe; |
| 2059 | DWORD len; |
| 2060 | } WinCharState; |
| 2061 | |
| 2062 | #define NSENDBUF 2048 |
| 2063 | #define NRECVBUF 2048 |
| 2064 | #define MAXCONNECT 1 |
| 2065 | #define NTIMEOUT 5000 |
| 2066 | |
| 2067 | static int win_chr_poll(void *opaque); |
| 2068 | static int win_chr_pipe_poll(void *opaque); |
| 2069 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2070 | static void win_chr_close(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2071 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2072 | WinCharState *s = chr->opaque; |
| 2073 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2074 | if (s->hsend) { |
| 2075 | CloseHandle(s->hsend); |
| 2076 | s->hsend = NULL; |
| 2077 | } |
| 2078 | if (s->hrecv) { |
| 2079 | CloseHandle(s->hrecv); |
| 2080 | s->hrecv = NULL; |
| 2081 | } |
| 2082 | if (s->hcom) { |
| 2083 | CloseHandle(s->hcom); |
| 2084 | s->hcom = NULL; |
| 2085 | } |
| 2086 | if (s->fpipe) |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2087 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2088 | else |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2089 | qemu_del_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2092 | static int win_chr_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2093 | { |
| 2094 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2095 | COMMCONFIG comcfg; |
| 2096 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 2097 | COMSTAT comstat; |
| 2098 | DWORD size; |
| 2099 | DWORD err; |
| 2100 | |
| 2101 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2102 | if (!s->hsend) { |
| 2103 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2104 | goto fail; |
| 2105 | } |
| 2106 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2107 | if (!s->hrecv) { |
| 2108 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2109 | goto fail; |
| 2110 | } |
| 2111 | |
| 2112 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 2113 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 2114 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2115 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 2116 | s->hcom = NULL; |
| 2117 | goto fail; |
| 2118 | } |
| 2119 | |
| 2120 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 2121 | fprintf(stderr, "Failed SetupComm\n"); |
| 2122 | goto fail; |
| 2123 | } |
| 2124 | |
| 2125 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 2126 | size = sizeof(COMMCONFIG); |
| 2127 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 2128 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 2129 | CommConfigDialog(filename, NULL, &comcfg); |
| 2130 | |
| 2131 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 2132 | fprintf(stderr, "Failed SetCommState\n"); |
| 2133 | goto fail; |
| 2134 | } |
| 2135 | |
| 2136 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 2137 | fprintf(stderr, "Failed SetCommMask\n"); |
| 2138 | goto fail; |
| 2139 | } |
| 2140 | |
| 2141 | cto.ReadIntervalTimeout = MAXDWORD; |
| 2142 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 2143 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 2144 | goto fail; |
| 2145 | } |
| 2146 | |
| 2147 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 2148 | fprintf(stderr, "Failed ClearCommError\n"); |
| 2149 | goto fail; |
| 2150 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2151 | qemu_add_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2152 | return 0; |
| 2153 | |
| 2154 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2155 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2156 | return -1; |
| 2157 | } |
| 2158 | |
| 2159 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2160 | { |
| 2161 | WinCharState *s = chr->opaque; |
| 2162 | DWORD len, ret, size, err; |
| 2163 | |
| 2164 | len = len1; |
| 2165 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2166 | s->osend.hEvent = s->hsend; |
| 2167 | while (len > 0) { |
| 2168 | if (s->hsend) |
| 2169 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2170 | else |
| 2171 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2172 | if (!ret) { |
| 2173 | err = GetLastError(); |
| 2174 | if (err == ERROR_IO_PENDING) { |
| 2175 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2176 | if (ret) { |
| 2177 | buf += size; |
| 2178 | len -= size; |
| 2179 | } else { |
| 2180 | break; |
| 2181 | } |
| 2182 | } else { |
| 2183 | break; |
| 2184 | } |
| 2185 | } else { |
| 2186 | buf += size; |
| 2187 | len -= size; |
| 2188 | } |
| 2189 | } |
| 2190 | return len1 - len; |
| 2191 | } |
| 2192 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2193 | static int win_chr_read_poll(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2194 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2195 | WinCharState *s = chr->opaque; |
| 2196 | |
| 2197 | s->max_size = qemu_chr_can_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2198 | return s->max_size; |
| 2199 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2200 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2201 | static void win_chr_readfile(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2202 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2203 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2204 | int ret, err; |
| 2205 | uint8_t buf[1024]; |
| 2206 | DWORD size; |
| 2207 | |
| 2208 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2209 | s->orecv.hEvent = s->hrecv; |
| 2210 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2211 | if (!ret) { |
| 2212 | err = GetLastError(); |
| 2213 | if (err == ERROR_IO_PENDING) { |
| 2214 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | if (size > 0) { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2219 | qemu_chr_read(chr, buf, size); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2223 | static void win_chr_read(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2224 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2225 | WinCharState *s = chr->opaque; |
| 2226 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2227 | if (s->len > s->max_size) |
| 2228 | s->len = s->max_size; |
| 2229 | if (s->len == 0) |
| 2230 | return; |
| 2231 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2232 | win_chr_readfile(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | static int win_chr_poll(void *opaque) |
| 2236 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2237 | CharDriverState *chr = opaque; |
| 2238 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2239 | COMSTAT status; |
| 2240 | DWORD comerr; |
| 2241 | |
| 2242 | ClearCommError(s->hcom, &comerr, &status); |
| 2243 | if (status.cbInQue > 0) { |
| 2244 | s->len = status.cbInQue; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2245 | win_chr_read_poll(chr); |
| 2246 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2247 | return 1; |
| 2248 | } |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2252 | static CharDriverState *qemu_chr_open_win(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2253 | { |
| 2254 | CharDriverState *chr; |
| 2255 | WinCharState *s; |
| 2256 | |
| 2257 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2258 | if (!chr) |
| 2259 | return NULL; |
| 2260 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2261 | if (!s) { |
| 2262 | free(chr); |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | chr->opaque = s; |
| 2266 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2267 | chr->chr_close = win_chr_close; |
| 2268 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2269 | if (win_chr_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2270 | free(s); |
| 2271 | free(chr); |
| 2272 | return NULL; |
| 2273 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2274 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2275 | return chr; |
| 2276 | } |
| 2277 | |
| 2278 | static int win_chr_pipe_poll(void *opaque) |
| 2279 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2280 | CharDriverState *chr = opaque; |
| 2281 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2282 | DWORD size; |
| 2283 | |
| 2284 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2285 | if (size > 0) { |
| 2286 | s->len = size; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2287 | win_chr_read_poll(chr); |
| 2288 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2289 | return 1; |
| 2290 | } |
| 2291 | return 0; |
| 2292 | } |
| 2293 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2294 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2295 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2296 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2297 | OVERLAPPED ov; |
| 2298 | int ret; |
| 2299 | DWORD size; |
| 2300 | char openname[256]; |
| 2301 | |
| 2302 | s->fpipe = TRUE; |
| 2303 | |
| 2304 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2305 | if (!s->hsend) { |
| 2306 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2307 | goto fail; |
| 2308 | } |
| 2309 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2310 | if (!s->hrecv) { |
| 2311 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2312 | goto fail; |
| 2313 | } |
| 2314 | |
| 2315 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 2316 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2317 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2318 | PIPE_WAIT, |
| 2319 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 2320 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2321 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 2322 | s->hcom = NULL; |
| 2323 | goto fail; |
| 2324 | } |
| 2325 | |
| 2326 | ZeroMemory(&ov, sizeof(ov)); |
| 2327 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2328 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2329 | if (ret) { |
| 2330 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 2331 | goto fail; |
| 2332 | } |
| 2333 | |
| 2334 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2335 | if (!ret) { |
| 2336 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 2337 | if (ov.hEvent) { |
| 2338 | CloseHandle(ov.hEvent); |
| 2339 | ov.hEvent = NULL; |
| 2340 | } |
| 2341 | goto fail; |
| 2342 | } |
| 2343 | |
| 2344 | if (ov.hEvent) { |
| 2345 | CloseHandle(ov.hEvent); |
| 2346 | ov.hEvent = NULL; |
| 2347 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2348 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2349 | return 0; |
| 2350 | |
| 2351 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2352 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2353 | return -1; |
| 2354 | } |
| 2355 | |
| 2356 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2357 | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2358 | { |
| 2359 | CharDriverState *chr; |
| 2360 | WinCharState *s; |
| 2361 | |
| 2362 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2363 | if (!chr) |
| 2364 | return NULL; |
| 2365 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2366 | if (!s) { |
| 2367 | free(chr); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | chr->opaque = s; |
| 2371 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2372 | chr->chr_close = win_chr_close; |
| 2373 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2374 | if (win_chr_pipe_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2375 | free(s); |
| 2376 | free(chr); |
| 2377 | return NULL; |
| 2378 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2379 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2380 | return chr; |
| 2381 | } |
| 2382 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2383 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2384 | { |
| 2385 | CharDriverState *chr; |
| 2386 | WinCharState *s; |
| 2387 | |
| 2388 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2389 | if (!chr) |
| 2390 | return NULL; |
| 2391 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2392 | if (!s) { |
| 2393 | free(chr); |
| 2394 | return NULL; |
| 2395 | } |
| 2396 | s->hcom = fd_out; |
| 2397 | chr->opaque = s; |
| 2398 | chr->chr_write = win_chr_write; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2399 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2400 | return chr; |
| 2401 | } |
| 2402 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2403 | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2404 | { |
| 2405 | HANDLE fd_out; |
| 2406 | |
| 2407 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 2408 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 2409 | if (fd_out == INVALID_HANDLE_VALUE) |
| 2410 | return NULL; |
| 2411 | |
| 2412 | return qemu_chr_open_win_file(fd_out); |
| 2413 | } |
| 2414 | #endif |
| 2415 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2416 | /***********************************************************/ |
| 2417 | /* UDP Net console */ |
| 2418 | |
| 2419 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2420 | int fd; |
| 2421 | struct sockaddr_in daddr; |
| 2422 | char buf[1024]; |
| 2423 | int bufcnt; |
| 2424 | int bufptr; |
| 2425 | int max_size; |
| 2426 | } NetCharDriver; |
| 2427 | |
| 2428 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2429 | { |
| 2430 | NetCharDriver *s = chr->opaque; |
| 2431 | |
| 2432 | return sendto(s->fd, buf, len, 0, |
| 2433 | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
| 2434 | } |
| 2435 | |
| 2436 | static int udp_chr_read_poll(void *opaque) |
| 2437 | { |
| 2438 | CharDriverState *chr = opaque; |
| 2439 | NetCharDriver *s = chr->opaque; |
| 2440 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2441 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* If there were any stray characters in the queue process them |
| 2444 | * first |
| 2445 | */ |
| 2446 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2447 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2448 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2449 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2450 | } |
| 2451 | return s->max_size; |
| 2452 | } |
| 2453 | |
| 2454 | static void udp_chr_read(void *opaque) |
| 2455 | { |
| 2456 | CharDriverState *chr = opaque; |
| 2457 | NetCharDriver *s = chr->opaque; |
| 2458 | |
| 2459 | if (s->max_size == 0) |
| 2460 | return; |
| 2461 | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 2462 | s->bufptr = s->bufcnt; |
| 2463 | if (s->bufcnt <= 0) |
| 2464 | return; |
| 2465 | |
| 2466 | s->bufptr = 0; |
| 2467 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2468 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2469 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2470 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2471 | } |
| 2472 | } |
| 2473 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2474 | static void udp_chr_update_read_handler(CharDriverState *chr) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2475 | { |
| 2476 | NetCharDriver *s = chr->opaque; |
| 2477 | |
| 2478 | if (s->fd >= 0) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2479 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 2480 | udp_chr_read, NULL, chr); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2485 | #ifndef _WIN32 |
| 2486 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
| 2487 | #endif |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2488 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 2489 | struct sockaddr_in *saddr, |
| 2490 | const char *str); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2491 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2492 | static CharDriverState *qemu_chr_open_udp(const char *def) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2493 | { |
| 2494 | CharDriverState *chr = NULL; |
| 2495 | NetCharDriver *s = NULL; |
| 2496 | int fd = -1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2497 | struct sockaddr_in saddr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2498 | |
| 2499 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2500 | if (!chr) |
| 2501 | goto return_err; |
| 2502 | s = qemu_mallocz(sizeof(NetCharDriver)); |
| 2503 | if (!s) |
| 2504 | goto return_err; |
| 2505 | |
| 2506 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 2507 | if (fd < 0) { |
| 2508 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 2509 | goto return_err; |
| 2510 | } |
| 2511 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2512 | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
| 2513 | printf("Could not parse: %s\n", def); |
| 2514 | goto return_err; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2517 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2518 | { |
| 2519 | perror("bind"); |
| 2520 | goto return_err; |
| 2521 | } |
| 2522 | |
| 2523 | s->fd = fd; |
| 2524 | s->bufcnt = 0; |
| 2525 | s->bufptr = 0; |
| 2526 | chr->opaque = s; |
| 2527 | chr->chr_write = udp_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2528 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2529 | return chr; |
| 2530 | |
| 2531 | return_err: |
| 2532 | if (chr) |
| 2533 | free(chr); |
| 2534 | if (s) |
| 2535 | free(s); |
| 2536 | if (fd >= 0) |
| 2537 | closesocket(fd); |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | |
| 2541 | /***********************************************************/ |
| 2542 | /* TCP Net console */ |
| 2543 | |
| 2544 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2545 | int fd, listen_fd; |
| 2546 | int connected; |
| 2547 | int max_size; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2548 | int do_telnetopt; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2549 | int do_nodelay; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2550 | int is_unix; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2551 | } TCPCharDriver; |
| 2552 | |
| 2553 | static void tcp_chr_accept(void *opaque); |
| 2554 | |
| 2555 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2556 | { |
| 2557 | TCPCharDriver *s = chr->opaque; |
| 2558 | if (s->connected) { |
| 2559 | return send_all(s->fd, buf, len); |
| 2560 | } else { |
| 2561 | /* XXX: indicate an error ? */ |
| 2562 | return len; |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | static int tcp_chr_read_poll(void *opaque) |
| 2567 | { |
| 2568 | CharDriverState *chr = opaque; |
| 2569 | TCPCharDriver *s = chr->opaque; |
| 2570 | if (!s->connected) |
| 2571 | return 0; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2572 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2573 | return s->max_size; |
| 2574 | } |
| 2575 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2576 | #define IAC 255 |
| 2577 | #define IAC_BREAK 243 |
| 2578 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2579 | TCPCharDriver *s, |
| 2580 | char *buf, int *size) |
| 2581 | { |
| 2582 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2583 | * char mode with no echo. All IAC options will be removed from |
| 2584 | * the buf and the do_telnetopt variable will be used to track the |
| 2585 | * state of the width of the IAC information. |
| 2586 | * |
| 2587 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2588 | * "IAC BREAK" command and the double IAC. |
| 2589 | */ |
| 2590 | |
| 2591 | int i; |
| 2592 | int j = 0; |
| 2593 | |
| 2594 | for (i = 0; i < *size; i++) { |
| 2595 | if (s->do_telnetopt > 1) { |
| 2596 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2597 | /* Double IAC means send an IAC */ |
| 2598 | if (j != i) |
| 2599 | buf[j] = buf[i]; |
| 2600 | j++; |
| 2601 | s->do_telnetopt = 1; |
| 2602 | } else { |
| 2603 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2604 | /* Handle IAC break commands by sending a serial break */ |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2605 | qemu_chr_event(chr, CHR_EVENT_BREAK); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2606 | s->do_telnetopt++; |
| 2607 | } |
| 2608 | s->do_telnetopt++; |
| 2609 | } |
| 2610 | if (s->do_telnetopt >= 4) { |
| 2611 | s->do_telnetopt = 1; |
| 2612 | } |
| 2613 | } else { |
| 2614 | if ((unsigned char)buf[i] == IAC) { |
| 2615 | s->do_telnetopt = 2; |
| 2616 | } else { |
| 2617 | if (j != i) |
| 2618 | buf[j] = buf[i]; |
| 2619 | j++; |
| 2620 | } |
| 2621 | } |
| 2622 | } |
| 2623 | *size = j; |
| 2624 | } |
| 2625 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2626 | static void tcp_chr_read(void *opaque) |
| 2627 | { |
| 2628 | CharDriverState *chr = opaque; |
| 2629 | TCPCharDriver *s = chr->opaque; |
| 2630 | uint8_t buf[1024]; |
| 2631 | int len, size; |
| 2632 | |
| 2633 | if (!s->connected || s->max_size <= 0) |
| 2634 | return; |
| 2635 | len = sizeof(buf); |
| 2636 | if (len > s->max_size) |
| 2637 | len = s->max_size; |
| 2638 | size = recv(s->fd, buf, len, 0); |
| 2639 | if (size == 0) { |
| 2640 | /* connection closed */ |
| 2641 | s->connected = 0; |
| 2642 | if (s->listen_fd >= 0) { |
| 2643 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
| 2644 | } |
| 2645 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 2646 | closesocket(s->fd); |
| 2647 | s->fd = -1; |
| 2648 | } else if (size > 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2649 | if (s->do_telnetopt) |
| 2650 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2651 | if (size > 0) |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2652 | qemu_chr_read(chr, buf, size); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2656 | static void tcp_chr_connect(void *opaque) |
| 2657 | { |
| 2658 | CharDriverState *chr = opaque; |
| 2659 | TCPCharDriver *s = chr->opaque; |
| 2660 | |
| 2661 | s->connected = 1; |
| 2662 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 2663 | tcp_chr_read, NULL, chr); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2664 | qemu_chr_reset(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2667 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2668 | static void tcp_chr_telnet_init(int fd) |
| 2669 | { |
| 2670 | char buf[3]; |
| 2671 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2672 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2673 | send(fd, (char *)buf, 3, 0); |
| 2674 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2675 | send(fd, (char *)buf, 3, 0); |
| 2676 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2677 | send(fd, (char *)buf, 3, 0); |
| 2678 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2679 | send(fd, (char *)buf, 3, 0); |
| 2680 | } |
| 2681 | |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2682 | static void socket_set_nodelay(int fd) |
| 2683 | { |
| 2684 | int val = 1; |
| 2685 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| 2686 | } |
| 2687 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2688 | static void tcp_chr_accept(void *opaque) |
| 2689 | { |
| 2690 | CharDriverState *chr = opaque; |
| 2691 | TCPCharDriver *s = chr->opaque; |
| 2692 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2693 | #ifndef _WIN32 |
| 2694 | struct sockaddr_un uaddr; |
| 2695 | #endif |
| 2696 | struct sockaddr *addr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2697 | socklen_t len; |
| 2698 | int fd; |
| 2699 | |
| 2700 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2701 | #ifndef _WIN32 |
| 2702 | if (s->is_unix) { |
| 2703 | len = sizeof(uaddr); |
| 2704 | addr = (struct sockaddr *)&uaddr; |
| 2705 | } else |
| 2706 | #endif |
| 2707 | { |
| 2708 | len = sizeof(saddr); |
| 2709 | addr = (struct sockaddr *)&saddr; |
| 2710 | } |
| 2711 | fd = accept(s->listen_fd, addr, &len); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2712 | if (fd < 0 && errno != EINTR) { |
| 2713 | return; |
| 2714 | } else if (fd >= 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2715 | if (s->do_telnetopt) |
| 2716 | tcp_chr_telnet_init(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2717 | break; |
| 2718 | } |
| 2719 | } |
| 2720 | socket_set_nonblock(fd); |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2721 | if (s->do_nodelay) |
| 2722 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2723 | s->fd = fd; |
| 2724 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 2725 | tcp_chr_connect(chr); |
| 2726 | } |
| 2727 | |
| 2728 | static void tcp_chr_close(CharDriverState *chr) |
| 2729 | { |
| 2730 | TCPCharDriver *s = chr->opaque; |
| 2731 | if (s->fd >= 0) |
| 2732 | closesocket(s->fd); |
| 2733 | if (s->listen_fd >= 0) |
| 2734 | closesocket(s->listen_fd); |
| 2735 | qemu_free(s); |
| 2736 | } |
| 2737 | |
| 2738 | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2739 | int is_telnet, |
| 2740 | int is_unix) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2741 | { |
| 2742 | CharDriverState *chr = NULL; |
| 2743 | TCPCharDriver *s = NULL; |
| 2744 | int fd = -1, ret, err, val; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2745 | int is_listen = 0; |
| 2746 | int is_waitconnect = 1; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2747 | int do_nodelay = 0; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2748 | const char *ptr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2749 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2750 | #ifndef _WIN32 |
| 2751 | struct sockaddr_un uaddr; |
| 2752 | #endif |
| 2753 | struct sockaddr *addr; |
| 2754 | socklen_t addrlen; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2755 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2756 | #ifndef _WIN32 |
| 2757 | if (is_unix) { |
| 2758 | addr = (struct sockaddr *)&uaddr; |
| 2759 | addrlen = sizeof(uaddr); |
| 2760 | if (parse_unix_path(&uaddr, host_str) < 0) |
| 2761 | goto fail; |
| 2762 | } else |
| 2763 | #endif |
| 2764 | { |
| 2765 | addr = (struct sockaddr *)&saddr; |
| 2766 | addrlen = sizeof(saddr); |
| 2767 | if (parse_host_port(&saddr, host_str) < 0) |
| 2768 | goto fail; |
| 2769 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2770 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2771 | ptr = host_str; |
| 2772 | while((ptr = strchr(ptr,','))) { |
| 2773 | ptr++; |
| 2774 | if (!strncmp(ptr,"server",6)) { |
| 2775 | is_listen = 1; |
| 2776 | } else if (!strncmp(ptr,"nowait",6)) { |
| 2777 | is_waitconnect = 0; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2778 | } else if (!strncmp(ptr,"nodelay",6)) { |
| 2779 | do_nodelay = 1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2780 | } else { |
| 2781 | printf("Unknown option: %s\n", ptr); |
| 2782 | goto fail; |
| 2783 | } |
| 2784 | } |
| 2785 | if (!is_listen) |
| 2786 | is_waitconnect = 0; |
| 2787 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2788 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2789 | if (!chr) |
| 2790 | goto fail; |
| 2791 | s = qemu_mallocz(sizeof(TCPCharDriver)); |
| 2792 | if (!s) |
| 2793 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2794 | |
| 2795 | #ifndef _WIN32 |
| 2796 | if (is_unix) |
| 2797 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 2798 | else |
| 2799 | #endif |
| 2800 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 2801 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2802 | if (fd < 0) |
| 2803 | goto fail; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2804 | |
| 2805 | if (!is_waitconnect) |
| 2806 | socket_set_nonblock(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2807 | |
| 2808 | s->connected = 0; |
| 2809 | s->fd = -1; |
| 2810 | s->listen_fd = -1; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2811 | s->is_unix = is_unix; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2812 | s->do_nodelay = do_nodelay && !is_unix; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2813 | |
| 2814 | chr->opaque = s; |
| 2815 | chr->chr_write = tcp_chr_write; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2816 | chr->chr_close = tcp_chr_close; |
| 2817 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2818 | if (is_listen) { |
| 2819 | /* allow fast reuse */ |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2820 | #ifndef _WIN32 |
| 2821 | if (is_unix) { |
| 2822 | char path[109]; |
| 2823 | strncpy(path, uaddr.sun_path, 108); |
| 2824 | path[108] = 0; |
| 2825 | unlink(path); |
| 2826 | } else |
| 2827 | #endif |
| 2828 | { |
| 2829 | val = 1; |
| 2830 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 2831 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2832 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2833 | ret = bind(fd, addr, addrlen); |
| 2834 | if (ret < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2835 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2836 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2837 | ret = listen(fd, 0); |
| 2838 | if (ret < 0) |
| 2839 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2840 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2841 | s->listen_fd = fd; |
| 2842 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2843 | if (is_telnet) |
| 2844 | s->do_telnetopt = 1; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2845 | } else { |
| 2846 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2847 | ret = connect(fd, addr, addrlen); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2848 | if (ret < 0) { |
| 2849 | err = socket_error(); |
| 2850 | if (err == EINTR || err == EWOULDBLOCK) { |
| 2851 | } else if (err == EINPROGRESS) { |
| 2852 | break; |
ths | f5b1226 | 2007-03-25 15:58:03 +0000 | [diff] [blame] | 2853 | #ifdef _WIN32 |
| 2854 | } else if (err == WSAEALREADY) { |
| 2855 | break; |
| 2856 | #endif |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2857 | } else { |
| 2858 | goto fail; |
| 2859 | } |
| 2860 | } else { |
| 2861 | s->connected = 1; |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | s->fd = fd; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2866 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2867 | if (s->connected) |
| 2868 | tcp_chr_connect(chr); |
| 2869 | else |
| 2870 | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr); |
| 2871 | } |
| 2872 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2873 | if (is_listen && is_waitconnect) { |
| 2874 | printf("QEMU waiting for connection on: %s\n", host_str); |
| 2875 | tcp_chr_accept(chr); |
| 2876 | socket_set_nonblock(s->listen_fd); |
| 2877 | } |
| 2878 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2879 | return chr; |
| 2880 | fail: |
| 2881 | if (fd >= 0) |
| 2882 | closesocket(fd); |
| 2883 | qemu_free(s); |
| 2884 | qemu_free(chr); |
| 2885 | return NULL; |
| 2886 | } |
| 2887 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2888 | CharDriverState *qemu_chr_open(const char *filename) |
| 2889 | { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2890 | const char *p; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2891 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2892 | if (!strcmp(filename, "vc")) { |
| 2893 | return text_console_init(&display_state); |
| 2894 | } else if (!strcmp(filename, "null")) { |
| 2895 | return qemu_chr_open_null(); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2896 | } else |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2897 | if (strstart(filename, "tcp:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2898 | return qemu_chr_open_tcp(p, 0, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2899 | } else |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2900 | if (strstart(filename, "telnet:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2901 | return qemu_chr_open_tcp(p, 1, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2902 | } else |
| 2903 | if (strstart(filename, "udp:", &p)) { |
| 2904 | return qemu_chr_open_udp(p); |
| 2905 | } else |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2906 | if (strstart(filename, "mon:", &p)) { |
| 2907 | CharDriverState *drv = qemu_chr_open(p); |
| 2908 | if (drv) { |
| 2909 | drv = qemu_chr_open_mux(drv); |
| 2910 | monitor_init(drv, !nographic); |
| 2911 | return drv; |
| 2912 | } |
| 2913 | printf("Unable to open driver: %s\n", p); |
| 2914 | return 0; |
| 2915 | } else |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2916 | #ifndef _WIN32 |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2917 | if (strstart(filename, "unix:", &p)) { |
| 2918 | return qemu_chr_open_tcp(p, 0, 1); |
| 2919 | } else if (strstart(filename, "file:", &p)) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2920 | return qemu_chr_open_file_out(p); |
| 2921 | } else if (strstart(filename, "pipe:", &p)) { |
| 2922 | return qemu_chr_open_pipe(p); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2923 | } else if (!strcmp(filename, "pty")) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2924 | return qemu_chr_open_pty(); |
| 2925 | } else if (!strcmp(filename, "stdio")) { |
| 2926 | return qemu_chr_open_stdio(); |
| 2927 | } else |
| 2928 | #endif |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2929 | #if defined(__linux__) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2930 | if (strstart(filename, "/dev/parport", NULL)) { |
| 2931 | return qemu_chr_open_pp(filename); |
| 2932 | } else |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2933 | if (strstart(filename, "/dev/", NULL)) { |
| 2934 | return qemu_chr_open_tty(filename); |
| 2935 | } else |
| 2936 | #endif |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2937 | #ifdef _WIN32 |
| 2938 | if (strstart(filename, "COM", NULL)) { |
| 2939 | return qemu_chr_open_win(filename); |
| 2940 | } else |
| 2941 | if (strstart(filename, "pipe:", &p)) { |
| 2942 | return qemu_chr_open_win_pipe(p); |
| 2943 | } else |
| 2944 | if (strstart(filename, "file:", &p)) { |
| 2945 | return qemu_chr_open_win_file_out(p); |
| 2946 | } |
| 2947 | #endif |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2948 | { |
| 2949 | return NULL; |
| 2950 | } |
| 2951 | } |
| 2952 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2953 | void qemu_chr_close(CharDriverState *chr) |
| 2954 | { |
| 2955 | if (chr->chr_close) |
| 2956 | chr->chr_close(chr); |
| 2957 | } |
| 2958 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2959 | /***********************************************************/ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2960 | /* network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2961 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2962 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2963 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2964 | int len, i, j, c; |
| 2965 | |
| 2966 | for(i=0;i<size;i+=16) { |
| 2967 | len = size - i; |
| 2968 | if (len > 16) |
| 2969 | len = 16; |
| 2970 | fprintf(f, "%08x ", i); |
| 2971 | for(j=0;j<16;j++) { |
| 2972 | if (j < len) |
| 2973 | fprintf(f, " %02x", buf[i+j]); |
| 2974 | else |
| 2975 | fprintf(f, " "); |
| 2976 | } |
| 2977 | fprintf(f, " "); |
| 2978 | for(j=0;j<len;j++) { |
| 2979 | c = buf[i+j]; |
| 2980 | if (c < ' ' || c > '~') |
| 2981 | c = '.'; |
| 2982 | fprintf(f, "%c", c); |
| 2983 | } |
| 2984 | fprintf(f, "\n"); |
| 2985 | } |
| 2986 | } |
| 2987 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2988 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2989 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2990 | int i; |
| 2991 | for(i = 0; i < 6; i++) { |
| 2992 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 2993 | if (i == 5) { |
| 2994 | if (*p != '\0') |
| 2995 | return -1; |
| 2996 | } else { |
| 2997 | if (*p != ':') |
| 2998 | return -1; |
| 2999 | p++; |
| 3000 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3001 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3002 | return 0; |
| 3003 | } |
| 3004 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3005 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 3006 | { |
| 3007 | const char *p, *p1; |
| 3008 | int len; |
| 3009 | p = *pp; |
| 3010 | p1 = strchr(p, sep); |
| 3011 | if (!p1) |
| 3012 | return -1; |
| 3013 | len = p1 - p; |
| 3014 | p1++; |
| 3015 | if (buf_size > 0) { |
| 3016 | if (len > buf_size - 1) |
| 3017 | len = buf_size - 1; |
| 3018 | memcpy(buf, p, len); |
| 3019 | buf[len] = '\0'; |
| 3020 | } |
| 3021 | *pp = p1; |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3025 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 3026 | struct sockaddr_in *saddr, |
| 3027 | const char *input_str) |
| 3028 | { |
| 3029 | char *str = strdup(input_str); |
| 3030 | char *host_str = str; |
| 3031 | char *src_str; |
| 3032 | char *ptr; |
| 3033 | |
| 3034 | /* |
| 3035 | * Chop off any extra arguments at the end of the string which |
| 3036 | * would start with a comma, then fill in the src port information |
| 3037 | * if it was provided else use the "any address" and "any port". |
| 3038 | */ |
| 3039 | if ((ptr = strchr(str,','))) |
| 3040 | *ptr = '\0'; |
| 3041 | |
| 3042 | if ((src_str = strchr(input_str,'@'))) { |
| 3043 | *src_str = '\0'; |
| 3044 | src_str++; |
| 3045 | } |
| 3046 | |
| 3047 | if (parse_host_port(haddr, host_str) < 0) |
| 3048 | goto fail; |
| 3049 | |
| 3050 | if (!src_str || *src_str == '\0') |
| 3051 | src_str = ":0"; |
| 3052 | |
| 3053 | if (parse_host_port(saddr, src_str) < 0) |
| 3054 | goto fail; |
| 3055 | |
| 3056 | free(str); |
| 3057 | return(0); |
| 3058 | |
| 3059 | fail: |
| 3060 | free(str); |
| 3061 | return -1; |
| 3062 | } |
| 3063 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3064 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 3065 | { |
| 3066 | char buf[512]; |
| 3067 | struct hostent *he; |
| 3068 | const char *p, *r; |
| 3069 | int port; |
| 3070 | |
| 3071 | p = str; |
| 3072 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3073 | return -1; |
| 3074 | saddr->sin_family = AF_INET; |
| 3075 | if (buf[0] == '\0') { |
| 3076 | saddr->sin_addr.s_addr = 0; |
| 3077 | } else { |
| 3078 | if (isdigit(buf[0])) { |
| 3079 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 3080 | return -1; |
| 3081 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3082 | if ((he = gethostbyname(buf)) == NULL) |
| 3083 | return - 1; |
| 3084 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | port = strtol(p, (char **)&r, 0); |
| 3088 | if (r == p) |
| 3089 | return -1; |
| 3090 | saddr->sin_port = htons(port); |
| 3091 | return 0; |
| 3092 | } |
| 3093 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3094 | #ifndef _WIN32 |
| 3095 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3096 | { |
| 3097 | const char *p; |
| 3098 | int len; |
| 3099 | |
| 3100 | len = MIN(108, strlen(str)); |
| 3101 | p = strchr(str, ','); |
| 3102 | if (p) |
| 3103 | len = MIN(len, p - str); |
| 3104 | |
| 3105 | memset(uaddr, 0, sizeof(*uaddr)); |
| 3106 | |
| 3107 | uaddr->sun_family = AF_UNIX; |
| 3108 | memcpy(uaddr->sun_path, str, len); |
| 3109 | |
| 3110 | return 0; |
| 3111 | } |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3112 | #endif |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3113 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3114 | /* find or alloc a new VLAN */ |
| 3115 | VLANState *qemu_find_vlan(int id) |
| 3116 | { |
| 3117 | VLANState **pvlan, *vlan; |
| 3118 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 3119 | if (vlan->id == id) |
| 3120 | return vlan; |
| 3121 | } |
| 3122 | vlan = qemu_mallocz(sizeof(VLANState)); |
| 3123 | if (!vlan) |
| 3124 | return NULL; |
| 3125 | vlan->id = id; |
| 3126 | vlan->next = NULL; |
| 3127 | pvlan = &first_vlan; |
| 3128 | while (*pvlan != NULL) |
| 3129 | pvlan = &(*pvlan)->next; |
| 3130 | *pvlan = vlan; |
| 3131 | return vlan; |
| 3132 | } |
| 3133 | |
| 3134 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3135 | IOReadHandler *fd_read, |
| 3136 | IOCanRWHandler *fd_can_read, |
| 3137 | void *opaque) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3138 | { |
| 3139 | VLANClientState *vc, **pvc; |
| 3140 | vc = qemu_mallocz(sizeof(VLANClientState)); |
| 3141 | if (!vc) |
| 3142 | return NULL; |
| 3143 | vc->fd_read = fd_read; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3144 | vc->fd_can_read = fd_can_read; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3145 | vc->opaque = opaque; |
| 3146 | vc->vlan = vlan; |
| 3147 | |
| 3148 | vc->next = NULL; |
| 3149 | pvc = &vlan->first_client; |
| 3150 | while (*pvc != NULL) |
| 3151 | pvc = &(*pvc)->next; |
| 3152 | *pvc = vc; |
| 3153 | return vc; |
| 3154 | } |
| 3155 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3156 | int qemu_can_send_packet(VLANClientState *vc1) |
| 3157 | { |
| 3158 | VLANState *vlan = vc1->vlan; |
| 3159 | VLANClientState *vc; |
| 3160 | |
| 3161 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3162 | if (vc != vc1) { |
| 3163 | if (vc->fd_can_read && !vc->fd_can_read(vc->opaque)) |
| 3164 | return 0; |
| 3165 | } |
| 3166 | } |
| 3167 | return 1; |
| 3168 | } |
| 3169 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3170 | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
| 3171 | { |
| 3172 | VLANState *vlan = vc1->vlan; |
| 3173 | VLANClientState *vc; |
| 3174 | |
| 3175 | #if 0 |
| 3176 | printf("vlan %d send:\n", vlan->id); |
| 3177 | hex_dump(stdout, buf, size); |
| 3178 | #endif |
| 3179 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3180 | if (vc != vc1) { |
| 3181 | vc->fd_read(vc->opaque, buf, size); |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | #if defined(CONFIG_SLIRP) |
| 3187 | |
| 3188 | /* slirp network adapter */ |
| 3189 | |
| 3190 | static int slirp_inited; |
| 3191 | static VLANClientState *slirp_vc; |
| 3192 | |
| 3193 | int slirp_can_output(void) |
| 3194 | { |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3195 | return !slirp_vc || qemu_can_send_packet(slirp_vc); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 3199 | { |
| 3200 | #if 0 |
| 3201 | printf("slirp output:\n"); |
| 3202 | hex_dump(stdout, pkt, pkt_len); |
| 3203 | #endif |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3204 | if (!slirp_vc) |
| 3205 | return; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3206 | qemu_send_packet(slirp_vc, pkt, pkt_len); |
| 3207 | } |
| 3208 | |
| 3209 | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
| 3210 | { |
| 3211 | #if 0 |
| 3212 | printf("slirp input:\n"); |
| 3213 | hex_dump(stdout, buf, size); |
| 3214 | #endif |
| 3215 | slirp_input(buf, size); |
| 3216 | } |
| 3217 | |
| 3218 | static int net_slirp_init(VLANState *vlan) |
| 3219 | { |
| 3220 | if (!slirp_inited) { |
| 3221 | slirp_inited = 1; |
| 3222 | slirp_init(); |
| 3223 | } |
| 3224 | slirp_vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3225 | slirp_receive, NULL, NULL); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3226 | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3230 | static void net_slirp_redir(const char *redir_str) |
| 3231 | { |
| 3232 | int is_udp; |
| 3233 | char buf[256], *r; |
| 3234 | const char *p; |
| 3235 | struct in_addr guest_addr; |
| 3236 | int host_port, guest_port; |
| 3237 | |
| 3238 | if (!slirp_inited) { |
| 3239 | slirp_inited = 1; |
| 3240 | slirp_init(); |
| 3241 | } |
| 3242 | |
| 3243 | p = redir_str; |
| 3244 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3245 | goto fail; |
| 3246 | if (!strcmp(buf, "tcp")) { |
| 3247 | is_udp = 0; |
| 3248 | } else if (!strcmp(buf, "udp")) { |
| 3249 | is_udp = 1; |
| 3250 | } else { |
| 3251 | goto fail; |
| 3252 | } |
| 3253 | |
| 3254 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3255 | goto fail; |
| 3256 | host_port = strtol(buf, &r, 0); |
| 3257 | if (r == buf) |
| 3258 | goto fail; |
| 3259 | |
| 3260 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3261 | goto fail; |
| 3262 | if (buf[0] == '\0') { |
| 3263 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 3264 | } |
| 3265 | if (!inet_aton(buf, &guest_addr)) |
| 3266 | goto fail; |
| 3267 | |
| 3268 | guest_port = strtol(p, &r, 0); |
| 3269 | if (r == p) |
| 3270 | goto fail; |
| 3271 | |
| 3272 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 3273 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 3274 | exit(1); |
| 3275 | } |
| 3276 | return; |
| 3277 | fail: |
| 3278 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 3279 | exit(1); |
| 3280 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3281 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3282 | #ifndef _WIN32 |
| 3283 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3284 | char smb_dir[1024]; |
| 3285 | |
| 3286 | static void smb_exit(void) |
| 3287 | { |
| 3288 | DIR *d; |
| 3289 | struct dirent *de; |
| 3290 | char filename[1024]; |
| 3291 | |
| 3292 | /* erase all the files in the directory */ |
| 3293 | d = opendir(smb_dir); |
| 3294 | for(;;) { |
| 3295 | de = readdir(d); |
| 3296 | if (!de) |
| 3297 | break; |
| 3298 | if (strcmp(de->d_name, ".") != 0 && |
| 3299 | strcmp(de->d_name, "..") != 0) { |
| 3300 | snprintf(filename, sizeof(filename), "%s/%s", |
| 3301 | smb_dir, de->d_name); |
| 3302 | unlink(filename); |
| 3303 | } |
| 3304 | } |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3305 | closedir(d); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3306 | rmdir(smb_dir); |
| 3307 | } |
| 3308 | |
| 3309 | /* automatic user mode samba server configuration */ |
| 3310 | void net_slirp_smb(const char *exported_dir) |
| 3311 | { |
| 3312 | char smb_conf[1024]; |
| 3313 | char smb_cmdline[1024]; |
| 3314 | FILE *f; |
| 3315 | |
| 3316 | if (!slirp_inited) { |
| 3317 | slirp_inited = 1; |
| 3318 | slirp_init(); |
| 3319 | } |
| 3320 | |
| 3321 | /* XXX: better tmp dir construction */ |
| 3322 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 3323 | if (mkdir(smb_dir, 0700) < 0) { |
| 3324 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 3325 | exit(1); |
| 3326 | } |
| 3327 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
| 3328 | |
| 3329 | f = fopen(smb_conf, "w"); |
| 3330 | if (!f) { |
| 3331 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 3332 | exit(1); |
| 3333 | } |
| 3334 | fprintf(f, |
| 3335 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3336 | "private dir=%s\n" |
| 3337 | "smb ports=0\n" |
| 3338 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3339 | "pid directory=%s\n" |
| 3340 | "lock directory=%s\n" |
| 3341 | "log file=%s/log.smbd\n" |
| 3342 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3343 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3344 | "[qemu]\n" |
| 3345 | "path=%s\n" |
| 3346 | "read only=no\n" |
| 3347 | "guest ok=yes\n", |
| 3348 | smb_dir, |
| 3349 | smb_dir, |
| 3350 | smb_dir, |
| 3351 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3352 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3353 | exported_dir |
| 3354 | ); |
| 3355 | fclose(f); |
| 3356 | atexit(smb_exit); |
| 3357 | |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 3358 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 3359 | SMBD_COMMAND, smb_conf); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3360 | |
| 3361 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 3362 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3363 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3364 | #endif /* !defined(_WIN32) */ |
| 3365 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3366 | #endif /* CONFIG_SLIRP */ |
| 3367 | |
| 3368 | #if !defined(_WIN32) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3369 | |
| 3370 | typedef struct TAPState { |
| 3371 | VLANClientState *vc; |
| 3372 | int fd; |
| 3373 | } TAPState; |
| 3374 | |
| 3375 | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
| 3376 | { |
| 3377 | TAPState *s = opaque; |
| 3378 | int ret; |
| 3379 | for(;;) { |
| 3380 | ret = write(s->fd, buf, size); |
| 3381 | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 3382 | } else { |
| 3383 | break; |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | static void tap_send(void *opaque) |
| 3389 | { |
| 3390 | TAPState *s = opaque; |
| 3391 | uint8_t buf[4096]; |
| 3392 | int size; |
| 3393 | |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3394 | #ifdef __sun__ |
| 3395 | struct strbuf sbuf; |
| 3396 | int f = 0; |
| 3397 | sbuf.maxlen = sizeof(buf); |
| 3398 | sbuf.buf = buf; |
| 3399 | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
| 3400 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3401 | size = read(s->fd, buf, sizeof(buf)); |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3402 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3403 | if (size > 0) { |
| 3404 | qemu_send_packet(s->vc, buf, size); |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | /* fd support */ |
| 3409 | |
| 3410 | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
| 3411 | { |
| 3412 | TAPState *s; |
| 3413 | |
| 3414 | s = qemu_mallocz(sizeof(TAPState)); |
| 3415 | if (!s) |
| 3416 | return NULL; |
| 3417 | s->fd = fd; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3418 | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3419 | qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
| 3420 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
| 3421 | return s; |
| 3422 | } |
| 3423 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3424 | #ifdef _BSD |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3425 | static int tap_open(char *ifname, int ifname_size) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3426 | { |
| 3427 | int fd; |
| 3428 | char *dev; |
| 3429 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3430 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3431 | fd = open("/dev/tap", O_RDWR); |
| 3432 | if (fd < 0) { |
| 3433 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 3434 | return -1; |
| 3435 | } |
| 3436 | |
| 3437 | fstat(fd, &s); |
| 3438 | dev = devname(s.st_rdev, S_IFCHR); |
| 3439 | pstrcpy(ifname, ifname_size, dev); |
| 3440 | |
| 3441 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3442 | return fd; |
| 3443 | } |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3444 | #elif defined(__sun__) |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3445 | #define TUNNEWPPA (('T'<<16) | 0x0001) |
| 3446 | /* |
| 3447 | * Allocate TAP device, returns opened fd. |
| 3448 | * Stores dev name in the first arg(must be large enough). |
| 3449 | */ |
| 3450 | int tap_alloc(char *dev) |
| 3451 | { |
| 3452 | int tap_fd, if_fd, ppa = -1; |
| 3453 | static int ip_fd = 0; |
| 3454 | char *ptr; |
| 3455 | |
| 3456 | static int arp_fd = 0; |
| 3457 | int ip_muxid, arp_muxid; |
| 3458 | struct strioctl strioc_if, strioc_ppa; |
| 3459 | int link_type = I_PLINK;; |
| 3460 | struct lifreq ifr; |
| 3461 | char actual_name[32] = ""; |
| 3462 | |
| 3463 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3464 | |
| 3465 | if( *dev ){ |
| 3466 | ptr = dev; |
| 3467 | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
| 3468 | ppa = atoi(ptr); |
| 3469 | } |
| 3470 | |
| 3471 | /* Check if IP device was opened */ |
| 3472 | if( ip_fd ) |
| 3473 | close(ip_fd); |
| 3474 | |
| 3475 | if( (ip_fd = open("/dev/udp", O_RDWR, 0)) < 0){ |
| 3476 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); |
| 3477 | return -1; |
| 3478 | } |
| 3479 | |
| 3480 | if( (tap_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3481 | syslog(LOG_ERR, "Can't open /dev/tap"); |
| 3482 | return -1; |
| 3483 | } |
| 3484 | |
| 3485 | /* Assign a new PPA and get its unit number. */ |
| 3486 | strioc_ppa.ic_cmd = TUNNEWPPA; |
| 3487 | strioc_ppa.ic_timout = 0; |
| 3488 | strioc_ppa.ic_len = sizeof(ppa); |
| 3489 | strioc_ppa.ic_dp = (char *)&ppa; |
| 3490 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
| 3491 | syslog (LOG_ERR, "Can't assign new interface"); |
| 3492 | |
| 3493 | if( (if_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3494 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); |
| 3495 | return -1; |
| 3496 | } |
| 3497 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
| 3498 | syslog(LOG_ERR, "Can't push IP module"); |
| 3499 | return -1; |
| 3500 | } |
| 3501 | |
| 3502 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
| 3503 | syslog(LOG_ERR, "Can't get flags\n"); |
| 3504 | |
| 3505 | snprintf (actual_name, 32, "tap%d", ppa); |
| 3506 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3507 | |
| 3508 | ifr.lifr_ppa = ppa; |
| 3509 | /* Assign ppa according to the unit number returned by tun device */ |
| 3510 | |
| 3511 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
| 3512 | syslog (LOG_ERR, "Can't set PPA %d", ppa); |
| 3513 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
| 3514 | syslog (LOG_ERR, "Can't get flags\n"); |
| 3515 | /* Push arp module to if_fd */ |
| 3516 | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
| 3517 | syslog (LOG_ERR, "Can't push ARP module (2)"); |
| 3518 | |
| 3519 | /* Push arp module to ip_fd */ |
| 3520 | if (ioctl (ip_fd, I_POP, NULL) < 0) |
| 3521 | syslog (LOG_ERR, "I_POP failed\n"); |
| 3522 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
| 3523 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); |
| 3524 | /* Open arp_fd */ |
| 3525 | if ((arp_fd = open ("/dev/tap", O_RDWR, 0)) < 0) |
| 3526 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
| 3527 | |
| 3528 | /* Set ifname to arp */ |
| 3529 | strioc_if.ic_cmd = SIOCSLIFNAME; |
| 3530 | strioc_if.ic_timout = 0; |
| 3531 | strioc_if.ic_len = sizeof(ifr); |
| 3532 | strioc_if.ic_dp = (char *)𝔦 |
| 3533 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
| 3534 | syslog (LOG_ERR, "Can't set ifname to arp\n"); |
| 3535 | } |
| 3536 | |
| 3537 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
| 3538 | syslog(LOG_ERR, "Can't link TAP device to IP"); |
| 3539 | return -1; |
| 3540 | } |
| 3541 | |
| 3542 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
| 3543 | syslog (LOG_ERR, "Can't link TAP device to ARP"); |
| 3544 | |
| 3545 | close (if_fd); |
| 3546 | |
| 3547 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3548 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3549 | ifr.lifr_ip_muxid = ip_muxid; |
| 3550 | ifr.lifr_arp_muxid = arp_muxid; |
| 3551 | |
| 3552 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
| 3553 | { |
| 3554 | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
| 3555 | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
| 3556 | syslog (LOG_ERR, "Can't set multiplexor id"); |
| 3557 | } |
| 3558 | |
| 3559 | sprintf(dev, "tap%d", ppa); |
| 3560 | return tap_fd; |
| 3561 | } |
| 3562 | |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3563 | static int tap_open(char *ifname, int ifname_size) |
| 3564 | { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3565 | char dev[10]=""; |
| 3566 | int fd; |
| 3567 | if( (fd = tap_alloc(dev)) < 0 ){ |
| 3568 | fprintf(stderr, "Cannot allocate TAP device\n"); |
| 3569 | return -1; |
| 3570 | } |
| 3571 | pstrcpy(ifname, ifname_size, dev); |
| 3572 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3573 | return fd; |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3574 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3575 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3576 | static int tap_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3577 | { |
| 3578 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3579 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3580 | |
| 3581 | fd = open("/dev/net/tun", O_RDWR); |
| 3582 | if (fd < 0) { |
| 3583 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 3584 | return -1; |
| 3585 | } |
| 3586 | memset(&ifr, 0, sizeof(ifr)); |
| 3587 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3588 | if (ifname[0] != '\0') |
| 3589 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 3590 | else |
| 3591 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3592 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 3593 | if (ret != 0) { |
| 3594 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 3595 | close(fd); |
| 3596 | return -1; |
| 3597 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3598 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3599 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3600 | return fd; |
| 3601 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3602 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3603 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3604 | static int net_tap_init(VLANState *vlan, const char *ifname1, |
| 3605 | const char *setup_script) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3606 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3607 | TAPState *s; |
| 3608 | int pid, status, fd; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3609 | char *args[3]; |
| 3610 | char **parg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3611 | char ifname[128]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3612 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3613 | if (ifname1 != NULL) |
| 3614 | pstrcpy(ifname, sizeof(ifname), ifname1); |
| 3615 | else |
| 3616 | ifname[0] = '\0'; |
| 3617 | fd = tap_open(ifname, sizeof(ifname)); |
| 3618 | if (fd < 0) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3619 | return -1; |
| 3620 | |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 3621 | if (!setup_script || !strcmp(setup_script, "no")) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3622 | setup_script = ""; |
| 3623 | if (setup_script[0] != '\0') { |
| 3624 | /* try to launch network init script */ |
| 3625 | pid = fork(); |
| 3626 | if (pid >= 0) { |
| 3627 | if (pid == 0) { |
ths | 50d3eea | 2007-03-19 16:36:43 +0000 | [diff] [blame] | 3628 | int open_max = sysconf (_SC_OPEN_MAX), i; |
| 3629 | for (i = 0; i < open_max; i++) |
| 3630 | if (i != STDIN_FILENO && |
| 3631 | i != STDOUT_FILENO && |
| 3632 | i != STDERR_FILENO && |
| 3633 | i != fd) |
| 3634 | close(i); |
| 3635 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3636 | parg = args; |
| 3637 | *parg++ = (char *)setup_script; |
| 3638 | *parg++ = ifname; |
| 3639 | *parg++ = NULL; |
| 3640 | execv(setup_script, args); |
bellard | 4a38940 | 2005-11-26 20:10:07 +0000 | [diff] [blame] | 3641 | _exit(1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3642 | } |
| 3643 | while (waitpid(pid, &status, 0) != pid); |
| 3644 | if (!WIFEXITED(status) || |
| 3645 | WEXITSTATUS(status) != 0) { |
| 3646 | fprintf(stderr, "%s: could not launch network script\n", |
| 3647 | setup_script); |
| 3648 | return -1; |
| 3649 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3650 | } |
| 3651 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3652 | s = net_tap_fd_init(vlan, fd); |
| 3653 | if (!s) |
| 3654 | return -1; |
| 3655 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3656 | "tap: ifname=%s setup_script=%s", ifname, setup_script); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3657 | return 0; |
| 3658 | } |
| 3659 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3660 | #endif /* !_WIN32 */ |
| 3661 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3662 | /* network connection */ |
| 3663 | typedef struct NetSocketState { |
| 3664 | VLANClientState *vc; |
| 3665 | int fd; |
| 3666 | int state; /* 0 = getting length, 1 = getting data */ |
| 3667 | int index; |
| 3668 | int packet_len; |
| 3669 | uint8_t buf[4096]; |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3670 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3671 | } NetSocketState; |
| 3672 | |
| 3673 | typedef struct NetSocketListenState { |
| 3674 | VLANState *vlan; |
| 3675 | int fd; |
| 3676 | } NetSocketListenState; |
| 3677 | |
| 3678 | /* XXX: we consider we can send the whole packet without blocking */ |
| 3679 | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3680 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3681 | NetSocketState *s = opaque; |
| 3682 | uint32_t len; |
| 3683 | len = htonl(size); |
| 3684 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3685 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 3686 | send_all(s->fd, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3689 | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
| 3690 | { |
| 3691 | NetSocketState *s = opaque; |
| 3692 | sendto(s->fd, buf, size, 0, |
| 3693 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 3694 | } |
| 3695 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3696 | static void net_socket_send(void *opaque) |
| 3697 | { |
| 3698 | NetSocketState *s = opaque; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3699 | int l, size, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3700 | uint8_t buf1[4096]; |
| 3701 | const uint8_t *buf; |
| 3702 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3703 | size = recv(s->fd, buf1, sizeof(buf1), 0); |
| 3704 | if (size < 0) { |
| 3705 | err = socket_error(); |
| 3706 | if (err != EWOULDBLOCK) |
| 3707 | goto eoc; |
| 3708 | } else if (size == 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3709 | /* end of connection */ |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3710 | eoc: |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3711 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3712 | closesocket(s->fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3713 | return; |
| 3714 | } |
| 3715 | buf = buf1; |
| 3716 | while (size > 0) { |
| 3717 | /* reassemble a packet from the network */ |
| 3718 | switch(s->state) { |
| 3719 | case 0: |
| 3720 | l = 4 - s->index; |
| 3721 | if (l > size) |
| 3722 | l = size; |
| 3723 | memcpy(s->buf + s->index, buf, l); |
| 3724 | buf += l; |
| 3725 | size -= l; |
| 3726 | s->index += l; |
| 3727 | if (s->index == 4) { |
| 3728 | /* got length */ |
| 3729 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 3730 | s->index = 0; |
| 3731 | s->state = 1; |
| 3732 | } |
| 3733 | break; |
| 3734 | case 1: |
| 3735 | l = s->packet_len - s->index; |
| 3736 | if (l > size) |
| 3737 | l = size; |
| 3738 | memcpy(s->buf + s->index, buf, l); |
| 3739 | s->index += l; |
| 3740 | buf += l; |
| 3741 | size -= l; |
| 3742 | if (s->index >= s->packet_len) { |
| 3743 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 3744 | s->index = 0; |
| 3745 | s->state = 0; |
| 3746 | } |
| 3747 | break; |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3752 | static void net_socket_send_dgram(void *opaque) |
| 3753 | { |
| 3754 | NetSocketState *s = opaque; |
| 3755 | int size; |
| 3756 | |
| 3757 | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 3758 | if (size < 0) |
| 3759 | return; |
| 3760 | if (size == 0) { |
| 3761 | /* end of connection */ |
| 3762 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 3763 | return; |
| 3764 | } |
| 3765 | qemu_send_packet(s->vc, s->buf, size); |
| 3766 | } |
| 3767 | |
| 3768 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 3769 | { |
| 3770 | struct ip_mreq imr; |
| 3771 | int fd; |
| 3772 | int val, ret; |
| 3773 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 3774 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3775 | inet_ntoa(mcastaddr->sin_addr), |
| 3776 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3777 | return -1; |
| 3778 | |
| 3779 | } |
| 3780 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 3781 | if (fd < 0) { |
| 3782 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 3783 | return -1; |
| 3784 | } |
| 3785 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3786 | val = 1; |
| 3787 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 3788 | (const char *)&val, sizeof(val)); |
| 3789 | if (ret < 0) { |
| 3790 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 3791 | goto fail; |
| 3792 | } |
| 3793 | |
| 3794 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 3795 | if (ret < 0) { |
| 3796 | perror("bind"); |
| 3797 | goto fail; |
| 3798 | } |
| 3799 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3800 | /* Add host to multicast group */ |
| 3801 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 3802 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 3803 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3804 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 3805 | (const char *)&imr, sizeof(struct ip_mreq)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3806 | if (ret < 0) { |
| 3807 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 3808 | goto fail; |
| 3809 | } |
| 3810 | |
| 3811 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 3812 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3813 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 3814 | (const char *)&val, sizeof(val)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3815 | if (ret < 0) { |
| 3816 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 3817 | goto fail; |
| 3818 | } |
| 3819 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3820 | socket_set_nonblock(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3821 | return fd; |
| 3822 | fail: |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3823 | if (fd >= 0) |
| 3824 | closesocket(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3825 | return -1; |
| 3826 | } |
| 3827 | |
| 3828 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
| 3829 | int is_connected) |
| 3830 | { |
| 3831 | struct sockaddr_in saddr; |
| 3832 | int newfd; |
| 3833 | socklen_t saddr_len; |
| 3834 | NetSocketState *s; |
| 3835 | |
| 3836 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 3837 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 3838 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 3839 | */ |
| 3840 | |
| 3841 | if (is_connected) { |
| 3842 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 3843 | /* must be bound */ |
| 3844 | if (saddr.sin_addr.s_addr==0) { |
| 3845 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 3846 | fd); |
| 3847 | return NULL; |
| 3848 | } |
| 3849 | /* clone dgram socket */ |
| 3850 | newfd = net_socket_mcast_create(&saddr); |
| 3851 | if (newfd < 0) { |
| 3852 | /* error already reported by net_socket_mcast_create() */ |
| 3853 | close(fd); |
| 3854 | return NULL; |
| 3855 | } |
| 3856 | /* clone newfd to fd, close newfd */ |
| 3857 | dup2(newfd, fd); |
| 3858 | close(newfd); |
| 3859 | |
| 3860 | } else { |
| 3861 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 3862 | fd, strerror(errno)); |
| 3863 | return NULL; |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3868 | if (!s) |
| 3869 | return NULL; |
| 3870 | s->fd = fd; |
| 3871 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3872 | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3873 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 3874 | |
| 3875 | /* mcast: save bound address as dst */ |
| 3876 | if (is_connected) s->dgram_dst=saddr; |
| 3877 | |
| 3878 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3879 | "socket: fd=%d (%s mcast=%s:%d)", |
| 3880 | fd, is_connected? "cloned" : "", |
| 3881 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3882 | return s; |
| 3883 | } |
| 3884 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3885 | static void net_socket_connect(void *opaque) |
| 3886 | { |
| 3887 | NetSocketState *s = opaque; |
| 3888 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 3889 | } |
| 3890 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3891 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3892 | int is_connected) |
| 3893 | { |
| 3894 | NetSocketState *s; |
| 3895 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3896 | if (!s) |
| 3897 | return NULL; |
| 3898 | s->fd = fd; |
| 3899 | s->vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3900 | net_socket_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3901 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3902 | "socket: fd=%d", fd); |
| 3903 | if (is_connected) { |
| 3904 | net_socket_connect(s); |
| 3905 | } else { |
| 3906 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 3907 | } |
| 3908 | return s; |
| 3909 | } |
| 3910 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3911 | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
| 3912 | int is_connected) |
| 3913 | { |
| 3914 | int so_type=-1, optlen=sizeof(so_type); |
| 3915 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3916 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) { |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3917 | fprintf(stderr, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 3918 | return NULL; |
| 3919 | } |
| 3920 | switch(so_type) { |
| 3921 | case SOCK_DGRAM: |
| 3922 | return net_socket_fd_init_dgram(vlan, fd, is_connected); |
| 3923 | case SOCK_STREAM: |
| 3924 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3925 | default: |
| 3926 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 3927 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
| 3928 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3929 | } |
| 3930 | return NULL; |
| 3931 | } |
| 3932 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3933 | static void net_socket_accept(void *opaque) |
| 3934 | { |
| 3935 | NetSocketListenState *s = opaque; |
| 3936 | NetSocketState *s1; |
| 3937 | struct sockaddr_in saddr; |
| 3938 | socklen_t len; |
| 3939 | int fd; |
| 3940 | |
| 3941 | for(;;) { |
| 3942 | len = sizeof(saddr); |
| 3943 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 3944 | if (fd < 0 && errno != EINTR) { |
| 3945 | return; |
| 3946 | } else if (fd >= 0) { |
| 3947 | break; |
| 3948 | } |
| 3949 | } |
| 3950 | s1 = net_socket_fd_init(s->vlan, fd, 1); |
| 3951 | if (!s1) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3952 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3953 | } else { |
| 3954 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 3955 | "socket: connection from %s:%d", |
| 3956 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
| 3961 | { |
| 3962 | NetSocketListenState *s; |
| 3963 | int fd, val, ret; |
| 3964 | struct sockaddr_in saddr; |
| 3965 | |
| 3966 | if (parse_host_port(&saddr, host_str) < 0) |
| 3967 | return -1; |
| 3968 | |
| 3969 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
| 3970 | if (!s) |
| 3971 | return -1; |
| 3972 | |
| 3973 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3974 | if (fd < 0) { |
| 3975 | perror("socket"); |
| 3976 | return -1; |
| 3977 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3978 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3979 | |
| 3980 | /* allow fast reuse */ |
| 3981 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3982 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3983 | |
| 3984 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 3985 | if (ret < 0) { |
| 3986 | perror("bind"); |
| 3987 | return -1; |
| 3988 | } |
| 3989 | ret = listen(fd, 0); |
| 3990 | if (ret < 0) { |
| 3991 | perror("listen"); |
| 3992 | return -1; |
| 3993 | } |
| 3994 | s->vlan = vlan; |
| 3995 | s->fd = fd; |
| 3996 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 3997 | return 0; |
| 3998 | } |
| 3999 | |
| 4000 | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
| 4001 | { |
| 4002 | NetSocketState *s; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4003 | int fd, connected, ret, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4004 | struct sockaddr_in saddr; |
| 4005 | |
| 4006 | if (parse_host_port(&saddr, host_str) < 0) |
| 4007 | return -1; |
| 4008 | |
| 4009 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 4010 | if (fd < 0) { |
| 4011 | perror("socket"); |
| 4012 | return -1; |
| 4013 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4014 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4015 | |
| 4016 | connected = 0; |
| 4017 | for(;;) { |
| 4018 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 4019 | if (ret < 0) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4020 | err = socket_error(); |
| 4021 | if (err == EINTR || err == EWOULDBLOCK) { |
| 4022 | } else if (err == EINPROGRESS) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4023 | break; |
ths | f5b1226 | 2007-03-25 15:58:03 +0000 | [diff] [blame] | 4024 | #ifdef _WIN32 |
| 4025 | } else if (err == WSAEALREADY) { |
| 4026 | break; |
| 4027 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4028 | } else { |
| 4029 | perror("connect"); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4030 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4031 | return -1; |
| 4032 | } |
| 4033 | } else { |
| 4034 | connected = 1; |
| 4035 | break; |
| 4036 | } |
| 4037 | } |
| 4038 | s = net_socket_fd_init(vlan, fd, connected); |
| 4039 | if (!s) |
| 4040 | return -1; |
| 4041 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4042 | "socket: connect to %s:%d", |
| 4043 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4044 | return 0; |
| 4045 | } |
| 4046 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4047 | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
| 4048 | { |
| 4049 | NetSocketState *s; |
| 4050 | int fd; |
| 4051 | struct sockaddr_in saddr; |
| 4052 | |
| 4053 | if (parse_host_port(&saddr, host_str) < 0) |
| 4054 | return -1; |
| 4055 | |
| 4056 | |
| 4057 | fd = net_socket_mcast_create(&saddr); |
| 4058 | if (fd < 0) |
| 4059 | return -1; |
| 4060 | |
| 4061 | s = net_socket_fd_init(vlan, fd, 0); |
| 4062 | if (!s) |
| 4063 | return -1; |
| 4064 | |
| 4065 | s->dgram_dst = saddr; |
| 4066 | |
| 4067 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4068 | "socket: mcast=%s:%d", |
| 4069 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4070 | return 0; |
| 4071 | |
| 4072 | } |
| 4073 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4074 | static int get_param_value(char *buf, int buf_size, |
| 4075 | const char *tag, const char *str) |
| 4076 | { |
| 4077 | const char *p; |
| 4078 | char *q; |
| 4079 | char option[128]; |
| 4080 | |
| 4081 | p = str; |
| 4082 | for(;;) { |
| 4083 | q = option; |
| 4084 | while (*p != '\0' && *p != '=') { |
| 4085 | if ((q - option) < sizeof(option) - 1) |
| 4086 | *q++ = *p; |
| 4087 | p++; |
| 4088 | } |
| 4089 | *q = '\0'; |
| 4090 | if (*p != '=') |
| 4091 | break; |
| 4092 | p++; |
| 4093 | if (!strcmp(tag, option)) { |
| 4094 | q = buf; |
| 4095 | while (*p != '\0' && *p != ',') { |
| 4096 | if ((q - buf) < buf_size - 1) |
| 4097 | *q++ = *p; |
| 4098 | p++; |
| 4099 | } |
| 4100 | *q = '\0'; |
| 4101 | return q - buf; |
| 4102 | } else { |
| 4103 | while (*p != '\0' && *p != ',') { |
| 4104 | p++; |
| 4105 | } |
| 4106 | } |
| 4107 | if (*p != ',') |
| 4108 | break; |
| 4109 | p++; |
| 4110 | } |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 4114 | static int net_client_init(const char *str) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4115 | { |
| 4116 | const char *p; |
| 4117 | char *q; |
| 4118 | char device[64]; |
| 4119 | char buf[1024]; |
| 4120 | int vlan_id, ret; |
| 4121 | VLANState *vlan; |
| 4122 | |
| 4123 | p = str; |
| 4124 | q = device; |
| 4125 | while (*p != '\0' && *p != ',') { |
| 4126 | if ((q - device) < sizeof(device) - 1) |
| 4127 | *q++ = *p; |
| 4128 | p++; |
| 4129 | } |
| 4130 | *q = '\0'; |
| 4131 | if (*p == ',') |
| 4132 | p++; |
| 4133 | vlan_id = 0; |
| 4134 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
| 4135 | vlan_id = strtol(buf, NULL, 0); |
| 4136 | } |
| 4137 | vlan = qemu_find_vlan(vlan_id); |
| 4138 | if (!vlan) { |
| 4139 | fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
| 4140 | return -1; |
| 4141 | } |
| 4142 | if (!strcmp(device, "nic")) { |
| 4143 | NICInfo *nd; |
| 4144 | uint8_t *macaddr; |
| 4145 | |
| 4146 | if (nb_nics >= MAX_NICS) { |
| 4147 | fprintf(stderr, "Too Many NICs\n"); |
| 4148 | return -1; |
| 4149 | } |
| 4150 | nd = &nd_table[nb_nics]; |
| 4151 | macaddr = nd->macaddr; |
| 4152 | macaddr[0] = 0x52; |
| 4153 | macaddr[1] = 0x54; |
| 4154 | macaddr[2] = 0x00; |
| 4155 | macaddr[3] = 0x12; |
| 4156 | macaddr[4] = 0x34; |
| 4157 | macaddr[5] = 0x56 + nb_nics; |
| 4158 | |
| 4159 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
| 4160 | if (parse_macaddr(macaddr, buf) < 0) { |
| 4161 | fprintf(stderr, "invalid syntax for ethernet address\n"); |
| 4162 | return -1; |
| 4163 | } |
| 4164 | } |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 4165 | if (get_param_value(buf, sizeof(buf), "model", p)) { |
| 4166 | nd->model = strdup(buf); |
| 4167 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4168 | nd->vlan = vlan; |
| 4169 | nb_nics++; |
| 4170 | ret = 0; |
| 4171 | } else |
| 4172 | if (!strcmp(device, "none")) { |
| 4173 | /* does nothing. It is needed to signal that no network cards |
| 4174 | are wanted */ |
| 4175 | ret = 0; |
| 4176 | } else |
| 4177 | #ifdef CONFIG_SLIRP |
| 4178 | if (!strcmp(device, "user")) { |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4179 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
bellard | 3f423c9 | 2006-04-30 21:34:15 +0000 | [diff] [blame] | 4180 | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4181 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4182 | ret = net_slirp_init(vlan); |
| 4183 | } else |
| 4184 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 4185 | #ifdef _WIN32 |
| 4186 | if (!strcmp(device, "tap")) { |
| 4187 | char ifname[64]; |
| 4188 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4189 | fprintf(stderr, "tap: no interface name\n"); |
| 4190 | return -1; |
| 4191 | } |
| 4192 | ret = tap_win32_init(vlan, ifname); |
| 4193 | } else |
| 4194 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4195 | if (!strcmp(device, "tap")) { |
| 4196 | char ifname[64]; |
| 4197 | char setup_script[1024]; |
| 4198 | int fd; |
| 4199 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4200 | fd = strtol(buf, NULL, 0); |
| 4201 | ret = -1; |
| 4202 | if (net_tap_fd_init(vlan, fd)) |
| 4203 | ret = 0; |
| 4204 | } else { |
bellard | bf8c534 | 2007-01-09 19:44:41 +0000 | [diff] [blame] | 4205 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4206 | ifname[0] = '\0'; |
| 4207 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4208 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
| 4209 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
| 4210 | } |
| 4211 | ret = net_tap_init(vlan, ifname, setup_script); |
| 4212 | } |
| 4213 | } else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4214 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4215 | if (!strcmp(device, "socket")) { |
| 4216 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4217 | int fd; |
| 4218 | fd = strtol(buf, NULL, 0); |
| 4219 | ret = -1; |
| 4220 | if (net_socket_fd_init(vlan, fd, 1)) |
| 4221 | ret = 0; |
| 4222 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
| 4223 | ret = net_socket_listen_init(vlan, buf); |
| 4224 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
| 4225 | ret = net_socket_connect_init(vlan, buf); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4226 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
| 4227 | ret = net_socket_mcast_init(vlan, buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4228 | } else { |
| 4229 | fprintf(stderr, "Unknown socket options: %s\n", p); |
| 4230 | return -1; |
| 4231 | } |
| 4232 | } else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4233 | { |
| 4234 | fprintf(stderr, "Unknown network device: %s\n", device); |
| 4235 | return -1; |
| 4236 | } |
| 4237 | if (ret < 0) { |
| 4238 | fprintf(stderr, "Could not initialize device '%s'\n", device); |
| 4239 | } |
| 4240 | |
| 4241 | return ret; |
| 4242 | } |
| 4243 | |
| 4244 | void do_info_network(void) |
| 4245 | { |
| 4246 | VLANState *vlan; |
| 4247 | VLANClientState *vc; |
| 4248 | |
| 4249 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 4250 | term_printf("VLAN %d devices:\n", vlan->id); |
| 4251 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
| 4252 | term_printf(" %s\n", vc->info_str); |
| 4253 | } |
| 4254 | } |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 4255 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4256 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4257 | /* USB devices */ |
| 4258 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4259 | static USBPort *used_usb_ports; |
| 4260 | static USBPort *free_usb_ports; |
| 4261 | |
| 4262 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 4263 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 4264 | usb_attachfn attach) |
| 4265 | { |
| 4266 | port->opaque = opaque; |
| 4267 | port->index = index; |
| 4268 | port->attach = attach; |
| 4269 | port->next = free_usb_ports; |
| 4270 | free_usb_ports = port; |
| 4271 | } |
| 4272 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4273 | static int usb_device_add(const char *devname) |
| 4274 | { |
| 4275 | const char *p; |
| 4276 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4277 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4278 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4279 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4280 | return -1; |
| 4281 | |
| 4282 | if (strstart(devname, "host:", &p)) { |
| 4283 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4284 | } else if (!strcmp(devname, "mouse")) { |
| 4285 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 4286 | } else if (!strcmp(devname, "tablet")) { |
| 4287 | dev = usb_tablet_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 4288 | } else if (strstart(devname, "disk:", &p)) { |
| 4289 | dev = usb_msd_init(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4290 | } else { |
| 4291 | return -1; |
| 4292 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4293 | if (!dev) |
| 4294 | return -1; |
| 4295 | |
| 4296 | /* Find a USB port to add the device to. */ |
| 4297 | port = free_usb_ports; |
| 4298 | if (!port->next) { |
| 4299 | USBDevice *hub; |
| 4300 | |
| 4301 | /* Create a new hub and chain it on. */ |
| 4302 | free_usb_ports = NULL; |
| 4303 | port->next = used_usb_ports; |
| 4304 | used_usb_ports = port; |
| 4305 | |
| 4306 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 4307 | usb_attach(port, hub); |
| 4308 | port = free_usb_ports; |
| 4309 | } |
| 4310 | |
| 4311 | free_usb_ports = port->next; |
| 4312 | port->next = used_usb_ports; |
| 4313 | used_usb_ports = port; |
| 4314 | usb_attach(port, dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
| 4318 | static int usb_device_del(const char *devname) |
| 4319 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4320 | USBPort *port; |
| 4321 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4322 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4323 | int bus_num, addr; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4324 | const char *p; |
| 4325 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4326 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4327 | return -1; |
| 4328 | |
| 4329 | p = strchr(devname, '.'); |
| 4330 | if (!p) |
| 4331 | return -1; |
| 4332 | bus_num = strtoul(devname, NULL, 0); |
| 4333 | addr = strtoul(p + 1, NULL, 0); |
| 4334 | if (bus_num != 0) |
| 4335 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4336 | |
| 4337 | lastp = &used_usb_ports; |
| 4338 | port = used_usb_ports; |
| 4339 | while (port && port->dev->addr != addr) { |
| 4340 | lastp = &port->next; |
| 4341 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4342 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4343 | |
| 4344 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4345 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4346 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4347 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4348 | *lastp = port->next; |
| 4349 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4350 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4351 | port->next = free_usb_ports; |
| 4352 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4353 | return 0; |
| 4354 | } |
| 4355 | |
| 4356 | void do_usb_add(const char *devname) |
| 4357 | { |
| 4358 | int ret; |
| 4359 | ret = usb_device_add(devname); |
| 4360 | if (ret < 0) |
| 4361 | term_printf("Could not add USB device '%s'\n", devname); |
| 4362 | } |
| 4363 | |
| 4364 | void do_usb_del(const char *devname) |
| 4365 | { |
| 4366 | int ret; |
| 4367 | ret = usb_device_del(devname); |
| 4368 | if (ret < 0) |
| 4369 | term_printf("Could not remove USB device '%s'\n", devname); |
| 4370 | } |
| 4371 | |
| 4372 | void usb_info(void) |
| 4373 | { |
| 4374 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4375 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4376 | const char *speed_str; |
| 4377 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4378 | if (!usb_enabled) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4379 | term_printf("USB support not enabled\n"); |
| 4380 | return; |
| 4381 | } |
| 4382 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4383 | for (port = used_usb_ports; port; port = port->next) { |
| 4384 | dev = port->dev; |
| 4385 | if (!dev) |
| 4386 | continue; |
| 4387 | switch(dev->speed) { |
| 4388 | case USB_SPEED_LOW: |
| 4389 | speed_str = "1.5"; |
| 4390 | break; |
| 4391 | case USB_SPEED_FULL: |
| 4392 | speed_str = "12"; |
| 4393 | break; |
| 4394 | case USB_SPEED_HIGH: |
| 4395 | speed_str = "480"; |
| 4396 | break; |
| 4397 | default: |
| 4398 | speed_str = "?"; |
| 4399 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4400 | } |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 4401 | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 4402 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 4406 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4407 | /* dumb display */ |
| 4408 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4409 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 4410 | { |
| 4411 | } |
| 4412 | |
| 4413 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 4414 | { |
| 4415 | } |
| 4416 | |
| 4417 | static void dumb_refresh(DisplayState *ds) |
| 4418 | { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 4419 | vga_hw_update(); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | void dumb_display_init(DisplayState *ds) |
| 4423 | { |
| 4424 | ds->data = NULL; |
| 4425 | ds->linesize = 0; |
| 4426 | ds->depth = 0; |
| 4427 | ds->dpy_update = dumb_update; |
| 4428 | ds->dpy_resize = dumb_resize; |
| 4429 | ds->dpy_refresh = dumb_refresh; |
| 4430 | } |
| 4431 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4432 | /***********************************************************/ |
| 4433 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4434 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4435 | #define MAX_IO_HANDLERS 64 |
| 4436 | |
| 4437 | typedef struct IOHandlerRecord { |
| 4438 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4439 | IOCanRWHandler *fd_read_poll; |
| 4440 | IOHandler *fd_read; |
| 4441 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4442 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4443 | void *opaque; |
| 4444 | /* temporary data */ |
| 4445 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4446 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4447 | } IOHandlerRecord; |
| 4448 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4449 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4450 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4451 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 4452 | necessary in the character devices to suppress fd_can_read(). */ |
| 4453 | int qemu_set_fd_handler2(int fd, |
| 4454 | IOCanRWHandler *fd_read_poll, |
| 4455 | IOHandler *fd_read, |
| 4456 | IOHandler *fd_write, |
| 4457 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4458 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4459 | IOHandlerRecord **pioh, *ioh; |
| 4460 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4461 | if (!fd_read && !fd_write) { |
| 4462 | pioh = &first_io_handler; |
| 4463 | for(;;) { |
| 4464 | ioh = *pioh; |
| 4465 | if (ioh == NULL) |
| 4466 | break; |
| 4467 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4468 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4469 | break; |
| 4470 | } |
| 4471 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4472 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4473 | } else { |
| 4474 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 4475 | if (ioh->fd == fd) |
| 4476 | goto found; |
| 4477 | } |
| 4478 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 4479 | if (!ioh) |
| 4480 | return -1; |
| 4481 | ioh->next = first_io_handler; |
| 4482 | first_io_handler = ioh; |
| 4483 | found: |
| 4484 | ioh->fd = fd; |
| 4485 | ioh->fd_read_poll = fd_read_poll; |
| 4486 | ioh->fd_read = fd_read; |
| 4487 | ioh->fd_write = fd_write; |
| 4488 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4489 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4490 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4491 | return 0; |
| 4492 | } |
| 4493 | |
| 4494 | int qemu_set_fd_handler(int fd, |
| 4495 | IOHandler *fd_read, |
| 4496 | IOHandler *fd_write, |
| 4497 | void *opaque) |
| 4498 | { |
| 4499 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4502 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4503 | /* Polling handling */ |
| 4504 | |
| 4505 | typedef struct PollingEntry { |
| 4506 | PollingFunc *func; |
| 4507 | void *opaque; |
| 4508 | struct PollingEntry *next; |
| 4509 | } PollingEntry; |
| 4510 | |
| 4511 | static PollingEntry *first_polling_entry; |
| 4512 | |
| 4513 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 4514 | { |
| 4515 | PollingEntry **ppe, *pe; |
| 4516 | pe = qemu_mallocz(sizeof(PollingEntry)); |
| 4517 | if (!pe) |
| 4518 | return -1; |
| 4519 | pe->func = func; |
| 4520 | pe->opaque = opaque; |
| 4521 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 4522 | *ppe = pe; |
| 4523 | return 0; |
| 4524 | } |
| 4525 | |
| 4526 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 4527 | { |
| 4528 | PollingEntry **ppe, *pe; |
| 4529 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 4530 | pe = *ppe; |
| 4531 | if (pe->func == func && pe->opaque == opaque) { |
| 4532 | *ppe = pe->next; |
| 4533 | qemu_free(pe); |
| 4534 | break; |
| 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4539 | #ifdef _WIN32 |
| 4540 | /***********************************************************/ |
| 4541 | /* Wait objects support */ |
| 4542 | typedef struct WaitObjects { |
| 4543 | int num; |
| 4544 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4545 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4546 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4547 | } WaitObjects; |
| 4548 | |
| 4549 | static WaitObjects wait_objects = {0}; |
| 4550 | |
| 4551 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4552 | { |
| 4553 | WaitObjects *w = &wait_objects; |
| 4554 | |
| 4555 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 4556 | return -1; |
| 4557 | w->events[w->num] = handle; |
| 4558 | w->func[w->num] = func; |
| 4559 | w->opaque[w->num] = opaque; |
| 4560 | w->num++; |
| 4561 | return 0; |
| 4562 | } |
| 4563 | |
| 4564 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4565 | { |
| 4566 | int i, found; |
| 4567 | WaitObjects *w = &wait_objects; |
| 4568 | |
| 4569 | found = 0; |
| 4570 | for (i = 0; i < w->num; i++) { |
| 4571 | if (w->events[i] == handle) |
| 4572 | found = 1; |
| 4573 | if (found) { |
| 4574 | w->events[i] = w->events[i + 1]; |
| 4575 | w->func[i] = w->func[i + 1]; |
| 4576 | w->opaque[i] = w->opaque[i + 1]; |
| 4577 | } |
| 4578 | } |
| 4579 | if (found) |
| 4580 | w->num--; |
| 4581 | } |
| 4582 | #endif |
| 4583 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4584 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4585 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4586 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4587 | #define IO_BUF_SIZE 32768 |
| 4588 | |
| 4589 | struct QEMUFile { |
| 4590 | FILE *outfile; |
| 4591 | BlockDriverState *bs; |
| 4592 | int is_file; |
| 4593 | int is_writable; |
| 4594 | int64_t base_offset; |
| 4595 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 4596 | when reading */ |
| 4597 | int buf_index; |
| 4598 | int buf_size; /* 0 when writing */ |
| 4599 | uint8_t buf[IO_BUF_SIZE]; |
| 4600 | }; |
| 4601 | |
| 4602 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 4603 | { |
| 4604 | QEMUFile *f; |
| 4605 | |
| 4606 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4607 | if (!f) |
| 4608 | return NULL; |
| 4609 | if (!strcmp(mode, "wb")) { |
| 4610 | f->is_writable = 1; |
| 4611 | } else if (!strcmp(mode, "rb")) { |
| 4612 | f->is_writable = 0; |
| 4613 | } else { |
| 4614 | goto fail; |
| 4615 | } |
| 4616 | f->outfile = fopen(filename, mode); |
| 4617 | if (!f->outfile) |
| 4618 | goto fail; |
| 4619 | f->is_file = 1; |
| 4620 | return f; |
| 4621 | fail: |
| 4622 | if (f->outfile) |
| 4623 | fclose(f->outfile); |
| 4624 | qemu_free(f); |
| 4625 | return NULL; |
| 4626 | } |
| 4627 | |
| 4628 | QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
| 4629 | { |
| 4630 | QEMUFile *f; |
| 4631 | |
| 4632 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4633 | if (!f) |
| 4634 | return NULL; |
| 4635 | f->is_file = 0; |
| 4636 | f->bs = bs; |
| 4637 | f->is_writable = is_writable; |
| 4638 | f->base_offset = offset; |
| 4639 | return f; |
| 4640 | } |
| 4641 | |
| 4642 | void qemu_fflush(QEMUFile *f) |
| 4643 | { |
| 4644 | if (!f->is_writable) |
| 4645 | return; |
| 4646 | if (f->buf_index > 0) { |
| 4647 | if (f->is_file) { |
| 4648 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4649 | fwrite(f->buf, 1, f->buf_index, f->outfile); |
| 4650 | } else { |
| 4651 | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
| 4652 | f->buf, f->buf_index); |
| 4653 | } |
| 4654 | f->buf_offset += f->buf_index; |
| 4655 | f->buf_index = 0; |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | static void qemu_fill_buffer(QEMUFile *f) |
| 4660 | { |
| 4661 | int len; |
| 4662 | |
| 4663 | if (f->is_writable) |
| 4664 | return; |
| 4665 | if (f->is_file) { |
| 4666 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4667 | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile); |
| 4668 | if (len < 0) |
| 4669 | len = 0; |
| 4670 | } else { |
| 4671 | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
| 4672 | f->buf, IO_BUF_SIZE); |
| 4673 | if (len < 0) |
| 4674 | len = 0; |
| 4675 | } |
| 4676 | f->buf_index = 0; |
| 4677 | f->buf_size = len; |
| 4678 | f->buf_offset += len; |
| 4679 | } |
| 4680 | |
| 4681 | void qemu_fclose(QEMUFile *f) |
| 4682 | { |
| 4683 | if (f->is_writable) |
| 4684 | qemu_fflush(f); |
| 4685 | if (f->is_file) { |
| 4686 | fclose(f->outfile); |
| 4687 | } |
| 4688 | qemu_free(f); |
| 4689 | } |
| 4690 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4691 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 4692 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4693 | int l; |
| 4694 | while (size > 0) { |
| 4695 | l = IO_BUF_SIZE - f->buf_index; |
| 4696 | if (l > size) |
| 4697 | l = size; |
| 4698 | memcpy(f->buf + f->buf_index, buf, l); |
| 4699 | f->buf_index += l; |
| 4700 | buf += l; |
| 4701 | size -= l; |
| 4702 | if (f->buf_index >= IO_BUF_SIZE) |
| 4703 | qemu_fflush(f); |
| 4704 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | void qemu_put_byte(QEMUFile *f, int v) |
| 4708 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4709 | f->buf[f->buf_index++] = v; |
| 4710 | if (f->buf_index >= IO_BUF_SIZE) |
| 4711 | qemu_fflush(f); |
| 4712 | } |
| 4713 | |
| 4714 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 4715 | { |
| 4716 | int size, l; |
| 4717 | |
| 4718 | size = size1; |
| 4719 | while (size > 0) { |
| 4720 | l = f->buf_size - f->buf_index; |
| 4721 | if (l == 0) { |
| 4722 | qemu_fill_buffer(f); |
| 4723 | l = f->buf_size - f->buf_index; |
| 4724 | if (l == 0) |
| 4725 | break; |
| 4726 | } |
| 4727 | if (l > size) |
| 4728 | l = size; |
| 4729 | memcpy(buf, f->buf + f->buf_index, l); |
| 4730 | f->buf_index += l; |
| 4731 | buf += l; |
| 4732 | size -= l; |
| 4733 | } |
| 4734 | return size1 - size; |
| 4735 | } |
| 4736 | |
| 4737 | int qemu_get_byte(QEMUFile *f) |
| 4738 | { |
| 4739 | if (f->buf_index >= f->buf_size) { |
| 4740 | qemu_fill_buffer(f); |
| 4741 | if (f->buf_index >= f->buf_size) |
| 4742 | return 0; |
| 4743 | } |
| 4744 | return f->buf[f->buf_index++]; |
| 4745 | } |
| 4746 | |
| 4747 | int64_t qemu_ftell(QEMUFile *f) |
| 4748 | { |
| 4749 | return f->buf_offset - f->buf_size + f->buf_index; |
| 4750 | } |
| 4751 | |
| 4752 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 4753 | { |
| 4754 | if (whence == SEEK_SET) { |
| 4755 | /* nothing to do */ |
| 4756 | } else if (whence == SEEK_CUR) { |
| 4757 | pos += qemu_ftell(f); |
| 4758 | } else { |
| 4759 | /* SEEK_END not supported */ |
| 4760 | return -1; |
| 4761 | } |
| 4762 | if (f->is_writable) { |
| 4763 | qemu_fflush(f); |
| 4764 | f->buf_offset = pos; |
| 4765 | } else { |
| 4766 | f->buf_offset = pos; |
| 4767 | f->buf_index = 0; |
| 4768 | f->buf_size = 0; |
| 4769 | } |
| 4770 | return pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 4774 | { |
| 4775 | qemu_put_byte(f, v >> 8); |
| 4776 | qemu_put_byte(f, v); |
| 4777 | } |
| 4778 | |
| 4779 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 4780 | { |
| 4781 | qemu_put_byte(f, v >> 24); |
| 4782 | qemu_put_byte(f, v >> 16); |
| 4783 | qemu_put_byte(f, v >> 8); |
| 4784 | qemu_put_byte(f, v); |
| 4785 | } |
| 4786 | |
| 4787 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 4788 | { |
| 4789 | qemu_put_be32(f, v >> 32); |
| 4790 | qemu_put_be32(f, v); |
| 4791 | } |
| 4792 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4793 | unsigned int qemu_get_be16(QEMUFile *f) |
| 4794 | { |
| 4795 | unsigned int v; |
| 4796 | v = qemu_get_byte(f) << 8; |
| 4797 | v |= qemu_get_byte(f); |
| 4798 | return v; |
| 4799 | } |
| 4800 | |
| 4801 | unsigned int qemu_get_be32(QEMUFile *f) |
| 4802 | { |
| 4803 | unsigned int v; |
| 4804 | v = qemu_get_byte(f) << 24; |
| 4805 | v |= qemu_get_byte(f) << 16; |
| 4806 | v |= qemu_get_byte(f) << 8; |
| 4807 | v |= qemu_get_byte(f); |
| 4808 | return v; |
| 4809 | } |
| 4810 | |
| 4811 | uint64_t qemu_get_be64(QEMUFile *f) |
| 4812 | { |
| 4813 | uint64_t v; |
| 4814 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 4815 | v |= qemu_get_be32(f); |
| 4816 | return v; |
| 4817 | } |
| 4818 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4819 | typedef struct SaveStateEntry { |
| 4820 | char idstr[256]; |
| 4821 | int instance_id; |
| 4822 | int version_id; |
| 4823 | SaveStateHandler *save_state; |
| 4824 | LoadStateHandler *load_state; |
| 4825 | void *opaque; |
| 4826 | struct SaveStateEntry *next; |
| 4827 | } SaveStateEntry; |
| 4828 | |
| 4829 | static SaveStateEntry *first_se; |
| 4830 | |
| 4831 | int register_savevm(const char *idstr, |
| 4832 | int instance_id, |
| 4833 | int version_id, |
| 4834 | SaveStateHandler *save_state, |
| 4835 | LoadStateHandler *load_state, |
| 4836 | void *opaque) |
| 4837 | { |
| 4838 | SaveStateEntry *se, **pse; |
| 4839 | |
| 4840 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 4841 | if (!se) |
| 4842 | return -1; |
| 4843 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 4844 | se->instance_id = instance_id; |
| 4845 | se->version_id = version_id; |
| 4846 | se->save_state = save_state; |
| 4847 | se->load_state = load_state; |
| 4848 | se->opaque = opaque; |
| 4849 | se->next = NULL; |
| 4850 | |
| 4851 | /* add at the end of list */ |
| 4852 | pse = &first_se; |
| 4853 | while (*pse != NULL) |
| 4854 | pse = &(*pse)->next; |
| 4855 | *pse = se; |
| 4856 | return 0; |
| 4857 | } |
| 4858 | |
| 4859 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4860 | #define QEMU_VM_FILE_VERSION 0x00000002 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4861 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4862 | int qemu_savevm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4863 | { |
| 4864 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4865 | int len, ret; |
| 4866 | int64_t cur_pos, len_pos, total_len_pos; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4867 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4868 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 4869 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4870 | total_len_pos = qemu_ftell(f); |
| 4871 | qemu_put_be64(f, 0); /* total size */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4872 | |
| 4873 | for(se = first_se; se != NULL; se = se->next) { |
| 4874 | /* ID string */ |
| 4875 | len = strlen(se->idstr); |
| 4876 | qemu_put_byte(f, len); |
| 4877 | qemu_put_buffer(f, se->idstr, len); |
| 4878 | |
| 4879 | qemu_put_be32(f, se->instance_id); |
| 4880 | qemu_put_be32(f, se->version_id); |
| 4881 | |
| 4882 | /* record size: filled later */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4883 | len_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4884 | qemu_put_be32(f, 0); |
| 4885 | |
| 4886 | se->save_state(f, se->opaque); |
| 4887 | |
| 4888 | /* fill record size */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4889 | cur_pos = qemu_ftell(f); |
| 4890 | len = cur_pos - len_pos - 4; |
| 4891 | qemu_fseek(f, len_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4892 | qemu_put_be32(f, len); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4893 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4894 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4895 | cur_pos = qemu_ftell(f); |
| 4896 | qemu_fseek(f, total_len_pos, SEEK_SET); |
| 4897 | qemu_put_be64(f, cur_pos - total_len_pos - 8); |
| 4898 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4899 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4900 | ret = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4901 | return ret; |
| 4902 | } |
| 4903 | |
| 4904 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 4905 | { |
| 4906 | SaveStateEntry *se; |
| 4907 | |
| 4908 | for(se = first_se; se != NULL; se = se->next) { |
| 4909 | if (!strcmp(se->idstr, idstr) && |
| 4910 | instance_id == se->instance_id) |
| 4911 | return se; |
| 4912 | } |
| 4913 | return NULL; |
| 4914 | } |
| 4915 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4916 | int qemu_loadvm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4917 | { |
| 4918 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4919 | int len, ret, instance_id, record_len, version_id; |
| 4920 | int64_t total_len, end_pos, cur_pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4921 | unsigned int v; |
| 4922 | char idstr[256]; |
| 4923 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4924 | v = qemu_get_be32(f); |
| 4925 | if (v != QEMU_VM_FILE_MAGIC) |
| 4926 | goto fail; |
| 4927 | v = qemu_get_be32(f); |
| 4928 | if (v != QEMU_VM_FILE_VERSION) { |
| 4929 | fail: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4930 | ret = -1; |
| 4931 | goto the_end; |
| 4932 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4933 | total_len = qemu_get_be64(f); |
| 4934 | end_pos = total_len + qemu_ftell(f); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4935 | for(;;) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4936 | if (qemu_ftell(f) >= end_pos) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4937 | break; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4938 | len = qemu_get_byte(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4939 | qemu_get_buffer(f, idstr, len); |
| 4940 | idstr[len] = '\0'; |
| 4941 | instance_id = qemu_get_be32(f); |
| 4942 | version_id = qemu_get_be32(f); |
| 4943 | record_len = qemu_get_be32(f); |
| 4944 | #if 0 |
| 4945 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
| 4946 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 4947 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4948 | cur_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4949 | se = find_se(idstr, instance_id); |
| 4950 | if (!se) { |
| 4951 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
| 4952 | instance_id, idstr); |
| 4953 | } else { |
| 4954 | ret = se->load_state(f, se->opaque, version_id); |
| 4955 | if (ret < 0) { |
| 4956 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 4957 | instance_id, idstr); |
| 4958 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 4959 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4960 | /* always seek to exact end of record */ |
| 4961 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 4962 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4963 | ret = 0; |
| 4964 | the_end: |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4965 | return ret; |
| 4966 | } |
| 4967 | |
| 4968 | /* device can contain snapshots */ |
| 4969 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 4970 | { |
| 4971 | return (bs && |
| 4972 | !bdrv_is_removable(bs) && |
| 4973 | !bdrv_is_read_only(bs)); |
| 4974 | } |
| 4975 | |
| 4976 | /* device must be snapshots in order to have a reliable snapshot */ |
| 4977 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 4978 | { |
| 4979 | return (bs && |
| 4980 | !bdrv_is_removable(bs) && |
| 4981 | !bdrv_is_read_only(bs)); |
| 4982 | } |
| 4983 | |
| 4984 | static BlockDriverState *get_bs_snapshots(void) |
| 4985 | { |
| 4986 | BlockDriverState *bs; |
| 4987 | int i; |
| 4988 | |
| 4989 | if (bs_snapshots) |
| 4990 | return bs_snapshots; |
| 4991 | for(i = 0; i <= MAX_DISKS; i++) { |
| 4992 | bs = bs_table[i]; |
| 4993 | if (bdrv_can_snapshot(bs)) |
| 4994 | goto ok; |
| 4995 | } |
| 4996 | return NULL; |
| 4997 | ok: |
| 4998 | bs_snapshots = bs; |
| 4999 | return bs; |
| 5000 | } |
| 5001 | |
| 5002 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 5003 | const char *name) |
| 5004 | { |
| 5005 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5006 | int nb_sns, i, ret; |
| 5007 | |
| 5008 | ret = -ENOENT; |
| 5009 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5010 | if (nb_sns < 0) |
| 5011 | return ret; |
| 5012 | for(i = 0; i < nb_sns; i++) { |
| 5013 | sn = &sn_tab[i]; |
| 5014 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 5015 | *sn_info = *sn; |
| 5016 | ret = 0; |
| 5017 | break; |
| 5018 | } |
| 5019 | } |
| 5020 | qemu_free(sn_tab); |
| 5021 | return ret; |
| 5022 | } |
| 5023 | |
| 5024 | void do_savevm(const char *name) |
| 5025 | { |
| 5026 | BlockDriverState *bs, *bs1; |
| 5027 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
| 5028 | int must_delete, ret, i; |
| 5029 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5030 | QEMUFile *f; |
| 5031 | int saved_vm_running; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5032 | #ifdef _WIN32 |
| 5033 | struct _timeb tb; |
| 5034 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5035 | struct timeval tv; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5036 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5037 | |
| 5038 | bs = get_bs_snapshots(); |
| 5039 | if (!bs) { |
| 5040 | term_printf("No block device can accept snapshots\n"); |
| 5041 | return; |
| 5042 | } |
| 5043 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5044 | /* ??? Should this occur after vm_stop? */ |
| 5045 | qemu_aio_flush(); |
| 5046 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5047 | saved_vm_running = vm_running; |
| 5048 | vm_stop(0); |
| 5049 | |
| 5050 | must_delete = 0; |
| 5051 | if (name) { |
| 5052 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 5053 | if (ret >= 0) { |
| 5054 | must_delete = 1; |
| 5055 | } |
| 5056 | } |
| 5057 | memset(sn, 0, sizeof(*sn)); |
| 5058 | if (must_delete) { |
| 5059 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 5060 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 5061 | } else { |
| 5062 | if (name) |
| 5063 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 5064 | } |
| 5065 | |
| 5066 | /* fill auxiliary fields */ |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5067 | #ifdef _WIN32 |
| 5068 | _ftime(&tb); |
| 5069 | sn->date_sec = tb.time; |
| 5070 | sn->date_nsec = tb.millitm * 1000000; |
| 5071 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5072 | gettimeofday(&tv, NULL); |
| 5073 | sn->date_sec = tv.tv_sec; |
| 5074 | sn->date_nsec = tv.tv_usec * 1000; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5075 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5076 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
| 5077 | |
| 5078 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5079 | term_printf("Device %s does not support VM state snapshots\n", |
| 5080 | bdrv_get_device_name(bs)); |
| 5081 | goto the_end; |
| 5082 | } |
| 5083 | |
| 5084 | /* save the VM state */ |
| 5085 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); |
| 5086 | if (!f) { |
| 5087 | term_printf("Could not open VM state file\n"); |
| 5088 | goto the_end; |
| 5089 | } |
| 5090 | ret = qemu_savevm_state(f); |
| 5091 | sn->vm_state_size = qemu_ftell(f); |
| 5092 | qemu_fclose(f); |
| 5093 | if (ret < 0) { |
| 5094 | term_printf("Error %d while writing VM\n", ret); |
| 5095 | goto the_end; |
| 5096 | } |
| 5097 | |
| 5098 | /* create the snapshots */ |
| 5099 | |
| 5100 | for(i = 0; i < MAX_DISKS; i++) { |
| 5101 | bs1 = bs_table[i]; |
| 5102 | if (bdrv_has_snapshot(bs1)) { |
| 5103 | if (must_delete) { |
| 5104 | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
| 5105 | if (ret < 0) { |
| 5106 | term_printf("Error while deleting snapshot on '%s'\n", |
| 5107 | bdrv_get_device_name(bs1)); |
| 5108 | } |
| 5109 | } |
| 5110 | ret = bdrv_snapshot_create(bs1, sn); |
| 5111 | if (ret < 0) { |
| 5112 | term_printf("Error while creating snapshot on '%s'\n", |
| 5113 | bdrv_get_device_name(bs1)); |
| 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | the_end: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5119 | if (saved_vm_running) |
| 5120 | vm_start(); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
| 5123 | void do_loadvm(const char *name) |
| 5124 | { |
| 5125 | BlockDriverState *bs, *bs1; |
| 5126 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5127 | QEMUFile *f; |
| 5128 | int i, ret; |
| 5129 | int saved_vm_running; |
| 5130 | |
| 5131 | bs = get_bs_snapshots(); |
| 5132 | if (!bs) { |
| 5133 | term_printf("No block device supports snapshots\n"); |
| 5134 | return; |
| 5135 | } |
| 5136 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5137 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 5138 | qemu_aio_flush(); |
| 5139 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5140 | saved_vm_running = vm_running; |
| 5141 | vm_stop(0); |
| 5142 | |
| 5143 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5144 | bs1 = bs_table[i]; |
| 5145 | if (bdrv_has_snapshot(bs1)) { |
| 5146 | ret = bdrv_snapshot_goto(bs1, name); |
| 5147 | if (ret < 0) { |
| 5148 | if (bs != bs1) |
| 5149 | term_printf("Warning: "); |
| 5150 | switch(ret) { |
| 5151 | case -ENOTSUP: |
| 5152 | term_printf("Snapshots not supported on device '%s'\n", |
| 5153 | bdrv_get_device_name(bs1)); |
| 5154 | break; |
| 5155 | case -ENOENT: |
| 5156 | term_printf("Could not find snapshot '%s' on device '%s'\n", |
| 5157 | name, bdrv_get_device_name(bs1)); |
| 5158 | break; |
| 5159 | default: |
| 5160 | term_printf("Error %d while activating snapshot on '%s'\n", |
| 5161 | ret, bdrv_get_device_name(bs1)); |
| 5162 | break; |
| 5163 | } |
| 5164 | /* fatal on snapshot block device */ |
| 5165 | if (bs == bs1) |
| 5166 | goto the_end; |
| 5167 | } |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5172 | term_printf("Device %s does not support VM state snapshots\n", |
| 5173 | bdrv_get_device_name(bs)); |
| 5174 | return; |
| 5175 | } |
| 5176 | |
| 5177 | /* restore the VM state */ |
| 5178 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); |
| 5179 | if (!f) { |
| 5180 | term_printf("Could not open VM state file\n"); |
| 5181 | goto the_end; |
| 5182 | } |
| 5183 | ret = qemu_loadvm_state(f); |
| 5184 | qemu_fclose(f); |
| 5185 | if (ret < 0) { |
| 5186 | term_printf("Error %d while loading VM state\n", ret); |
| 5187 | } |
| 5188 | the_end: |
| 5189 | if (saved_vm_running) |
| 5190 | vm_start(); |
| 5191 | } |
| 5192 | |
| 5193 | void do_delvm(const char *name) |
| 5194 | { |
| 5195 | BlockDriverState *bs, *bs1; |
| 5196 | int i, ret; |
| 5197 | |
| 5198 | bs = get_bs_snapshots(); |
| 5199 | if (!bs) { |
| 5200 | term_printf("No block device supports snapshots\n"); |
| 5201 | return; |
| 5202 | } |
| 5203 | |
| 5204 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5205 | bs1 = bs_table[i]; |
| 5206 | if (bdrv_has_snapshot(bs1)) { |
| 5207 | ret = bdrv_snapshot_delete(bs1, name); |
| 5208 | if (ret < 0) { |
| 5209 | if (ret == -ENOTSUP) |
| 5210 | term_printf("Snapshots not supported on device '%s'\n", |
| 5211 | bdrv_get_device_name(bs1)); |
| 5212 | else |
| 5213 | term_printf("Error %d while deleting snapshot on '%s'\n", |
| 5214 | ret, bdrv_get_device_name(bs1)); |
| 5215 | } |
| 5216 | } |
| 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | void do_info_snapshots(void) |
| 5221 | { |
| 5222 | BlockDriverState *bs, *bs1; |
| 5223 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5224 | int nb_sns, i; |
| 5225 | char buf[256]; |
| 5226 | |
| 5227 | bs = get_bs_snapshots(); |
| 5228 | if (!bs) { |
| 5229 | term_printf("No available block device supports snapshots\n"); |
| 5230 | return; |
| 5231 | } |
| 5232 | term_printf("Snapshot devices:"); |
| 5233 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5234 | bs1 = bs_table[i]; |
| 5235 | if (bdrv_has_snapshot(bs1)) { |
| 5236 | if (bs == bs1) |
| 5237 | term_printf(" %s", bdrv_get_device_name(bs1)); |
| 5238 | } |
| 5239 | } |
| 5240 | term_printf("\n"); |
| 5241 | |
| 5242 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5243 | if (nb_sns < 0) { |
| 5244 | term_printf("bdrv_snapshot_list: error %d\n", nb_sns); |
| 5245 | return; |
| 5246 | } |
| 5247 | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs)); |
| 5248 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 5249 | for(i = 0; i < nb_sns; i++) { |
| 5250 | sn = &sn_tab[i]; |
| 5251 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 5252 | } |
| 5253 | qemu_free(sn_tab); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | /***********************************************************/ |
| 5257 | /* cpu save/restore */ |
| 5258 | |
| 5259 | #if defined(TARGET_I386) |
| 5260 | |
| 5261 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
| 5262 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5263 | qemu_put_be32(f, dt->selector); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5264 | qemu_put_betl(f, dt->base); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5265 | qemu_put_be32(f, dt->limit); |
| 5266 | qemu_put_be32(f, dt->flags); |
| 5267 | } |
| 5268 | |
| 5269 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
| 5270 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5271 | dt->selector = qemu_get_be32(f); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5272 | dt->base = qemu_get_betl(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5273 | dt->limit = qemu_get_be32(f); |
| 5274 | dt->flags = qemu_get_be32(f); |
| 5275 | } |
| 5276 | |
| 5277 | void cpu_save(QEMUFile *f, void *opaque) |
| 5278 | { |
| 5279 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5280 | uint16_t fptag, fpus, fpuc, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5281 | uint32_t hflags; |
| 5282 | int i; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5283 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5284 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5285 | qemu_put_betls(f, &env->regs[i]); |
| 5286 | qemu_put_betls(f, &env->eip); |
| 5287 | qemu_put_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5288 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ |
| 5289 | qemu_put_be32s(f, &hflags); |
| 5290 | |
| 5291 | /* FPU */ |
| 5292 | fpuc = env->fpuc; |
| 5293 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
| 5294 | fptag = 0; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5295 | for(i = 0; i < 8; i++) { |
| 5296 | fptag |= ((!env->fptags[i]) << i); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | qemu_put_be16s(f, &fpuc); |
| 5300 | qemu_put_be16s(f, &fpus); |
| 5301 | qemu_put_be16s(f, &fptag); |
| 5302 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5303 | #ifdef USE_X86LDOUBLE |
| 5304 | fpregs_format = 0; |
| 5305 | #else |
| 5306 | fpregs_format = 1; |
| 5307 | #endif |
| 5308 | qemu_put_be16s(f, &fpregs_format); |
| 5309 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5310 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5311 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5312 | { |
| 5313 | uint64_t mant; |
| 5314 | uint16_t exp; |
| 5315 | /* we save the real CPU data (in case of MMX usage only 'mant' |
| 5316 | contains the MMX register */ |
| 5317 | cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
| 5318 | qemu_put_be64(f, mant); |
| 5319 | qemu_put_be16(f, exp); |
| 5320 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5321 | #else |
| 5322 | /* if we use doubles for float emulation, we save the doubles to |
| 5323 | avoid losing information in case of MMX usage. It can give |
| 5324 | problems if the image is restored on a CPU where long |
| 5325 | doubles are used instead. */ |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5326 | qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0)); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5327 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | for(i = 0; i < 6; i++) |
| 5331 | cpu_put_seg(f, &env->segs[i]); |
| 5332 | cpu_put_seg(f, &env->ldt); |
| 5333 | cpu_put_seg(f, &env->tr); |
| 5334 | cpu_put_seg(f, &env->gdt); |
| 5335 | cpu_put_seg(f, &env->idt); |
| 5336 | |
| 5337 | qemu_put_be32s(f, &env->sysenter_cs); |
| 5338 | qemu_put_be32s(f, &env->sysenter_esp); |
| 5339 | qemu_put_be32s(f, &env->sysenter_eip); |
| 5340 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5341 | qemu_put_betls(f, &env->cr[0]); |
| 5342 | qemu_put_betls(f, &env->cr[2]); |
| 5343 | qemu_put_betls(f, &env->cr[3]); |
| 5344 | qemu_put_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5345 | |
| 5346 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5347 | qemu_put_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5348 | |
| 5349 | /* MMU */ |
| 5350 | qemu_put_be32s(f, &env->a20_mask); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5351 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5352 | /* XMM */ |
| 5353 | qemu_put_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5354 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5355 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5356 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5357 | } |
| 5358 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5359 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5360 | qemu_put_be64s(f, &env->efer); |
| 5361 | qemu_put_be64s(f, &env->star); |
| 5362 | qemu_put_be64s(f, &env->lstar); |
| 5363 | qemu_put_be64s(f, &env->cstar); |
| 5364 | qemu_put_be64s(f, &env->fmask); |
| 5365 | qemu_put_be64s(f, &env->kernelgsbase); |
| 5366 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5367 | qemu_put_be32s(f, &env->smbase); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5370 | #ifdef USE_X86LDOUBLE |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5371 | /* XXX: add that in a FPU generic layer */ |
| 5372 | union x86_longdouble { |
| 5373 | uint64_t mant; |
| 5374 | uint16_t exp; |
| 5375 | }; |
| 5376 | |
| 5377 | #define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
| 5378 | #define EXPBIAS1 1023 |
| 5379 | #define EXPD1(fp) ((fp >> 52) & 0x7FF) |
| 5380 | #define SIGND1(fp) ((fp >> 32) & 0x80000000) |
| 5381 | |
| 5382 | static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
| 5383 | { |
| 5384 | int e; |
| 5385 | /* mantissa */ |
| 5386 | p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
| 5387 | /* exponent + sign */ |
| 5388 | e = EXPD1(temp) - EXPBIAS1 + 16383; |
| 5389 | e |= SIGND1(temp) >> 16; |
| 5390 | p->exp = e; |
| 5391 | } |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5392 | #endif |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5393 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5394 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5395 | { |
| 5396 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5397 | int i, guess_mmx; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5398 | uint32_t hflags; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5399 | uint16_t fpus, fpuc, fptag, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5400 | |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5401 | if (version_id != 3 && version_id != 4) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5402 | return -EINVAL; |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5403 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5404 | qemu_get_betls(f, &env->regs[i]); |
| 5405 | qemu_get_betls(f, &env->eip); |
| 5406 | qemu_get_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5407 | qemu_get_be32s(f, &hflags); |
| 5408 | |
| 5409 | qemu_get_be16s(f, &fpuc); |
| 5410 | qemu_get_be16s(f, &fpus); |
| 5411 | qemu_get_be16s(f, &fptag); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5412 | qemu_get_be16s(f, &fpregs_format); |
| 5413 | |
| 5414 | /* NOTE: we cannot always restore the FPU state if the image come |
| 5415 | from a host with a different 'USE_X86LDOUBLE' define. We guess |
| 5416 | if we are in an MMX state to restore correctly in that case. */ |
| 5417 | guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5418 | for(i = 0; i < 8; i++) { |
| 5419 | uint64_t mant; |
| 5420 | uint16_t exp; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5421 | |
| 5422 | switch(fpregs_format) { |
| 5423 | case 0: |
| 5424 | mant = qemu_get_be64(f); |
| 5425 | exp = qemu_get_be16(f); |
| 5426 | #ifdef USE_X86LDOUBLE |
| 5427 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5428 | #else |
| 5429 | /* difficult case */ |
| 5430 | if (guess_mmx) |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5431 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5432 | else |
| 5433 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5434 | #endif |
| 5435 | break; |
| 5436 | case 1: |
| 5437 | mant = qemu_get_be64(f); |
| 5438 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5439 | { |
| 5440 | union x86_longdouble *p; |
| 5441 | /* difficult case */ |
| 5442 | p = (void *)&env->fpregs[i]; |
| 5443 | if (guess_mmx) { |
| 5444 | p->mant = mant; |
| 5445 | p->exp = 0xffff; |
| 5446 | } else { |
| 5447 | fp64_to_fp80(p, mant); |
| 5448 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5449 | } |
| 5450 | #else |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5451 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5452 | #endif |
| 5453 | break; |
| 5454 | default: |
| 5455 | return -EINVAL; |
| 5456 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | env->fpuc = fpuc; |
bellard | 7a0e1f4 | 2005-03-13 17:01:47 +0000 | [diff] [blame] | 5460 | /* XXX: restore FPU round state */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5461 | env->fpstt = (fpus >> 11) & 7; |
| 5462 | env->fpus = fpus & ~0x3800; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5463 | fptag ^= 0xff; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5464 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5465 | env->fptags[i] = (fptag >> i) & 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | for(i = 0; i < 6; i++) |
| 5469 | cpu_get_seg(f, &env->segs[i]); |
| 5470 | cpu_get_seg(f, &env->ldt); |
| 5471 | cpu_get_seg(f, &env->tr); |
| 5472 | cpu_get_seg(f, &env->gdt); |
| 5473 | cpu_get_seg(f, &env->idt); |
| 5474 | |
| 5475 | qemu_get_be32s(f, &env->sysenter_cs); |
| 5476 | qemu_get_be32s(f, &env->sysenter_esp); |
| 5477 | qemu_get_be32s(f, &env->sysenter_eip); |
| 5478 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5479 | qemu_get_betls(f, &env->cr[0]); |
| 5480 | qemu_get_betls(f, &env->cr[2]); |
| 5481 | qemu_get_betls(f, &env->cr[3]); |
| 5482 | qemu_get_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5483 | |
| 5484 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5485 | qemu_get_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5486 | |
| 5487 | /* MMU */ |
| 5488 | qemu_get_be32s(f, &env->a20_mask); |
| 5489 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5490 | qemu_get_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5491 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5492 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5493 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5494 | } |
| 5495 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5496 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5497 | qemu_get_be64s(f, &env->efer); |
| 5498 | qemu_get_be64s(f, &env->star); |
| 5499 | qemu_get_be64s(f, &env->lstar); |
| 5500 | qemu_get_be64s(f, &env->cstar); |
| 5501 | qemu_get_be64s(f, &env->fmask); |
| 5502 | qemu_get_be64s(f, &env->kernelgsbase); |
| 5503 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5504 | if (version_id >= 4) |
| 5505 | qemu_get_be32s(f, &env->smbase); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5506 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5507 | /* XXX: compute hflags from scratch, except for CPL and IIF */ |
| 5508 | env->hflags = hflags; |
| 5509 | tlb_flush(env, 1); |
| 5510 | return 0; |
| 5511 | } |
| 5512 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 5513 | #elif defined(TARGET_PPC) |
| 5514 | void cpu_save(QEMUFile *f, void *opaque) |
| 5515 | { |
| 5516 | } |
| 5517 | |
| 5518 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5519 | { |
| 5520 | return 0; |
| 5521 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 5522 | |
| 5523 | #elif defined(TARGET_MIPS) |
| 5524 | void cpu_save(QEMUFile *f, void *opaque) |
| 5525 | { |
| 5526 | } |
| 5527 | |
| 5528 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5529 | { |
| 5530 | return 0; |
| 5531 | } |
| 5532 | |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5533 | #elif defined(TARGET_SPARC) |
| 5534 | void cpu_save(QEMUFile *f, void *opaque) |
| 5535 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5536 | CPUState *env = opaque; |
| 5537 | int i; |
| 5538 | uint32_t tmp; |
| 5539 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5540 | for(i = 0; i < 8; i++) |
| 5541 | qemu_put_betls(f, &env->gregs[i]); |
| 5542 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5543 | qemu_put_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5546 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5547 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5548 | float32 f; |
| 5549 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5550 | } u; |
| 5551 | u.f = env->fpr[i]; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5552 | qemu_put_be32(f, u.i); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5553 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5554 | |
| 5555 | qemu_put_betls(f, &env->pc); |
| 5556 | qemu_put_betls(f, &env->npc); |
| 5557 | qemu_put_betls(f, &env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5558 | tmp = GET_PSR(env); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5559 | qemu_put_be32(f, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5560 | qemu_put_betls(f, &env->fsr); |
| 5561 | qemu_put_betls(f, &env->tbr); |
| 5562 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5563 | qemu_put_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5564 | /* MMU */ |
| 5565 | for(i = 0; i < 16; i++) |
| 5566 | qemu_put_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5567 | #endif |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5571 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5572 | CPUState *env = opaque; |
| 5573 | int i; |
| 5574 | uint32_t tmp; |
| 5575 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5576 | for(i = 0; i < 8; i++) |
| 5577 | qemu_get_betls(f, &env->gregs[i]); |
| 5578 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5579 | qemu_get_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5580 | |
| 5581 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5582 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5583 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5584 | float32 f; |
| 5585 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5586 | } u; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5587 | u.i = qemu_get_be32(f); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5588 | env->fpr[i] = u.f; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5589 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5590 | |
| 5591 | qemu_get_betls(f, &env->pc); |
| 5592 | qemu_get_betls(f, &env->npc); |
| 5593 | qemu_get_betls(f, &env->y); |
| 5594 | tmp = qemu_get_be32(f); |
| 5595 | env->cwp = 0; /* needed to ensure that the wrapping registers are |
| 5596 | correctly updated */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5597 | PUT_PSR(env, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5598 | qemu_get_betls(f, &env->fsr); |
| 5599 | qemu_get_betls(f, &env->tbr); |
| 5600 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5601 | qemu_get_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5602 | /* MMU */ |
| 5603 | for(i = 0; i < 16; i++) |
| 5604 | qemu_get_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5605 | #endif |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5606 | tlb_flush(env, 1); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5607 | return 0; |
| 5608 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 5609 | |
| 5610 | #elif defined(TARGET_ARM) |
| 5611 | |
| 5612 | /* ??? Need to implement these. */ |
| 5613 | void cpu_save(QEMUFile *f, void *opaque) |
| 5614 | { |
| 5615 | } |
| 5616 | |
| 5617 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5618 | { |
| 5619 | return 0; |
| 5620 | } |
| 5621 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5622 | #else |
| 5623 | |
| 5624 | #warning No CPU save/restore functions |
| 5625 | |
| 5626 | #endif |
| 5627 | |
| 5628 | /***********************************************************/ |
| 5629 | /* ram save/restore */ |
| 5630 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5631 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 5632 | { |
| 5633 | int v; |
| 5634 | |
| 5635 | v = qemu_get_byte(f); |
| 5636 | switch(v) { |
| 5637 | case 0: |
| 5638 | if (qemu_get_buffer(f, buf, len) != len) |
| 5639 | return -EIO; |
| 5640 | break; |
| 5641 | case 1: |
| 5642 | v = qemu_get_byte(f); |
| 5643 | memset(buf, v, len); |
| 5644 | break; |
| 5645 | default: |
| 5646 | return -EINVAL; |
| 5647 | } |
| 5648 | return 0; |
| 5649 | } |
| 5650 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5651 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5652 | { |
| 5653 | int i, ret; |
| 5654 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5655 | if (qemu_get_be32(f) != phys_ram_size) |
| 5656 | return -EINVAL; |
| 5657 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 5658 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 5659 | if (ret) |
| 5660 | return ret; |
| 5661 | } |
| 5662 | return 0; |
| 5663 | } |
| 5664 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5665 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 5666 | #define IOBUF_SIZE 4096 |
| 5667 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 5668 | |
| 5669 | typedef struct RamCompressState { |
| 5670 | z_stream zstream; |
| 5671 | QEMUFile *f; |
| 5672 | uint8_t buf[IOBUF_SIZE]; |
| 5673 | } RamCompressState; |
| 5674 | |
| 5675 | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
| 5676 | { |
| 5677 | int ret; |
| 5678 | memset(s, 0, sizeof(*s)); |
| 5679 | s->f = f; |
| 5680 | ret = deflateInit2(&s->zstream, 1, |
| 5681 | Z_DEFLATED, 15, |
| 5682 | 9, Z_DEFAULT_STRATEGY); |
| 5683 | if (ret != Z_OK) |
| 5684 | return -1; |
| 5685 | s->zstream.avail_out = IOBUF_SIZE; |
| 5686 | s->zstream.next_out = s->buf; |
| 5687 | return 0; |
| 5688 | } |
| 5689 | |
| 5690 | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
| 5691 | { |
| 5692 | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
| 5693 | qemu_put_be16(s->f, len); |
| 5694 | qemu_put_buffer(s->f, buf, len); |
| 5695 | } |
| 5696 | |
| 5697 | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
| 5698 | { |
| 5699 | int ret; |
| 5700 | |
| 5701 | s->zstream.avail_in = len; |
| 5702 | s->zstream.next_in = (uint8_t *)buf; |
| 5703 | while (s->zstream.avail_in > 0) { |
| 5704 | ret = deflate(&s->zstream, Z_NO_FLUSH); |
| 5705 | if (ret != Z_OK) |
| 5706 | return -1; |
| 5707 | if (s->zstream.avail_out == 0) { |
| 5708 | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
| 5709 | s->zstream.avail_out = IOBUF_SIZE; |
| 5710 | s->zstream.next_out = s->buf; |
| 5711 | } |
| 5712 | } |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
| 5716 | static void ram_compress_close(RamCompressState *s) |
| 5717 | { |
| 5718 | int len, ret; |
| 5719 | |
| 5720 | /* compress last bytes */ |
| 5721 | for(;;) { |
| 5722 | ret = deflate(&s->zstream, Z_FINISH); |
| 5723 | if (ret == Z_OK || ret == Z_STREAM_END) { |
| 5724 | len = IOBUF_SIZE - s->zstream.avail_out; |
| 5725 | if (len > 0) { |
| 5726 | ram_put_cblock(s, s->buf, len); |
| 5727 | } |
| 5728 | s->zstream.avail_out = IOBUF_SIZE; |
| 5729 | s->zstream.next_out = s->buf; |
| 5730 | if (ret == Z_STREAM_END) |
| 5731 | break; |
| 5732 | } else { |
| 5733 | goto fail; |
| 5734 | } |
| 5735 | } |
| 5736 | fail: |
| 5737 | deflateEnd(&s->zstream); |
| 5738 | } |
| 5739 | |
| 5740 | typedef struct RamDecompressState { |
| 5741 | z_stream zstream; |
| 5742 | QEMUFile *f; |
| 5743 | uint8_t buf[IOBUF_SIZE]; |
| 5744 | } RamDecompressState; |
| 5745 | |
| 5746 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 5747 | { |
| 5748 | int ret; |
| 5749 | memset(s, 0, sizeof(*s)); |
| 5750 | s->f = f; |
| 5751 | ret = inflateInit(&s->zstream); |
| 5752 | if (ret != Z_OK) |
| 5753 | return -1; |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
| 5757 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 5758 | { |
| 5759 | int ret, clen; |
| 5760 | |
| 5761 | s->zstream.avail_out = len; |
| 5762 | s->zstream.next_out = buf; |
| 5763 | while (s->zstream.avail_out > 0) { |
| 5764 | if (s->zstream.avail_in == 0) { |
| 5765 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 5766 | return -1; |
| 5767 | clen = qemu_get_be16(s->f); |
| 5768 | if (clen > IOBUF_SIZE) |
| 5769 | return -1; |
| 5770 | qemu_get_buffer(s->f, s->buf, clen); |
| 5771 | s->zstream.avail_in = clen; |
| 5772 | s->zstream.next_in = s->buf; |
| 5773 | } |
| 5774 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 5775 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 5776 | return -1; |
| 5777 | } |
| 5778 | } |
| 5779 | return 0; |
| 5780 | } |
| 5781 | |
| 5782 | static void ram_decompress_close(RamDecompressState *s) |
| 5783 | { |
| 5784 | inflateEnd(&s->zstream); |
| 5785 | } |
| 5786 | |
| 5787 | static void ram_save(QEMUFile *f, void *opaque) |
| 5788 | { |
| 5789 | int i; |
| 5790 | RamCompressState s1, *s = &s1; |
| 5791 | uint8_t buf[10]; |
| 5792 | |
| 5793 | qemu_put_be32(f, phys_ram_size); |
| 5794 | if (ram_compress_open(s, f) < 0) |
| 5795 | return; |
| 5796 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5797 | #if 0 |
| 5798 | if (tight_savevm_enabled) { |
| 5799 | int64_t sector_num; |
| 5800 | int j; |
| 5801 | |
| 5802 | /* find if the memory block is available on a virtual |
| 5803 | block device */ |
| 5804 | sector_num = -1; |
| 5805 | for(j = 0; j < MAX_DISKS; j++) { |
| 5806 | if (bs_table[j]) { |
| 5807 | sector_num = bdrv_hash_find(bs_table[j], |
| 5808 | phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5809 | if (sector_num >= 0) |
| 5810 | break; |
| 5811 | } |
| 5812 | } |
| 5813 | if (j == MAX_DISKS) |
| 5814 | goto normal_compress; |
| 5815 | buf[0] = 1; |
| 5816 | buf[1] = j; |
| 5817 | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num); |
| 5818 | ram_compress_buf(s, buf, 10); |
| 5819 | } else |
| 5820 | #endif |
| 5821 | { |
| 5822 | // normal_compress: |
| 5823 | buf[0] = 0; |
| 5824 | ram_compress_buf(s, buf, 1); |
| 5825 | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5826 | } |
| 5827 | } |
| 5828 | ram_compress_close(s); |
| 5829 | } |
| 5830 | |
| 5831 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 5832 | { |
| 5833 | RamDecompressState s1, *s = &s1; |
| 5834 | uint8_t buf[10]; |
| 5835 | int i; |
| 5836 | |
| 5837 | if (version_id == 1) |
| 5838 | return ram_load_v1(f, opaque); |
| 5839 | if (version_id != 2) |
| 5840 | return -EINVAL; |
| 5841 | if (qemu_get_be32(f) != phys_ram_size) |
| 5842 | return -EINVAL; |
| 5843 | if (ram_decompress_open(s, f) < 0) |
| 5844 | return -EINVAL; |
| 5845 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5846 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 5847 | fprintf(stderr, "Error while reading ram block header\n"); |
| 5848 | goto error; |
| 5849 | } |
| 5850 | if (buf[0] == 0) { |
| 5851 | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
| 5852 | fprintf(stderr, "Error while reading ram block address=0x%08x", i); |
| 5853 | goto error; |
| 5854 | } |
| 5855 | } else |
| 5856 | #if 0 |
| 5857 | if (buf[0] == 1) { |
| 5858 | int bs_index; |
| 5859 | int64_t sector_num; |
| 5860 | |
| 5861 | ram_decompress_buf(s, buf + 1, 9); |
| 5862 | bs_index = buf[1]; |
| 5863 | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); |
| 5864 | if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) { |
| 5865 | fprintf(stderr, "Invalid block device index %d\n", bs_index); |
| 5866 | goto error; |
| 5867 | } |
| 5868 | if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i, |
| 5869 | BDRV_HASH_BLOCK_SIZE / 512) < 0) { |
| 5870 | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n", |
| 5871 | bs_index, sector_num); |
| 5872 | goto error; |
| 5873 | } |
| 5874 | } else |
| 5875 | #endif |
| 5876 | { |
| 5877 | error: |
| 5878 | printf("Error block header\n"); |
| 5879 | return -EINVAL; |
| 5880 | } |
| 5881 | } |
| 5882 | ram_decompress_close(s); |
| 5883 | return 0; |
| 5884 | } |
| 5885 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5886 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5887 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 5888 | |
| 5889 | struct QEMUBH { |
| 5890 | QEMUBHFunc *cb; |
| 5891 | void *opaque; |
| 5892 | int scheduled; |
| 5893 | QEMUBH *next; |
| 5894 | }; |
| 5895 | |
| 5896 | static QEMUBH *first_bh = NULL; |
| 5897 | |
| 5898 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 5899 | { |
| 5900 | QEMUBH *bh; |
| 5901 | bh = qemu_mallocz(sizeof(QEMUBH)); |
| 5902 | if (!bh) |
| 5903 | return NULL; |
| 5904 | bh->cb = cb; |
| 5905 | bh->opaque = opaque; |
| 5906 | return bh; |
| 5907 | } |
| 5908 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5909 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5910 | { |
| 5911 | QEMUBH *bh, **pbh; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5912 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5913 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5914 | ret = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5915 | for(;;) { |
| 5916 | pbh = &first_bh; |
| 5917 | bh = *pbh; |
| 5918 | if (!bh) |
| 5919 | break; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5920 | ret = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5921 | *pbh = bh->next; |
| 5922 | bh->scheduled = 0; |
| 5923 | bh->cb(bh->opaque); |
| 5924 | } |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5925 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | void qemu_bh_schedule(QEMUBH *bh) |
| 5929 | { |
| 5930 | CPUState *env = cpu_single_env; |
| 5931 | if (bh->scheduled) |
| 5932 | return; |
| 5933 | bh->scheduled = 1; |
| 5934 | bh->next = first_bh; |
| 5935 | first_bh = bh; |
| 5936 | |
| 5937 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 5938 | if (env) { |
| 5939 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
| 5940 | } |
| 5941 | } |
| 5942 | |
| 5943 | void qemu_bh_cancel(QEMUBH *bh) |
| 5944 | { |
| 5945 | QEMUBH **pbh; |
| 5946 | if (bh->scheduled) { |
| 5947 | pbh = &first_bh; |
| 5948 | while (*pbh != bh) |
| 5949 | pbh = &(*pbh)->next; |
| 5950 | *pbh = bh->next; |
| 5951 | bh->scheduled = 0; |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | void qemu_bh_delete(QEMUBH *bh) |
| 5956 | { |
| 5957 | qemu_bh_cancel(bh); |
| 5958 | qemu_free(bh); |
| 5959 | } |
| 5960 | |
| 5961 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5962 | /* machine registration */ |
| 5963 | |
| 5964 | QEMUMachine *first_machine = NULL; |
| 5965 | |
| 5966 | int qemu_register_machine(QEMUMachine *m) |
| 5967 | { |
| 5968 | QEMUMachine **pm; |
| 5969 | pm = &first_machine; |
| 5970 | while (*pm != NULL) |
| 5971 | pm = &(*pm)->next; |
| 5972 | m->next = NULL; |
| 5973 | *pm = m; |
| 5974 | return 0; |
| 5975 | } |
| 5976 | |
| 5977 | QEMUMachine *find_machine(const char *name) |
| 5978 | { |
| 5979 | QEMUMachine *m; |
| 5980 | |
| 5981 | for(m = first_machine; m != NULL; m = m->next) { |
| 5982 | if (!strcmp(m->name, name)) |
| 5983 | return m; |
| 5984 | } |
| 5985 | return NULL; |
| 5986 | } |
| 5987 | |
| 5988 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5989 | /* main execution loop */ |
| 5990 | |
| 5991 | void gui_update(void *opaque) |
| 5992 | { |
| 5993 | display_state.dpy_refresh(&display_state); |
| 5994 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
| 5995 | } |
| 5996 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 5997 | struct vm_change_state_entry { |
| 5998 | VMChangeStateHandler *cb; |
| 5999 | void *opaque; |
| 6000 | LIST_ENTRY (vm_change_state_entry) entries; |
| 6001 | }; |
| 6002 | |
| 6003 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 6004 | |
| 6005 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 6006 | void *opaque) |
| 6007 | { |
| 6008 | VMChangeStateEntry *e; |
| 6009 | |
| 6010 | e = qemu_mallocz(sizeof (*e)); |
| 6011 | if (!e) |
| 6012 | return NULL; |
| 6013 | |
| 6014 | e->cb = cb; |
| 6015 | e->opaque = opaque; |
| 6016 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 6017 | return e; |
| 6018 | } |
| 6019 | |
| 6020 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 6021 | { |
| 6022 | LIST_REMOVE (e, entries); |
| 6023 | qemu_free (e); |
| 6024 | } |
| 6025 | |
| 6026 | static void vm_state_notify(int running) |
| 6027 | { |
| 6028 | VMChangeStateEntry *e; |
| 6029 | |
| 6030 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
| 6031 | e->cb(e->opaque, running); |
| 6032 | } |
| 6033 | } |
| 6034 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6035 | /* XXX: support several handlers */ |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6036 | static VMStopHandler *vm_stop_cb; |
| 6037 | static void *vm_stop_opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6038 | |
| 6039 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6040 | { |
| 6041 | vm_stop_cb = cb; |
| 6042 | vm_stop_opaque = opaque; |
| 6043 | return 0; |
| 6044 | } |
| 6045 | |
| 6046 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6047 | { |
| 6048 | vm_stop_cb = NULL; |
| 6049 | } |
| 6050 | |
| 6051 | void vm_start(void) |
| 6052 | { |
| 6053 | if (!vm_running) { |
| 6054 | cpu_enable_ticks(); |
| 6055 | vm_running = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6056 | vm_state_notify(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | void vm_stop(int reason) |
| 6061 | { |
| 6062 | if (vm_running) { |
| 6063 | cpu_disable_ticks(); |
| 6064 | vm_running = 0; |
| 6065 | if (reason != 0) { |
| 6066 | if (vm_stop_cb) { |
| 6067 | vm_stop_cb(vm_stop_opaque, reason); |
| 6068 | } |
| 6069 | } |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6070 | vm_state_notify(0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6071 | } |
| 6072 | } |
| 6073 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6074 | /* reset/shutdown handler */ |
| 6075 | |
| 6076 | typedef struct QEMUResetEntry { |
| 6077 | QEMUResetHandler *func; |
| 6078 | void *opaque; |
| 6079 | struct QEMUResetEntry *next; |
| 6080 | } QEMUResetEntry; |
| 6081 | |
| 6082 | static QEMUResetEntry *first_reset_entry; |
| 6083 | static int reset_requested; |
| 6084 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6085 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6086 | |
| 6087 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 6088 | { |
| 6089 | QEMUResetEntry **pre, *re; |
| 6090 | |
| 6091 | pre = &first_reset_entry; |
| 6092 | while (*pre != NULL) |
| 6093 | pre = &(*pre)->next; |
| 6094 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 6095 | re->func = func; |
| 6096 | re->opaque = opaque; |
| 6097 | re->next = NULL; |
| 6098 | *pre = re; |
| 6099 | } |
| 6100 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 6101 | static void qemu_system_reset(void) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6102 | { |
| 6103 | QEMUResetEntry *re; |
| 6104 | |
| 6105 | /* reset all devices */ |
| 6106 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 6107 | re->func(re->opaque); |
| 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | void qemu_system_reset_request(void) |
| 6112 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6113 | if (no_reboot) { |
| 6114 | shutdown_requested = 1; |
| 6115 | } else { |
| 6116 | reset_requested = 1; |
| 6117 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6118 | if (cpu_single_env) |
| 6119 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | void qemu_system_shutdown_request(void) |
| 6123 | { |
| 6124 | shutdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6125 | if (cpu_single_env) |
| 6126 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6129 | void qemu_system_powerdown_request(void) |
| 6130 | { |
| 6131 | powerdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6132 | if (cpu_single_env) |
| 6133 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6136 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6137 | { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6138 | IOHandlerRecord *ioh; |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6139 | fd_set rfds, wfds, xfds; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6140 | int ret, nfds; |
| 6141 | struct timeval tv; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6142 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6143 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6144 | |
| 6145 | /* XXX: need to suppress polling by better using win32 events */ |
| 6146 | ret = 0; |
| 6147 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 6148 | ret |= pe->func(pe->opaque); |
| 6149 | } |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6150 | #ifdef _WIN32 |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6151 | if (ret == 0 && timeout > 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6152 | int err; |
| 6153 | WaitObjects *w = &wait_objects; |
| 6154 | |
| 6155 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
| 6156 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 6157 | if (w->func[ret - WAIT_OBJECT_0]) |
| 6158 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
| 6159 | } else if (ret == WAIT_TIMEOUT) { |
| 6160 | } else { |
| 6161 | err = GetLastError(); |
| 6162 | fprintf(stderr, "Wait error %d %d\n", ret, err); |
| 6163 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6164 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6165 | #endif |
| 6166 | /* poll any events */ |
| 6167 | /* XXX: separate device handlers from system ones */ |
| 6168 | nfds = -1; |
| 6169 | FD_ZERO(&rfds); |
| 6170 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6171 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6172 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6173 | if (ioh->deleted) |
| 6174 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6175 | if (ioh->fd_read && |
| 6176 | (!ioh->fd_read_poll || |
| 6177 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 6178 | FD_SET(ioh->fd, &rfds); |
| 6179 | if (ioh->fd > nfds) |
| 6180 | nfds = ioh->fd; |
| 6181 | } |
| 6182 | if (ioh->fd_write) { |
| 6183 | FD_SET(ioh->fd, &wfds); |
| 6184 | if (ioh->fd > nfds) |
| 6185 | nfds = ioh->fd; |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | tv.tv_sec = 0; |
| 6190 | #ifdef _WIN32 |
| 6191 | tv.tv_usec = 0; |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6192 | #else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6193 | tv.tv_usec = timeout * 1000; |
| 6194 | #endif |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6195 | #if defined(CONFIG_SLIRP) |
| 6196 | if (slirp_inited) { |
| 6197 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 6198 | } |
| 6199 | #endif |
| 6200 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6201 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6202 | IOHandlerRecord **pioh; |
| 6203 | |
| 6204 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 6205 | if (ioh->deleted) |
| 6206 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6207 | if (FD_ISSET(ioh->fd, &rfds)) { |
| 6208 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6209 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6210 | if (FD_ISSET(ioh->fd, &wfds)) { |
| 6211 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6214 | |
| 6215 | /* remove deleted IO handlers */ |
| 6216 | pioh = &first_io_handler; |
| 6217 | while (*pioh) { |
| 6218 | ioh = *pioh; |
| 6219 | if (ioh->deleted) { |
| 6220 | *pioh = ioh->next; |
| 6221 | qemu_free(ioh); |
| 6222 | } else |
| 6223 | pioh = &ioh->next; |
| 6224 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6225 | } |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6226 | #if defined(CONFIG_SLIRP) |
| 6227 | if (slirp_inited) { |
| 6228 | if (ret < 0) { |
| 6229 | FD_ZERO(&rfds); |
| 6230 | FD_ZERO(&wfds); |
| 6231 | FD_ZERO(&xfds); |
| 6232 | } |
| 6233 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 6234 | } |
| 6235 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6236 | qemu_aio_poll(); |
| 6237 | qemu_bh_poll(); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6238 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6239 | if (vm_running) { |
| 6240 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 6241 | qemu_get_clock(vm_clock)); |
| 6242 | /* run dma transfers, if any */ |
| 6243 | DMA_run(); |
| 6244 | } |
| 6245 | |
| 6246 | /* real time timers */ |
| 6247 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 6248 | qemu_get_clock(rt_clock)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6251 | static CPUState *cur_cpu; |
| 6252 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6253 | int main_loop(void) |
| 6254 | { |
| 6255 | int ret, timeout; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6256 | #ifdef CONFIG_PROFILER |
| 6257 | int64_t ti; |
| 6258 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6259 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6260 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6261 | cur_cpu = first_cpu; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6262 | for(;;) { |
| 6263 | if (vm_running) { |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6264 | |
| 6265 | env = cur_cpu; |
| 6266 | for(;;) { |
| 6267 | /* get next cpu */ |
| 6268 | env = env->next_cpu; |
| 6269 | if (!env) |
| 6270 | env = first_cpu; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6271 | #ifdef CONFIG_PROFILER |
| 6272 | ti = profile_getclock(); |
| 6273 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6274 | ret = cpu_exec(env); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6275 | #ifdef CONFIG_PROFILER |
| 6276 | qemu_time += profile_getclock() - ti; |
| 6277 | #endif |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 6278 | if (ret == EXCP_HLT) { |
| 6279 | /* Give the next CPU a chance to run. */ |
| 6280 | cur_cpu = env; |
| 6281 | continue; |
| 6282 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6283 | if (ret != EXCP_HALTED) |
| 6284 | break; |
| 6285 | /* all CPUs are halted ? */ |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 6286 | if (env == cur_cpu) |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6287 | break; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6288 | } |
| 6289 | cur_cpu = env; |
| 6290 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6291 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6292 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6293 | break; |
| 6294 | } |
| 6295 | if (reset_requested) { |
| 6296 | reset_requested = 0; |
| 6297 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6298 | ret = EXCP_INTERRUPT; |
| 6299 | } |
| 6300 | if (powerdown_requested) { |
| 6301 | powerdown_requested = 0; |
| 6302 | qemu_system_powerdown(); |
| 6303 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6304 | } |
| 6305 | if (ret == EXCP_DEBUG) { |
| 6306 | vm_stop(EXCP_DEBUG); |
| 6307 | } |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 6308 | /* If all cpus are halted then wait until the next IRQ */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6309 | /* XXX: use timeout computed from timers */ |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 6310 | if (ret == EXCP_HALTED) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6311 | timeout = 10; |
| 6312 | else |
| 6313 | timeout = 0; |
| 6314 | } else { |
| 6315 | timeout = 10; |
| 6316 | } |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6317 | #ifdef CONFIG_PROFILER |
| 6318 | ti = profile_getclock(); |
| 6319 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6320 | main_loop_wait(timeout); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6321 | #ifdef CONFIG_PROFILER |
| 6322 | dev_time += profile_getclock() - ti; |
| 6323 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6324 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 6325 | cpu_disable_ticks(); |
| 6326 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6327 | } |
| 6328 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6329 | void help(void) |
| 6330 | { |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 6331 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6332 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6333 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6334 | "'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] | 6335 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6336 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6337 | "-M machine select emulated machine (-M ? for list)\n" |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 6338 | "-cpu cpu select CPU (-cpu ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6339 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6340 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 6341 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6342 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 6343 | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n" |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6344 | "-snapshot write to temporary files instead of disk image files\n" |
| 6345 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6346 | "-no-frame open SDL window without a frame and window decorations\n" |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6347 | "-no-quit disable SDL window close capability\n" |
| 6348 | #endif |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6349 | #ifdef TARGET_I386 |
| 6350 | "-no-fd-bootchk disable boot signature checking for floppy disks\n" |
| 6351 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6352 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 6353 | "-smp n set the number of CPUs to 'n' [default=1]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6354 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6355 | #ifndef _WIN32 |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6356 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6357 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6358 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6359 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 6360 | "-soundhw c1,... enable audio support\n" |
| 6361 | " and only specified sound cards (comma separated list)\n" |
| 6362 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6363 | " use -soundhw all to enable all of them\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6364 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 6365 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6366 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6367 | #ifdef TARGET_I386 |
| 6368 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 6369 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 6370 | "-usb enable the USB driver (will be the default soon)\n" |
| 6371 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6372 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 6373 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6374 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 6375 | "-name string set the name of the guest\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6376 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6377 | "Network options:\n" |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 6378 | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6379 | " create a new Network Interface Card and connect it to VLAN 'n'\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6380 | #ifdef CONFIG_SLIRP |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 6381 | "-net user[,vlan=n][,hostname=host]\n" |
| 6382 | " connect the user mode network stack to VLAN 'n' and send\n" |
| 6383 | " hostname 'host' to DHCP clients\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6384 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6385 | #ifdef _WIN32 |
| 6386 | "-net tap[,vlan=n],ifname=name\n" |
| 6387 | " connect the host TAP network interface to VLAN 'n'\n" |
| 6388 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6389 | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n" |
| 6390 | " connect the host TAP network interface to VLAN 'n' and use\n" |
| 6391 | " the network script 'file' (default=%s);\n" |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 6392 | " use 'script=no' to disable script execution;\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6393 | " use 'fd=h' to connect to an already opened TAP interface\n" |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6394 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6395 | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6396 | " connect the vlan 'n' to another VLAN using a socket connection\n" |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 6397 | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" |
| 6398 | " connect the vlan 'n' to multicast maddr and port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6399 | "-net none use it alone to have zero network devices; if no -net option\n" |
| 6400 | " is provided, the default is '-net nic -net user'\n" |
| 6401 | "\n" |
| 6402 | #ifdef CONFIG_SLIRP |
ths | 0db1137 | 2007-02-20 00:12:07 +0000 | [diff] [blame] | 6403 | "-tftp dir allow tftp access to files in dir [-net user]\n" |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 6404 | "-bootp file advertise file in BOOTP replies\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6405 | #ifndef _WIN32 |
| 6406 | "-smb dir allow SMB access to files in 'dir' [-net user]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6407 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6408 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6409 | " redirect TCP or UDP connections from host to guest [-net user]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6410 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6411 | "\n" |
| 6412 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6413 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 6414 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 6415 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6416 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6417 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6418 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 6419 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6420 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6421 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6422 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 6423 | "-s wait gdb connection to port\n" |
| 6424 | "-p port set gdb connection port [default=%s]\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6425 | "-d item1,... output log to %s (use -d ? for a list of log items)\n" |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6426 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 6427 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | 87b4735 | 2006-08-17 17:22:54 +0000 | [diff] [blame] | 6428 | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6429 | #ifdef USE_KQEMU |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6430 | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6431 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 6432 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6433 | #ifdef USE_CODE_COPY |
| 6434 | "-no-code-copy disable code copy acceleration\n" |
| 6435 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6436 | #ifdef TARGET_I386 |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6437 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 6438 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6439 | "-no-acpi disable ACPI\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6440 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6441 | "-no-reboot exit instead of rebooting\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6442 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6443 | "-vnc display start a VNC server on display\n" |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6444 | #ifndef _WIN32 |
| 6445 | "-daemonize daemonize QEMU after initializing\n" |
| 6446 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6447 | "-option-rom rom load a file, rom, into the option ROM space\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6448 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6449 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 6450 | "ctrl-alt-f toggle full screen\n" |
| 6451 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 6452 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6453 | "\n" |
| 6454 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 6455 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6456 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6457 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6458 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6459 | DEFAULT_NETWORK_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6460 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 6461 | DEFAULT_GDBSTUB_PORT, |
| 6462 | "/tmp/qemu.log"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6463 | exit(1); |
| 6464 | } |
| 6465 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6466 | #define HAS_ARG 0x0001 |
| 6467 | |
| 6468 | enum { |
| 6469 | QEMU_OPTION_h, |
| 6470 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6471 | QEMU_OPTION_M, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6472 | QEMU_OPTION_cpu, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6473 | QEMU_OPTION_fda, |
| 6474 | QEMU_OPTION_fdb, |
| 6475 | QEMU_OPTION_hda, |
| 6476 | QEMU_OPTION_hdb, |
| 6477 | QEMU_OPTION_hdc, |
| 6478 | QEMU_OPTION_hdd, |
| 6479 | QEMU_OPTION_cdrom, |
| 6480 | QEMU_OPTION_boot, |
| 6481 | QEMU_OPTION_snapshot, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6482 | #ifdef TARGET_I386 |
| 6483 | QEMU_OPTION_no_fd_bootchk, |
| 6484 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6485 | QEMU_OPTION_m, |
| 6486 | QEMU_OPTION_nographic, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6487 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6488 | QEMU_OPTION_audio_help, |
| 6489 | QEMU_OPTION_soundhw, |
| 6490 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6491 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6492 | QEMU_OPTION_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6493 | QEMU_OPTION_tftp, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 6494 | QEMU_OPTION_bootp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6495 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6496 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6497 | |
| 6498 | QEMU_OPTION_kernel, |
| 6499 | QEMU_OPTION_append, |
| 6500 | QEMU_OPTION_initrd, |
| 6501 | |
| 6502 | QEMU_OPTION_S, |
| 6503 | QEMU_OPTION_s, |
| 6504 | QEMU_OPTION_p, |
| 6505 | QEMU_OPTION_d, |
| 6506 | QEMU_OPTION_hdachs, |
| 6507 | QEMU_OPTION_L, |
| 6508 | QEMU_OPTION_no_code_copy, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6509 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6510 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6511 | QEMU_OPTION_cirrusvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6512 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6513 | QEMU_OPTION_std_vga, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6514 | QEMU_OPTION_echr, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6515 | QEMU_OPTION_monitor, |
| 6516 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6517 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6518 | QEMU_OPTION_loadvm, |
| 6519 | QEMU_OPTION_full_screen, |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6520 | QEMU_OPTION_no_frame, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6521 | QEMU_OPTION_no_quit, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6522 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6523 | QEMU_OPTION_no_kqemu, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6524 | QEMU_OPTION_kernel_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6525 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 6526 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6527 | QEMU_OPTION_usbdevice, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6528 | QEMU_OPTION_smp, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6529 | QEMU_OPTION_vnc, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6530 | QEMU_OPTION_no_acpi, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6531 | QEMU_OPTION_no_reboot, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6532 | QEMU_OPTION_daemonize, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6533 | QEMU_OPTION_option_rom, |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 6534 | QEMU_OPTION_semihosting, |
| 6535 | QEMU_OPTION_name, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6536 | }; |
| 6537 | |
| 6538 | typedef struct QEMUOption { |
| 6539 | const char *name; |
| 6540 | int flags; |
| 6541 | int index; |
| 6542 | } QEMUOption; |
| 6543 | |
| 6544 | const QEMUOption qemu_options[] = { |
| 6545 | { "h", 0, QEMU_OPTION_h }, |
pbrook | 64423fb | 2007-01-27 17:11:41 +0000 | [diff] [blame] | 6546 | { "help", 0, QEMU_OPTION_h }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6547 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6548 | { "M", HAS_ARG, QEMU_OPTION_M }, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6549 | { "cpu", HAS_ARG, QEMU_OPTION_cpu }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6550 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 6551 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 6552 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 6553 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 6554 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 6555 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
| 6556 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
| 6557 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 6558 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6559 | #ifdef TARGET_I386 |
| 6560 | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
| 6561 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6562 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 6563 | { "nographic", 0, QEMU_OPTION_nographic }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6564 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6565 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6566 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 6567 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 6568 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6569 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6570 | { "net", HAS_ARG, QEMU_OPTION_net}, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6571 | #ifdef CONFIG_SLIRP |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6572 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 6573 | { "bootp", HAS_ARG, QEMU_OPTION_bootp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6574 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6575 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6576 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6577 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6578 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6579 | |
| 6580 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 6581 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 6582 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 6583 | |
| 6584 | { "S", 0, QEMU_OPTION_S }, |
| 6585 | { "s", 0, QEMU_OPTION_s }, |
| 6586 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 6587 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 6588 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 6589 | { "L", HAS_ARG, QEMU_OPTION_L }, |
| 6590 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6591 | #ifdef USE_KQEMU |
| 6592 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6593 | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6594 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6595 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6596 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 6597 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6598 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6599 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6600 | { "echr", 1, QEMU_OPTION_echr }, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6601 | { "monitor", 1, QEMU_OPTION_monitor }, |
| 6602 | { "serial", 1, QEMU_OPTION_serial }, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6603 | { "parallel", 1, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6604 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 6605 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6606 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6607 | { "no-frame", 0, QEMU_OPTION_no_frame }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6608 | { "no-quit", 0, QEMU_OPTION_no_quit }, |
| 6609 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6610 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6611 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6612 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6613 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6614 | { "vnc", HAS_ARG, QEMU_OPTION_vnc }, |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6615 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6616 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6617 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6618 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6619 | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6620 | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6621 | { "daemonize", 0, QEMU_OPTION_daemonize }, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6622 | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom }, |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 6623 | #if defined(TARGET_ARM) |
| 6624 | { "semihosting", 0, QEMU_OPTION_semihosting }, |
| 6625 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 6626 | { "name", HAS_ARG, QEMU_OPTION_name }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6627 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6628 | }; |
| 6629 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6630 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 6631 | |
| 6632 | /* this stack is only used during signal handling */ |
| 6633 | #define SIGNAL_STACK_SIZE 32768 |
| 6634 | |
| 6635 | static uint8_t *signal_stack; |
| 6636 | |
| 6637 | #endif |
| 6638 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6639 | /* password input */ |
| 6640 | |
| 6641 | static BlockDriverState *get_bdrv(int index) |
| 6642 | { |
| 6643 | BlockDriverState *bs; |
| 6644 | |
| 6645 | if (index < 4) { |
| 6646 | bs = bs_table[index]; |
| 6647 | } else if (index < 6) { |
| 6648 | bs = fd_table[index - 4]; |
| 6649 | } else { |
| 6650 | bs = NULL; |
| 6651 | } |
| 6652 | return bs; |
| 6653 | } |
| 6654 | |
| 6655 | static void read_passwords(void) |
| 6656 | { |
| 6657 | BlockDriverState *bs; |
| 6658 | int i, j; |
| 6659 | char password[256]; |
| 6660 | |
| 6661 | for(i = 0; i < 6; i++) { |
| 6662 | bs = get_bdrv(i); |
| 6663 | if (bs && bdrv_is_encrypted(bs)) { |
| 6664 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); |
| 6665 | for(j = 0; j < 3; j++) { |
| 6666 | monitor_readline("Password: ", |
| 6667 | 1, password, sizeof(password)); |
| 6668 | if (bdrv_set_key(bs, password) == 0) |
| 6669 | break; |
| 6670 | term_printf("invalid password\n"); |
| 6671 | } |
| 6672 | } |
| 6673 | } |
| 6674 | } |
| 6675 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6676 | /* XXX: currently we cannot use simultaneously different CPUs */ |
| 6677 | void register_machines(void) |
| 6678 | { |
| 6679 | #if defined(TARGET_I386) |
| 6680 | qemu_register_machine(&pc_machine); |
bellard | 3dbbdc2 | 2005-11-06 18:20:37 +0000 | [diff] [blame] | 6681 | qemu_register_machine(&isapc_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6682 | #elif defined(TARGET_PPC) |
| 6683 | qemu_register_machine(&heathrow_machine); |
| 6684 | qemu_register_machine(&core99_machine); |
| 6685 | qemu_register_machine(&prep_machine); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 6686 | #elif defined(TARGET_MIPS) |
| 6687 | qemu_register_machine(&mips_machine); |
ths | 5856de8 | 2007-01-15 23:58:11 +0000 | [diff] [blame] | 6688 | qemu_register_machine(&mips_malta_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6689 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6690 | #ifdef TARGET_SPARC64 |
| 6691 | qemu_register_machine(&sun4u_machine); |
| 6692 | #else |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6693 | qemu_register_machine(&sun4m_machine); |
| 6694 | #endif |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6695 | #elif defined(TARGET_ARM) |
pbrook | 3371d27 | 2007-03-08 03:04:12 +0000 | [diff] [blame] | 6696 | qemu_register_machine(&integratorcp_machine); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 6697 | qemu_register_machine(&versatilepb_machine); |
pbrook | 1640695 | 2006-04-27 23:15:07 +0000 | [diff] [blame] | 6698 | qemu_register_machine(&versatileab_machine); |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 6699 | qemu_register_machine(&realview_machine); |
bellard | 27c7ca7 | 2006-04-27 21:32:09 +0000 | [diff] [blame] | 6700 | #elif defined(TARGET_SH4) |
| 6701 | qemu_register_machine(&shix_machine); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6702 | #else |
| 6703 | #error unsupported CPU |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6704 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6707 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6708 | struct soundhw soundhw[] = { |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 6709 | #ifdef TARGET_I386 |
| 6710 | { |
| 6711 | "pcspk", |
| 6712 | "PC speaker", |
| 6713 | 0, |
| 6714 | 1, |
| 6715 | { .init_isa = pcspk_audio_init } |
| 6716 | }, |
| 6717 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6718 | { |
| 6719 | "sb16", |
| 6720 | "Creative Sound Blaster 16", |
| 6721 | 0, |
| 6722 | 1, |
| 6723 | { .init_isa = SB16_init } |
| 6724 | }, |
| 6725 | |
| 6726 | #ifdef CONFIG_ADLIB |
| 6727 | { |
| 6728 | "adlib", |
| 6729 | #ifdef HAS_YMF262 |
| 6730 | "Yamaha YMF262 (OPL3)", |
| 6731 | #else |
| 6732 | "Yamaha YM3812 (OPL2)", |
| 6733 | #endif |
| 6734 | 0, |
| 6735 | 1, |
| 6736 | { .init_isa = Adlib_init } |
| 6737 | }, |
| 6738 | #endif |
| 6739 | |
| 6740 | #ifdef CONFIG_GUS |
| 6741 | { |
| 6742 | "gus", |
| 6743 | "Gravis Ultrasound GF1", |
| 6744 | 0, |
| 6745 | 1, |
| 6746 | { .init_isa = GUS_init } |
| 6747 | }, |
| 6748 | #endif |
| 6749 | |
| 6750 | { |
| 6751 | "es1370", |
| 6752 | "ENSONIQ AudioPCI ES1370", |
| 6753 | 0, |
| 6754 | 0, |
| 6755 | { .init_pci = es1370_init } |
| 6756 | }, |
| 6757 | |
| 6758 | { NULL, NULL, 0, 0, { NULL } } |
| 6759 | }; |
| 6760 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6761 | static void select_soundhw (const char *optarg) |
| 6762 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6763 | struct soundhw *c; |
| 6764 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6765 | if (*optarg == '?') { |
| 6766 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6767 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6768 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6769 | for (c = soundhw; c->name; ++c) { |
| 6770 | printf ("%-11s %s\n", c->name, c->descr); |
| 6771 | } |
| 6772 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6773 | exit (*optarg != '?'); |
| 6774 | } |
| 6775 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6776 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6777 | const char *p; |
| 6778 | char *e; |
| 6779 | int bad_card = 0; |
| 6780 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6781 | if (!strcmp (optarg, "all")) { |
| 6782 | for (c = soundhw; c->name; ++c) { |
| 6783 | c->enabled = 1; |
| 6784 | } |
| 6785 | return; |
| 6786 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6787 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6788 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6789 | while (*p) { |
| 6790 | e = strchr (p, ','); |
| 6791 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6792 | |
| 6793 | for (c = soundhw; c->name; ++c) { |
| 6794 | if (!strncmp (c->name, p, l)) { |
| 6795 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6796 | break; |
| 6797 | } |
| 6798 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6799 | |
| 6800 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6801 | if (l > 80) { |
| 6802 | fprintf (stderr, |
| 6803 | "Unknown sound card name (too big to show)\n"); |
| 6804 | } |
| 6805 | else { |
| 6806 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 6807 | (int) l, p); |
| 6808 | } |
| 6809 | bad_card = 1; |
| 6810 | } |
| 6811 | p += l + (e != NULL); |
| 6812 | } |
| 6813 | |
| 6814 | if (bad_card) |
| 6815 | goto show_valid_cards; |
| 6816 | } |
| 6817 | } |
| 6818 | #endif |
| 6819 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6820 | #ifdef _WIN32 |
| 6821 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 6822 | { |
| 6823 | exit(STATUS_CONTROL_C_EXIT); |
| 6824 | return TRUE; |
| 6825 | } |
| 6826 | #endif |
| 6827 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6828 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6829 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6830 | int main(int argc, char **argv) |
| 6831 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6832 | #ifdef CONFIG_GDBSTUB |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 6833 | int use_gdbstub; |
| 6834 | const char *gdbstub_port; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6835 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6836 | int i, cdrom_index; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 6837 | int snapshot, linux_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 6838 | const char *initrd_filename; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6839 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6840 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6841 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6842 | int cyls, heads, secs, translation; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6843 | char net_clients[MAX_NET_CLIENTS][256]; |
| 6844 | int nb_net_clients; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6845 | int optind; |
| 6846 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6847 | CharDriverState *monitor_hd; |
| 6848 | char monitor_device[128]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6849 | char serial_devices[MAX_SERIAL_PORTS][128]; |
| 6850 | int serial_device_index; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6851 | char parallel_devices[MAX_PARALLEL_PORTS][128]; |
| 6852 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6853 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6854 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6855 | const char *cpu_model; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 6856 | char usb_devices[MAX_USB_CMDLINE][128]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6857 | int usb_devices_index; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6858 | int fds[2]; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 6859 | const char *pid_file = NULL; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6860 | |
| 6861 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6862 | #ifndef _WIN32 |
| 6863 | { |
| 6864 | struct sigaction act; |
| 6865 | sigfillset(&act.sa_mask); |
| 6866 | act.sa_flags = 0; |
| 6867 | act.sa_handler = SIG_IGN; |
| 6868 | sigaction(SIGPIPE, &act, NULL); |
| 6869 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6870 | #else |
| 6871 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 6872 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 6873 | QEMU to run on a single CPU */ |
| 6874 | { |
| 6875 | HANDLE h; |
| 6876 | DWORD mask, smask; |
| 6877 | int i; |
| 6878 | h = GetCurrentProcess(); |
| 6879 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 6880 | for(i = 0; i < 32; i++) { |
| 6881 | if (mask & (1 << i)) |
| 6882 | break; |
| 6883 | } |
| 6884 | if (i != 32) { |
| 6885 | mask = 1 << i; |
| 6886 | SetProcessAffinityMask(h, mask); |
| 6887 | } |
| 6888 | } |
| 6889 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6890 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6891 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6892 | register_machines(); |
| 6893 | machine = first_machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6894 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6895 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6896 | for(i = 0; i < MAX_FD; i++) |
| 6897 | fd_filename[i] = NULL; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6898 | for(i = 0; i < MAX_DISKS; i++) |
| 6899 | hd_filename[i] = NULL; |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6900 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6901 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6902 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6903 | use_gdbstub = 0; |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 6904 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6905 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6906 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6907 | nographic = 0; |
| 6908 | kernel_filename = NULL; |
| 6909 | kernel_cmdline = ""; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6910 | #ifdef TARGET_PPC |
| 6911 | cdrom_index = 1; |
| 6912 | #else |
| 6913 | cdrom_index = 2; |
| 6914 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6915 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6916 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6917 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6918 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6919 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
| 6920 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
| 6921 | serial_devices[i][0] = '\0'; |
| 6922 | serial_device_index = 0; |
| 6923 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6924 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
| 6925 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
| 6926 | parallel_devices[i][0] = '\0'; |
| 6927 | parallel_device_index = 0; |
| 6928 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6929 | usb_devices_index = 0; |
| 6930 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6931 | nb_net_clients = 0; |
| 6932 | |
| 6933 | nb_nics = 0; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 6934 | /* default mac address of the first network interface */ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6935 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6936 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6937 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6938 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6939 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6940 | r = argv[optind]; |
| 6941 | if (r[0] != '-') { |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6942 | hd_filename[0] = argv[optind++]; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6943 | } else { |
| 6944 | const QEMUOption *popt; |
| 6945 | |
| 6946 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 6947 | /* Treat --foo the same as -foo. */ |
| 6948 | if (r[1] == '-') |
| 6949 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6950 | popt = qemu_options; |
| 6951 | for(;;) { |
| 6952 | if (!popt->name) { |
| 6953 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 6954 | argv[0], r); |
| 6955 | exit(1); |
| 6956 | } |
| 6957 | if (!strcmp(popt->name, r + 1)) |
| 6958 | break; |
| 6959 | popt++; |
| 6960 | } |
| 6961 | if (popt->flags & HAS_ARG) { |
| 6962 | if (optind >= argc) { |
| 6963 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 6964 | argv[0], r); |
| 6965 | exit(1); |
| 6966 | } |
| 6967 | optarg = argv[optind++]; |
| 6968 | } else { |
| 6969 | optarg = NULL; |
| 6970 | } |
| 6971 | |
| 6972 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6973 | case QEMU_OPTION_M: |
| 6974 | machine = find_machine(optarg); |
| 6975 | if (!machine) { |
| 6976 | QEMUMachine *m; |
| 6977 | printf("Supported machines are:\n"); |
| 6978 | for(m = first_machine; m != NULL; m = m->next) { |
| 6979 | printf("%-10s %s%s\n", |
| 6980 | m->name, m->desc, |
| 6981 | m == first_machine ? " (default)" : ""); |
| 6982 | } |
| 6983 | exit(1); |
| 6984 | } |
| 6985 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6986 | case QEMU_OPTION_cpu: |
| 6987 | /* hw initialization will check this */ |
| 6988 | if (optarg[0] == '?') { |
| 6989 | #if defined(TARGET_PPC) |
| 6990 | ppc_cpu_list(stdout, &fprintf); |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 6991 | #elif defined(TARGET_ARM) |
| 6992 | arm_cpu_list(); |
ths | 33d68b5 | 2007-03-18 00:30:29 +0000 | [diff] [blame] | 6993 | #elif defined(TARGET_MIPS) |
| 6994 | mips_cpu_list(stdout, &fprintf); |
blueswir1 | 62724a3 | 2007-03-25 07:55:52 +0000 | [diff] [blame] | 6995 | #elif defined(TARGET_SPARC) |
| 6996 | sparc_cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 6997 | #endif |
| 6998 | exit(1); |
| 6999 | } else { |
| 7000 | cpu_model = optarg; |
| 7001 | } |
| 7002 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7003 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7004 | initrd_filename = optarg; |
| 7005 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7006 | case QEMU_OPTION_hda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7007 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7008 | case QEMU_OPTION_hdc: |
| 7009 | case QEMU_OPTION_hdd: |
| 7010 | { |
| 7011 | int hd_index; |
| 7012 | hd_index = popt->index - QEMU_OPTION_hda; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7013 | hd_filename[hd_index] = optarg; |
| 7014 | if (hd_index == cdrom_index) |
| 7015 | cdrom_index = -1; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7016 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7017 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7018 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7019 | snapshot = 1; |
| 7020 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7021 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7022 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7023 | const char *p; |
| 7024 | p = optarg; |
| 7025 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7026 | if (cyls < 1 || cyls > 16383) |
| 7027 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7028 | if (*p != ',') |
| 7029 | goto chs_fail; |
| 7030 | p++; |
| 7031 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7032 | if (heads < 1 || heads > 16) |
| 7033 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7034 | if (*p != ',') |
| 7035 | goto chs_fail; |
| 7036 | p++; |
| 7037 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7038 | if (secs < 1 || secs > 63) |
| 7039 | goto chs_fail; |
| 7040 | if (*p == ',') { |
| 7041 | p++; |
| 7042 | if (!strcmp(p, "none")) |
| 7043 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 7044 | else if (!strcmp(p, "lba")) |
| 7045 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 7046 | else if (!strcmp(p, "auto")) |
| 7047 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 7048 | else |
| 7049 | goto chs_fail; |
| 7050 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7051 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7052 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 7053 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7054 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7055 | } |
| 7056 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7057 | case QEMU_OPTION_nographic: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7058 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
ths | a39437a | 2007-03-25 20:27:04 +0000 | [diff] [blame] | 7059 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null"); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7060 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7061 | nographic = 1; |
| 7062 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7063 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7064 | kernel_filename = optarg; |
| 7065 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7066 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7067 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7068 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7069 | case QEMU_OPTION_cdrom: |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7070 | if (cdrom_index >= 0) { |
| 7071 | hd_filename[cdrom_index] = optarg; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7072 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7073 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7074 | case QEMU_OPTION_boot: |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7075 | boot_device = optarg[0]; |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 7076 | if (boot_device != 'a' && |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7077 | #if defined(TARGET_SPARC) || defined(TARGET_I386) |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 7078 | // Network boot |
| 7079 | boot_device != 'n' && |
| 7080 | #endif |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7081 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7082 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 7083 | exit(1); |
| 7084 | } |
| 7085 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7086 | case QEMU_OPTION_fda: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7087 | fd_filename[0] = optarg; |
| 7088 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7089 | case QEMU_OPTION_fdb: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7090 | fd_filename[1] = optarg; |
| 7091 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7092 | #ifdef TARGET_I386 |
| 7093 | case QEMU_OPTION_no_fd_bootchk: |
| 7094 | fd_bootchk = 0; |
| 7095 | break; |
| 7096 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7097 | case QEMU_OPTION_no_code_copy: |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 7098 | code_copy_enabled = 0; |
| 7099 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7100 | case QEMU_OPTION_net: |
| 7101 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 7102 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7103 | exit(1); |
| 7104 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7105 | pstrcpy(net_clients[nb_net_clients], |
| 7106 | sizeof(net_clients[0]), |
| 7107 | optarg); |
| 7108 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7109 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7110 | #ifdef CONFIG_SLIRP |
| 7111 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7112 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7113 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 7114 | case QEMU_OPTION_bootp: |
| 7115 | bootp_filename = optarg; |
| 7116 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7117 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7118 | case QEMU_OPTION_smb: |
| 7119 | net_slirp_smb(optarg); |
| 7120 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7121 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7122 | case QEMU_OPTION_redir: |
| 7123 | net_slirp_redir(optarg); |
| 7124 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7125 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7126 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7127 | case QEMU_OPTION_audio_help: |
| 7128 | AUD_help (); |
| 7129 | exit (0); |
| 7130 | break; |
| 7131 | case QEMU_OPTION_soundhw: |
| 7132 | select_soundhw (optarg); |
| 7133 | break; |
| 7134 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7135 | case QEMU_OPTION_h: |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7136 | help(); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7137 | break; |
| 7138 | case QEMU_OPTION_m: |
| 7139 | ram_size = atoi(optarg) * 1024 * 1024; |
| 7140 | if (ram_size <= 0) |
| 7141 | help(); |
| 7142 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
| 7143 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
| 7144 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 7145 | exit(1); |
| 7146 | } |
| 7147 | break; |
| 7148 | case QEMU_OPTION_d: |
| 7149 | { |
| 7150 | int mask; |
| 7151 | CPULogItem *item; |
| 7152 | |
| 7153 | mask = cpu_str_to_log_mask(optarg); |
| 7154 | if (!mask) { |
| 7155 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7156 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 7157 | printf("%-10s %s\n", item->name, item->help); |
| 7158 | } |
| 7159 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7160 | } |
| 7161 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7162 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7163 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7164 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7165 | case QEMU_OPTION_s: |
| 7166 | use_gdbstub = 1; |
| 7167 | break; |
| 7168 | case QEMU_OPTION_p: |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 7169 | gdbstub_port = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7170 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7171 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7172 | case QEMU_OPTION_L: |
| 7173 | bios_dir = optarg; |
| 7174 | break; |
| 7175 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7176 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7177 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 7178 | case QEMU_OPTION_k: |
| 7179 | keyboard_layout = optarg; |
| 7180 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 7181 | case QEMU_OPTION_localtime: |
| 7182 | rtc_utc = 0; |
| 7183 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7184 | case QEMU_OPTION_cirrusvga: |
| 7185 | cirrus_vga_enabled = 1; |
| 7186 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7187 | case QEMU_OPTION_std_vga: |
| 7188 | cirrus_vga_enabled = 0; |
| 7189 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 7190 | case QEMU_OPTION_g: |
| 7191 | { |
| 7192 | const char *p; |
| 7193 | int w, h, depth; |
| 7194 | p = optarg; |
| 7195 | w = strtol(p, (char **)&p, 10); |
| 7196 | if (w <= 0) { |
| 7197 | graphic_error: |
| 7198 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 7199 | exit(1); |
| 7200 | } |
| 7201 | if (*p != 'x') |
| 7202 | goto graphic_error; |
| 7203 | p++; |
| 7204 | h = strtol(p, (char **)&p, 10); |
| 7205 | if (h <= 0) |
| 7206 | goto graphic_error; |
| 7207 | if (*p == 'x') { |
| 7208 | p++; |
| 7209 | depth = strtol(p, (char **)&p, 10); |
| 7210 | if (depth != 8 && depth != 15 && depth != 16 && |
| 7211 | depth != 24 && depth != 32) |
| 7212 | goto graphic_error; |
| 7213 | } else if (*p == '\0') { |
| 7214 | depth = graphic_depth; |
| 7215 | } else { |
| 7216 | goto graphic_error; |
| 7217 | } |
| 7218 | |
| 7219 | graphic_width = w; |
| 7220 | graphic_height = h; |
| 7221 | graphic_depth = depth; |
| 7222 | } |
| 7223 | break; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7224 | case QEMU_OPTION_echr: |
| 7225 | { |
| 7226 | char *r; |
| 7227 | term_escape_char = strtol(optarg, &r, 0); |
| 7228 | if (r == optarg) |
| 7229 | printf("Bad argument to echr\n"); |
| 7230 | break; |
| 7231 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7232 | case QEMU_OPTION_monitor: |
| 7233 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); |
| 7234 | break; |
| 7235 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7236 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 7237 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 7238 | exit(1); |
| 7239 | } |
| 7240 | pstrcpy(serial_devices[serial_device_index], |
| 7241 | sizeof(serial_devices[0]), optarg); |
| 7242 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7243 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7244 | case QEMU_OPTION_parallel: |
| 7245 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 7246 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 7247 | exit(1); |
| 7248 | } |
| 7249 | pstrcpy(parallel_devices[parallel_device_index], |
| 7250 | sizeof(parallel_devices[0]), optarg); |
| 7251 | parallel_device_index++; |
| 7252 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7253 | case QEMU_OPTION_loadvm: |
| 7254 | loadvm = optarg; |
| 7255 | break; |
| 7256 | case QEMU_OPTION_full_screen: |
| 7257 | full_screen = 1; |
| 7258 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7259 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7260 | case QEMU_OPTION_no_frame: |
| 7261 | no_frame = 1; |
| 7262 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7263 | case QEMU_OPTION_no_quit: |
| 7264 | no_quit = 1; |
| 7265 | break; |
| 7266 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7267 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 7268 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7269 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7270 | #ifdef TARGET_I386 |
| 7271 | case QEMU_OPTION_win2k_hack: |
| 7272 | win2k_install_hack = 1; |
| 7273 | break; |
| 7274 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7275 | #ifdef USE_KQEMU |
| 7276 | case QEMU_OPTION_no_kqemu: |
| 7277 | kqemu_allowed = 0; |
| 7278 | break; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7279 | case QEMU_OPTION_kernel_kqemu: |
| 7280 | kqemu_allowed = 2; |
| 7281 | break; |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7282 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 7283 | case QEMU_OPTION_usb: |
| 7284 | usb_enabled = 1; |
| 7285 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7286 | case QEMU_OPTION_usbdevice: |
| 7287 | usb_enabled = 1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7288 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7289 | fprintf(stderr, "Too many USB devices\n"); |
| 7290 | exit(1); |
| 7291 | } |
| 7292 | pstrcpy(usb_devices[usb_devices_index], |
| 7293 | sizeof(usb_devices[usb_devices_index]), |
| 7294 | optarg); |
| 7295 | usb_devices_index++; |
| 7296 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7297 | case QEMU_OPTION_smp: |
| 7298 | smp_cpus = atoi(optarg); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 7299 | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7300 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 7301 | exit(1); |
| 7302 | } |
| 7303 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7304 | case QEMU_OPTION_vnc: |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7305 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7306 | break; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7307 | case QEMU_OPTION_no_acpi: |
| 7308 | acpi_enabled = 0; |
| 7309 | break; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7310 | case QEMU_OPTION_no_reboot: |
| 7311 | no_reboot = 1; |
| 7312 | break; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7313 | case QEMU_OPTION_daemonize: |
| 7314 | daemonize = 1; |
| 7315 | break; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7316 | case QEMU_OPTION_option_rom: |
| 7317 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 7318 | fprintf(stderr, "Too many option ROMs\n"); |
| 7319 | exit(1); |
| 7320 | } |
| 7321 | option_rom[nb_option_roms] = optarg; |
| 7322 | nb_option_roms++; |
| 7323 | break; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 7324 | case QEMU_OPTION_semihosting: |
| 7325 | semihosting_enabled = 1; |
| 7326 | break; |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 7327 | case QEMU_OPTION_name: |
| 7328 | qemu_name = optarg; |
| 7329 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7330 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7331 | } |
| 7332 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7333 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7334 | #ifndef _WIN32 |
| 7335 | if (daemonize && !nographic && vnc_display == NULL) { |
| 7336 | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n"); |
| 7337 | daemonize = 0; |
| 7338 | } |
| 7339 | |
| 7340 | if (daemonize) { |
| 7341 | pid_t pid; |
| 7342 | |
| 7343 | if (pipe(fds) == -1) |
| 7344 | exit(1); |
| 7345 | |
| 7346 | pid = fork(); |
| 7347 | if (pid > 0) { |
| 7348 | uint8_t status; |
| 7349 | ssize_t len; |
| 7350 | |
| 7351 | close(fds[1]); |
| 7352 | |
| 7353 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 7354 | len = read(fds[0], &status, 1); |
| 7355 | if (len == -1 && (errno == EINTR)) |
| 7356 | goto again; |
| 7357 | |
| 7358 | if (len != 1) |
| 7359 | exit(1); |
| 7360 | else if (status == 1) { |
| 7361 | fprintf(stderr, "Could not acquire pidfile\n"); |
| 7362 | exit(1); |
| 7363 | } else |
| 7364 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7365 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 7366 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7367 | |
| 7368 | setsid(); |
| 7369 | |
| 7370 | pid = fork(); |
| 7371 | if (pid > 0) |
| 7372 | exit(0); |
| 7373 | else if (pid < 0) |
| 7374 | exit(1); |
| 7375 | |
| 7376 | umask(027); |
| 7377 | chdir("/"); |
| 7378 | |
| 7379 | signal(SIGTSTP, SIG_IGN); |
| 7380 | signal(SIGTTOU, SIG_IGN); |
| 7381 | signal(SIGTTIN, SIG_IGN); |
| 7382 | } |
| 7383 | #endif |
| 7384 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame^] | 7385 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 7386 | if (daemonize) { |
| 7387 | uint8_t status = 1; |
| 7388 | write(fds[1], &status, 1); |
| 7389 | } else |
| 7390 | fprintf(stderr, "Could not acquire pid file\n"); |
| 7391 | exit(1); |
| 7392 | } |
| 7393 | |
bellard | ff3fbb3 | 2006-01-08 10:53:14 +0000 | [diff] [blame] | 7394 | #ifdef USE_KQEMU |
| 7395 | if (smp_cpus > 1) |
| 7396 | kqemu_allowed = 0; |
| 7397 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7398 | linux_boot = (kernel_filename != NULL); |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 7399 | |
| 7400 | if (!linux_boot && |
ths | d84fe7a | 2007-02-19 01:10:26 +0000 | [diff] [blame] | 7401 | boot_device != 'n' && |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7402 | hd_filename[0] == '\0' && |
| 7403 | (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7404 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7405 | help(); |
| 7406 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7407 | /* boot to floppy or the default cd if no hard disk defined yet */ |
| 7408 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 7409 | if (fd_filename[0] != '\0') |
| 7410 | boot_device = 'a'; |
| 7411 | else |
| 7412 | boot_device = 'd'; |
| 7413 | } |
| 7414 | |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 7415 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7416 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7417 | init_timers(); |
| 7418 | init_timer_alarm(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7419 | qemu_aio_init(); |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7420 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7421 | #ifdef _WIN32 |
| 7422 | socket_init(); |
| 7423 | #endif |
| 7424 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7425 | /* init network clients */ |
| 7426 | if (nb_net_clients == 0) { |
| 7427 | /* if no clients, we use a default config */ |
| 7428 | pstrcpy(net_clients[0], sizeof(net_clients[0]), |
| 7429 | "nic"); |
| 7430 | pstrcpy(net_clients[1], sizeof(net_clients[0]), |
| 7431 | "user"); |
| 7432 | nb_net_clients = 2; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7435 | for(i = 0;i < nb_net_clients; i++) { |
| 7436 | if (net_client_init(net_clients[i]) < 0) |
| 7437 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7438 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 7439 | |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7440 | #ifdef TARGET_I386 |
| 7441 | if (boot_device == 'n') { |
| 7442 | for (i = 0; i < nb_nics; i++) { |
| 7443 | const char *model = nd_table[i].model; |
| 7444 | char buf[1024]; |
| 7445 | if (model == NULL) |
| 7446 | model = "ne2k_pci"; |
| 7447 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
| 7448 | if (get_image_size(buf) > 0) { |
| 7449 | option_rom[nb_option_roms] = strdup(buf); |
| 7450 | nb_option_roms++; |
| 7451 | break; |
| 7452 | } |
| 7453 | } |
| 7454 | if (i == nb_nics) { |
| 7455 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
| 7456 | exit(1); |
| 7457 | } |
| 7458 | boot_device = 'c'; /* to prevent confusion by the BIOS */ |
| 7459 | } |
| 7460 | #endif |
| 7461 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7462 | /* init the memory */ |
bellard | 970ac5a | 2007-02-08 23:09:59 +0000 | [diff] [blame] | 7463 | phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7464 | |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7465 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7466 | if (!phys_ram_base) { |
| 7467 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7468 | exit(1); |
| 7469 | } |
| 7470 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7471 | /* we always create the cdrom drive, even if no disk is there */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7472 | bdrv_init(); |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7473 | if (cdrom_index >= 0) { |
| 7474 | bs_table[cdrom_index] = bdrv_new("cdrom"); |
| 7475 | bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7478 | /* open the virtual block devices */ |
| 7479 | for(i = 0; i < MAX_DISKS; i++) { |
| 7480 | if (hd_filename[i]) { |
| 7481 | if (!bs_table[i]) { |
| 7482 | char buf[64]; |
| 7483 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 7484 | bs_table[i] = bdrv_new(buf); |
| 7485 | } |
| 7486 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
| 7487 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", |
| 7488 | hd_filename[i]); |
| 7489 | exit(1); |
| 7490 | } |
| 7491 | if (i == 0 && cyls != 0) { |
| 7492 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
| 7493 | bdrv_set_translation_hint(bs_table[i], translation); |
| 7494 | } |
| 7495 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7496 | } |
| 7497 | |
| 7498 | /* we always create at least one floppy disk */ |
| 7499 | fd_table[0] = bdrv_new("fda"); |
| 7500 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 7501 | |
| 7502 | for(i = 0; i < MAX_FD; i++) { |
| 7503 | if (fd_filename[i]) { |
| 7504 | if (!fd_table[i]) { |
| 7505 | char buf[64]; |
| 7506 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 7507 | fd_table[i] = bdrv_new(buf); |
| 7508 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 7509 | } |
| 7510 | if (fd_filename[i] != '\0') { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7511 | if (bdrv_open(fd_table[i], fd_filename[i], |
| 7512 | snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7513 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7514 | fd_filename[i]); |
| 7515 | exit(1); |
| 7516 | } |
| 7517 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7518 | } |
| 7519 | } |
| 7520 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 7521 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
| 7522 | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7523 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7524 | init_ioports(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7525 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7526 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7527 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7528 | dumb_display_init(ds); |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7529 | } else if (vnc_display != NULL) { |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7530 | vnc_display_init(ds, vnc_display); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7531 | } else { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7532 | #if defined(CONFIG_SDL) |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7533 | sdl_display_init(ds, full_screen, no_frame); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7534 | #elif defined(CONFIG_COCOA) |
| 7535 | cocoa_display_init(ds, full_screen); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7536 | #else |
| 7537 | dumb_display_init(ds); |
| 7538 | #endif |
| 7539 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7540 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7541 | /* Maintain compatibility with multiple stdio monitors */ |
| 7542 | if (!strcmp(monitor_device,"stdio")) { |
| 7543 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 7544 | if (!strcmp(serial_devices[i],"mon:stdio")) { |
| 7545 | monitor_device[0] = '\0'; |
| 7546 | break; |
| 7547 | } else if (!strcmp(serial_devices[i],"stdio")) { |
| 7548 | monitor_device[0] = '\0'; |
| 7549 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio"); |
| 7550 | break; |
| 7551 | } |
| 7552 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7553 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7554 | if (monitor_device[0] != '\0') { |
| 7555 | monitor_hd = qemu_chr_open(monitor_device); |
| 7556 | if (!monitor_hd) { |
| 7557 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 7558 | exit(1); |
| 7559 | } |
| 7560 | monitor_init(monitor_hd, !nographic); |
| 7561 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7562 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7563 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7564 | const char *devname = serial_devices[i]; |
| 7565 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7566 | serial_hds[i] = qemu_chr_open(devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7567 | if (!serial_hds[i]) { |
| 7568 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7569 | devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7570 | exit(1); |
| 7571 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7572 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7573 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7574 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7575 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7576 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7577 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7578 | const char *devname = parallel_devices[i]; |
| 7579 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7580 | parallel_hds[i] = qemu_chr_open(devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7581 | if (!parallel_hds[i]) { |
| 7582 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7583 | devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7584 | exit(1); |
| 7585 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7586 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7587 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7588 | } |
| 7589 | } |
| 7590 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7591 | machine->init(ram_size, vga_ram_size, boot_device, |
| 7592 | ds, fd_filename, snapshot, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7593 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 7594 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7595 | /* init USB devices */ |
| 7596 | if (usb_enabled) { |
| 7597 | for(i = 0; i < usb_devices_index; i++) { |
| 7598 | if (usb_device_add(usb_devices[i]) < 0) { |
| 7599 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 7600 | usb_devices[i]); |
| 7601 | } |
| 7602 | } |
| 7603 | } |
| 7604 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7605 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
| 7606 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7607 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7608 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7609 | if (use_gdbstub) { |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7610 | /* XXX: use standard host:port notation and modify options |
| 7611 | accordingly. */ |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 7612 | if (gdbserver_start(gdbstub_port) < 0) { |
| 7613 | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n", |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7614 | gdbstub_port); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7615 | exit(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7616 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7617 | } else |
| 7618 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7619 | if (loadvm) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 7620 | do_loadvm(loadvm); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7621 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7622 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7623 | /* XXX: simplify init */ |
| 7624 | read_passwords(); |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7625 | if (autostart) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7626 | vm_start(); |
| 7627 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7628 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 7629 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7630 | if (daemonize) { |
| 7631 | uint8_t status = 0; |
| 7632 | ssize_t len; |
| 7633 | int fd; |
| 7634 | |
| 7635 | again1: |
| 7636 | len = write(fds[1], &status, 1); |
| 7637 | if (len == -1 && (errno == EINTR)) |
| 7638 | goto again1; |
| 7639 | |
| 7640 | if (len != 1) |
| 7641 | exit(1); |
| 7642 | |
| 7643 | fd = open("/dev/null", O_RDWR); |
| 7644 | if (fd == -1) |
| 7645 | exit(1); |
| 7646 | |
| 7647 | dup2(fd, 0); |
| 7648 | dup2(fd, 1); |
| 7649 | dup2(fd, 2); |
| 7650 | |
| 7651 | close(fd); |
| 7652 | } |
| 7653 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7654 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 7655 | quit_timers(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7656 | return 0; |
| 7657 | } |