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; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 192 | |
| 193 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 194 | /* x86 ISA bus support */ |
| 195 | |
| 196 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 197 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 198 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 199 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 200 | { |
| 201 | #ifdef DEBUG_UNUSED_IOPORT |
| 202 | fprintf(stderr, "inb: port=0x%04x\n", address); |
| 203 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 204 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 205 | } |
| 206 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 207 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 208 | { |
| 209 | #ifdef DEBUG_UNUSED_IOPORT |
| 210 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 215 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 216 | { |
| 217 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 218 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 219 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 220 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 221 | return data; |
| 222 | } |
| 223 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 224 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 225 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 226 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 227 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 228 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 231 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 232 | { |
| 233 | #ifdef DEBUG_UNUSED_IOPORT |
| 234 | fprintf(stderr, "inl: port=0x%04x\n", address); |
| 235 | #endif |
| 236 | return 0xffffffff; |
| 237 | } |
| 238 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 239 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 240 | { |
| 241 | #ifdef DEBUG_UNUSED_IOPORT |
| 242 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); |
| 243 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void init_ioports(void) |
| 247 | { |
| 248 | int i; |
| 249 | |
| 250 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 251 | ioport_read_table[0][i] = default_ioport_readb; |
| 252 | ioport_write_table[0][i] = default_ioport_writeb; |
| 253 | ioport_read_table[1][i] = default_ioport_readw; |
| 254 | ioport_write_table[1][i] = default_ioport_writew; |
| 255 | ioport_read_table[2][i] = default_ioport_readl; |
| 256 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 260 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 261 | int register_ioport_read(int start, int length, int size, |
| 262 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 263 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 264 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 265 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 266 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 267 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 268 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 269 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 270 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 271 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 272 | } else { |
| 273 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 274 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 275 | } |
| 276 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 277 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 278 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 279 | hw_error("register_ioport_read: invalid opaque"); |
| 280 | ioport_opaque[i] = opaque; |
| 281 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 285 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 286 | int register_ioport_write(int start, int length, int size, |
| 287 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 288 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 289 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 290 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 291 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 292 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 293 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 294 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 295 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 296 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 297 | } else { |
| 298 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 299 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 300 | } |
| 301 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 302 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 303 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 304 | hw_error("register_ioport_write: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 305 | ioport_opaque[i] = opaque; |
| 306 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 310 | void isa_unassign_ioport(int start, int length) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for(i = start; i < start + length; i++) { |
| 315 | ioport_read_table[0][i] = default_ioport_readb; |
| 316 | ioport_read_table[1][i] = default_ioport_readw; |
| 317 | ioport_read_table[2][i] = default_ioport_readl; |
| 318 | |
| 319 | ioport_write_table[0][i] = default_ioport_writeb; |
| 320 | ioport_write_table[1][i] = default_ioport_writew; |
| 321 | ioport_write_table[2][i] = default_ioport_writel; |
| 322 | } |
| 323 | } |
| 324 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 325 | /***********************************************************/ |
| 326 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 327 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 328 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 329 | #ifdef DEBUG_IOPORT |
| 330 | if (loglevel & CPU_LOG_IOPORT) |
| 331 | fprintf(logfile, "outb: %04x %02x\n", addr, val); |
| 332 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 333 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 334 | #ifdef USE_KQEMU |
| 335 | if (env) |
| 336 | env->last_io_time = cpu_get_time_fast(); |
| 337 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 338 | } |
| 339 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 340 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 341 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 342 | #ifdef DEBUG_IOPORT |
| 343 | if (loglevel & CPU_LOG_IOPORT) |
| 344 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
| 345 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 346 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 347 | #ifdef USE_KQEMU |
| 348 | if (env) |
| 349 | env->last_io_time = cpu_get_time_fast(); |
| 350 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 351 | } |
| 352 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 353 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 354 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 355 | #ifdef DEBUG_IOPORT |
| 356 | if (loglevel & CPU_LOG_IOPORT) |
| 357 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 358 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 359 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 360 | #ifdef USE_KQEMU |
| 361 | if (env) |
| 362 | env->last_io_time = cpu_get_time_fast(); |
| 363 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 364 | } |
| 365 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 366 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 367 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 368 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 369 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 370 | #ifdef DEBUG_IOPORT |
| 371 | if (loglevel & CPU_LOG_IOPORT) |
| 372 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 373 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 374 | #ifdef USE_KQEMU |
| 375 | if (env) |
| 376 | env->last_io_time = cpu_get_time_fast(); |
| 377 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 378 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 379 | } |
| 380 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 381 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 382 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 383 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 384 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 385 | #ifdef DEBUG_IOPORT |
| 386 | if (loglevel & CPU_LOG_IOPORT) |
| 387 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 388 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 389 | #ifdef USE_KQEMU |
| 390 | if (env) |
| 391 | env->last_io_time = cpu_get_time_fast(); |
| 392 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 393 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 394 | } |
| 395 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 396 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 397 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 398 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 399 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 400 | #ifdef DEBUG_IOPORT |
| 401 | if (loglevel & CPU_LOG_IOPORT) |
| 402 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 403 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 404 | #ifdef USE_KQEMU |
| 405 | if (env) |
| 406 | env->last_io_time = cpu_get_time_fast(); |
| 407 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 408 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 412 | void hw_error(const char *fmt, ...) |
| 413 | { |
| 414 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 415 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 416 | |
| 417 | va_start(ap, fmt); |
| 418 | fprintf(stderr, "qemu: hardware error: "); |
| 419 | vfprintf(stderr, fmt, ap); |
| 420 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 421 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 422 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 423 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 424 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 425 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 426 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 427 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 428 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 429 | va_end(ap); |
| 430 | abort(); |
| 431 | } |
| 432 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 433 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 434 | /* keyboard/mouse */ |
| 435 | |
| 436 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 437 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 438 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 439 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 440 | |
| 441 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 442 | { |
| 443 | qemu_put_kbd_event_opaque = opaque; |
| 444 | qemu_put_kbd_event = func; |
| 445 | } |
| 446 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 447 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 448 | void *opaque, int absolute, |
| 449 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 450 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 451 | QEMUPutMouseEntry *s, *cursor; |
| 452 | |
| 453 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 454 | if (!s) |
| 455 | return NULL; |
| 456 | |
| 457 | s->qemu_put_mouse_event = func; |
| 458 | s->qemu_put_mouse_event_opaque = opaque; |
| 459 | s->qemu_put_mouse_event_absolute = absolute; |
| 460 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 461 | s->next = NULL; |
| 462 | |
| 463 | if (!qemu_put_mouse_event_head) { |
| 464 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 465 | return s; |
| 466 | } |
| 467 | |
| 468 | cursor = qemu_put_mouse_event_head; |
| 469 | while (cursor->next != NULL) |
| 470 | cursor = cursor->next; |
| 471 | |
| 472 | cursor->next = s; |
| 473 | qemu_put_mouse_event_current = s; |
| 474 | |
| 475 | return s; |
| 476 | } |
| 477 | |
| 478 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 479 | { |
| 480 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 481 | |
| 482 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 483 | return; |
| 484 | |
| 485 | cursor = qemu_put_mouse_event_head; |
| 486 | while (cursor != NULL && cursor != entry) { |
| 487 | prev = cursor; |
| 488 | cursor = cursor->next; |
| 489 | } |
| 490 | |
| 491 | if (cursor == NULL) // does not exist or list empty |
| 492 | return; |
| 493 | else if (prev == NULL) { // entry is head |
| 494 | qemu_put_mouse_event_head = cursor->next; |
| 495 | if (qemu_put_mouse_event_current == entry) |
| 496 | qemu_put_mouse_event_current = cursor->next; |
| 497 | qemu_free(entry->qemu_put_mouse_event_name); |
| 498 | qemu_free(entry); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | prev->next = entry->next; |
| 503 | |
| 504 | if (qemu_put_mouse_event_current == entry) |
| 505 | qemu_put_mouse_event_current = prev; |
| 506 | |
| 507 | qemu_free(entry->qemu_put_mouse_event_name); |
| 508 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void kbd_put_keycode(int keycode) |
| 512 | { |
| 513 | if (qemu_put_kbd_event) { |
| 514 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 519 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 520 | QEMUPutMouseEvent *mouse_event; |
| 521 | void *mouse_event_opaque; |
| 522 | |
| 523 | if (!qemu_put_mouse_event_current) { |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | mouse_event = |
| 528 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 529 | mouse_event_opaque = |
| 530 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 531 | |
| 532 | if (mouse_event) { |
| 533 | mouse_event(mouse_event_opaque, dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 537 | int kbd_mouse_is_absolute(void) |
| 538 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 539 | if (!qemu_put_mouse_event_current) |
| 540 | return 0; |
| 541 | |
| 542 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 543 | } |
| 544 | |
| 545 | void do_info_mice(void) |
| 546 | { |
| 547 | QEMUPutMouseEntry *cursor; |
| 548 | int index = 0; |
| 549 | |
| 550 | if (!qemu_put_mouse_event_head) { |
| 551 | term_printf("No mouse devices connected\n"); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | term_printf("Mouse devices available:\n"); |
| 556 | cursor = qemu_put_mouse_event_head; |
| 557 | while (cursor != NULL) { |
| 558 | term_printf("%c Mouse #%d: %s\n", |
| 559 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 560 | index, cursor->qemu_put_mouse_event_name); |
| 561 | index++; |
| 562 | cursor = cursor->next; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | void do_mouse_set(int index) |
| 567 | { |
| 568 | QEMUPutMouseEntry *cursor; |
| 569 | int i = 0; |
| 570 | |
| 571 | if (!qemu_put_mouse_event_head) { |
| 572 | term_printf("No mouse devices connected\n"); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | cursor = qemu_put_mouse_event_head; |
| 577 | while (cursor != NULL && index != i) { |
| 578 | i++; |
| 579 | cursor = cursor->next; |
| 580 | } |
| 581 | |
| 582 | if (cursor != NULL) |
| 583 | qemu_put_mouse_event_current = cursor; |
| 584 | else |
| 585 | term_printf("Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 586 | } |
| 587 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 588 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 589 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 590 | { |
| 591 | union { |
| 592 | uint64_t ll; |
| 593 | struct { |
| 594 | #ifdef WORDS_BIGENDIAN |
| 595 | uint32_t high, low; |
| 596 | #else |
| 597 | uint32_t low, high; |
| 598 | #endif |
| 599 | } l; |
| 600 | } u, res; |
| 601 | uint64_t rl, rh; |
| 602 | |
| 603 | u.ll = a; |
| 604 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 605 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 606 | rh += (rl >> 32); |
| 607 | res.l.high = rh / c; |
| 608 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 609 | return res.ll; |
| 610 | } |
| 611 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 612 | /***********************************************************/ |
| 613 | /* real time host monotonic timer */ |
| 614 | |
| 615 | #define QEMU_TIMER_BASE 1000000000LL |
| 616 | |
| 617 | #ifdef WIN32 |
| 618 | |
| 619 | static int64_t clock_freq; |
| 620 | |
| 621 | static void init_get_clock(void) |
| 622 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 623 | LARGE_INTEGER freq; |
| 624 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 625 | ret = QueryPerformanceFrequency(&freq); |
| 626 | if (ret == 0) { |
| 627 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 628 | exit(1); |
| 629 | } |
| 630 | clock_freq = freq.QuadPart; |
| 631 | } |
| 632 | |
| 633 | static int64_t get_clock(void) |
| 634 | { |
| 635 | LARGE_INTEGER ti; |
| 636 | QueryPerformanceCounter(&ti); |
| 637 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 638 | } |
| 639 | |
| 640 | #else |
| 641 | |
| 642 | static int use_rt_clock; |
| 643 | |
| 644 | static void init_get_clock(void) |
| 645 | { |
| 646 | use_rt_clock = 0; |
| 647 | #if defined(__linux__) |
| 648 | { |
| 649 | struct timespec ts; |
| 650 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 651 | use_rt_clock = 1; |
| 652 | } |
| 653 | } |
| 654 | #endif |
| 655 | } |
| 656 | |
| 657 | static int64_t get_clock(void) |
| 658 | { |
| 659 | #if defined(__linux__) |
| 660 | if (use_rt_clock) { |
| 661 | struct timespec ts; |
| 662 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 663 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| 664 | } else |
| 665 | #endif |
| 666 | { |
| 667 | /* XXX: using gettimeofday leads to problems if the date |
| 668 | changes, so it should be avoided. */ |
| 669 | struct timeval tv; |
| 670 | gettimeofday(&tv, NULL); |
| 671 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | #endif |
| 676 | |
| 677 | /***********************************************************/ |
| 678 | /* guest cycle counter */ |
| 679 | |
| 680 | static int64_t cpu_ticks_prev; |
| 681 | static int64_t cpu_ticks_offset; |
| 682 | static int64_t cpu_clock_offset; |
| 683 | static int cpu_ticks_enabled; |
| 684 | |
| 685 | /* return the host CPU cycle counter and handle stop/restart */ |
| 686 | int64_t cpu_get_ticks(void) |
| 687 | { |
| 688 | if (!cpu_ticks_enabled) { |
| 689 | return cpu_ticks_offset; |
| 690 | } else { |
| 691 | int64_t ticks; |
| 692 | ticks = cpu_get_real_ticks(); |
| 693 | if (cpu_ticks_prev > ticks) { |
| 694 | /* Note: non increasing ticks may happen if the host uses |
| 695 | software suspend */ |
| 696 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 697 | } |
| 698 | cpu_ticks_prev = ticks; |
| 699 | return ticks + cpu_ticks_offset; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 704 | static int64_t cpu_get_clock(void) |
| 705 | { |
| 706 | int64_t ti; |
| 707 | if (!cpu_ticks_enabled) { |
| 708 | return cpu_clock_offset; |
| 709 | } else { |
| 710 | ti = get_clock(); |
| 711 | return ti + cpu_clock_offset; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /* enable cpu_get_ticks() */ |
| 716 | void cpu_enable_ticks(void) |
| 717 | { |
| 718 | if (!cpu_ticks_enabled) { |
| 719 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 720 | cpu_clock_offset -= get_clock(); |
| 721 | cpu_ticks_enabled = 1; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 726 | cpu_get_ticks() after that. */ |
| 727 | void cpu_disable_ticks(void) |
| 728 | { |
| 729 | if (cpu_ticks_enabled) { |
| 730 | cpu_ticks_offset = cpu_get_ticks(); |
| 731 | cpu_clock_offset = cpu_get_clock(); |
| 732 | cpu_ticks_enabled = 0; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /***********************************************************/ |
| 737 | /* timers */ |
| 738 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 739 | #define QEMU_TIMER_REALTIME 0 |
| 740 | #define QEMU_TIMER_VIRTUAL 1 |
| 741 | |
| 742 | struct QEMUClock { |
| 743 | int type; |
| 744 | /* XXX: add frequency */ |
| 745 | }; |
| 746 | |
| 747 | struct QEMUTimer { |
| 748 | QEMUClock *clock; |
| 749 | int64_t expire_time; |
| 750 | QEMUTimerCB *cb; |
| 751 | void *opaque; |
| 752 | struct QEMUTimer *next; |
| 753 | }; |
| 754 | |
| 755 | QEMUClock *rt_clock; |
| 756 | QEMUClock *vm_clock; |
| 757 | |
| 758 | static QEMUTimer *active_timers[2]; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 759 | #ifdef _WIN32 |
| 760 | static MMRESULT timerID; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 761 | static HANDLE host_alarm = NULL; |
| 762 | static unsigned int period = 1; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 763 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 764 | /* frequency of the times() clock tick */ |
| 765 | static int timer_freq; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 766 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 767 | |
| 768 | QEMUClock *qemu_new_clock(int type) |
| 769 | { |
| 770 | QEMUClock *clock; |
| 771 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 772 | if (!clock) |
| 773 | return NULL; |
| 774 | clock->type = type; |
| 775 | return clock; |
| 776 | } |
| 777 | |
| 778 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 779 | { |
| 780 | QEMUTimer *ts; |
| 781 | |
| 782 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 783 | ts->clock = clock; |
| 784 | ts->cb = cb; |
| 785 | ts->opaque = opaque; |
| 786 | return ts; |
| 787 | } |
| 788 | |
| 789 | void qemu_free_timer(QEMUTimer *ts) |
| 790 | { |
| 791 | qemu_free(ts); |
| 792 | } |
| 793 | |
| 794 | /* stop a timer, but do not dealloc it */ |
| 795 | void qemu_del_timer(QEMUTimer *ts) |
| 796 | { |
| 797 | QEMUTimer **pt, *t; |
| 798 | |
| 799 | /* NOTE: this code must be signal safe because |
| 800 | qemu_timer_expired() can be called from a signal. */ |
| 801 | pt = &active_timers[ts->clock->type]; |
| 802 | for(;;) { |
| 803 | t = *pt; |
| 804 | if (!t) |
| 805 | break; |
| 806 | if (t == ts) { |
| 807 | *pt = t->next; |
| 808 | break; |
| 809 | } |
| 810 | pt = &t->next; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* modify the current timer so that it will be fired when current_time |
| 815 | >= expire_time. The corresponding callback will be called. */ |
| 816 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 817 | { |
| 818 | QEMUTimer **pt, *t; |
| 819 | |
| 820 | qemu_del_timer(ts); |
| 821 | |
| 822 | /* add the timer in the sorted list */ |
| 823 | /* NOTE: this code must be signal safe because |
| 824 | qemu_timer_expired() can be called from a signal. */ |
| 825 | pt = &active_timers[ts->clock->type]; |
| 826 | for(;;) { |
| 827 | t = *pt; |
| 828 | if (!t) |
| 829 | break; |
| 830 | if (t->expire_time > expire_time) |
| 831 | break; |
| 832 | pt = &t->next; |
| 833 | } |
| 834 | ts->expire_time = expire_time; |
| 835 | ts->next = *pt; |
| 836 | *pt = ts; |
| 837 | } |
| 838 | |
| 839 | int qemu_timer_pending(QEMUTimer *ts) |
| 840 | { |
| 841 | QEMUTimer *t; |
| 842 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 843 | if (t == ts) |
| 844 | return 1; |
| 845 | } |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 850 | { |
| 851 | if (!timer_head) |
| 852 | return 0; |
| 853 | return (timer_head->expire_time <= current_time); |
| 854 | } |
| 855 | |
| 856 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 857 | { |
| 858 | QEMUTimer *ts; |
| 859 | |
| 860 | for(;;) { |
| 861 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 862 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 863 | break; |
| 864 | /* remove timer from the list before calling the callback */ |
| 865 | *ptimer_head = ts->next; |
| 866 | ts->next = NULL; |
| 867 | |
| 868 | /* run the callback (the timer list can be modified) */ |
| 869 | ts->cb(ts->opaque); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | int64_t qemu_get_clock(QEMUClock *clock) |
| 874 | { |
| 875 | switch(clock->type) { |
| 876 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 877 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 878 | default: |
| 879 | case QEMU_TIMER_VIRTUAL: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 880 | return cpu_get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 884 | static void init_timers(void) |
| 885 | { |
| 886 | init_get_clock(); |
| 887 | ticks_per_sec = QEMU_TIMER_BASE; |
| 888 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 889 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 890 | } |
| 891 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 892 | /* save a timer */ |
| 893 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 894 | { |
| 895 | uint64_t expire_time; |
| 896 | |
| 897 | if (qemu_timer_pending(ts)) { |
| 898 | expire_time = ts->expire_time; |
| 899 | } else { |
| 900 | expire_time = -1; |
| 901 | } |
| 902 | qemu_put_be64(f, expire_time); |
| 903 | } |
| 904 | |
| 905 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 906 | { |
| 907 | uint64_t expire_time; |
| 908 | |
| 909 | expire_time = qemu_get_be64(f); |
| 910 | if (expire_time != -1) { |
| 911 | qemu_mod_timer(ts, expire_time); |
| 912 | } else { |
| 913 | qemu_del_timer(ts); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | static void timer_save(QEMUFile *f, void *opaque) |
| 918 | { |
| 919 | if (cpu_ticks_enabled) { |
| 920 | hw_error("cannot save state if virtual timers are running"); |
| 921 | } |
| 922 | qemu_put_be64s(f, &cpu_ticks_offset); |
| 923 | qemu_put_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 924 | qemu_put_be64s(f, &cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 928 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 929 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 930 | return -EINVAL; |
| 931 | if (cpu_ticks_enabled) { |
| 932 | return -EINVAL; |
| 933 | } |
| 934 | qemu_get_be64s(f, &cpu_ticks_offset); |
| 935 | qemu_get_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 936 | if (version_id == 2) { |
| 937 | qemu_get_be64s(f, &cpu_clock_offset); |
| 938 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 942 | #ifdef _WIN32 |
| 943 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 944 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 945 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 946 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 947 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 948 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 949 | #if 0 |
| 950 | #define DISP_FREQ 1000 |
| 951 | { |
| 952 | static int64_t delta_min = INT64_MAX; |
| 953 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 954 | static int count; |
| 955 | ti = qemu_get_clock(vm_clock); |
| 956 | if (last_clock != 0) { |
| 957 | delta = ti - last_clock; |
| 958 | if (delta < delta_min) |
| 959 | delta_min = delta; |
| 960 | if (delta > delta_max) |
| 961 | delta_max = delta; |
| 962 | delta_cum += delta; |
| 963 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 964 | 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] | 965 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 966 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 967 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 968 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 969 | count = 0; |
| 970 | delta_min = INT64_MAX; |
| 971 | delta_max = 0; |
| 972 | delta_cum = 0; |
| 973 | } |
| 974 | } |
| 975 | last_clock = ti; |
| 976 | } |
| 977 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 978 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 979 | qemu_get_clock(vm_clock)) || |
| 980 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 981 | qemu_get_clock(rt_clock))) { |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 982 | #ifdef _WIN32 |
| 983 | SetEvent(host_alarm); |
| 984 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 985 | CPUState *env = cpu_single_env; |
| 986 | if (env) { |
| 987 | /* stop the currently executing cpu because a timer occured */ |
| 988 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 989 | #ifdef USE_KQEMU |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 990 | if (env->kqemu_enabled) { |
| 991 | kqemu_cpu_interrupt(env); |
| 992 | } |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 993 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 994 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 998 | #ifndef _WIN32 |
| 999 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1000 | #if defined(__linux__) |
| 1001 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1002 | #define RTC_FREQ 1024 |
| 1003 | |
| 1004 | static int rtc_fd; |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1005 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1006 | static int start_rtc_timer(void) |
| 1007 | { |
| 1008 | rtc_fd = open("/dev/rtc", O_RDONLY); |
| 1009 | if (rtc_fd < 0) |
| 1010 | return -1; |
| 1011 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
| 1012 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1013 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1014 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1015 | goto fail; |
| 1016 | } |
| 1017 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1018 | fail: |
| 1019 | close(rtc_fd); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1026 | #else |
| 1027 | |
| 1028 | static int start_rtc_timer(void) |
| 1029 | { |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | #endif /* !defined(__linux__) */ |
| 1034 | |
| 1035 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1036 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1037 | static void init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1038 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1039 | #ifdef _WIN32 |
| 1040 | { |
| 1041 | int count=0; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1042 | TIMECAPS tc; |
| 1043 | |
| 1044 | ZeroMemory(&tc, sizeof(TIMECAPS)); |
| 1045 | timeGetDevCaps(&tc, sizeof(TIMECAPS)); |
| 1046 | if (period < tc.wPeriodMin) |
| 1047 | period = tc.wPeriodMin; |
| 1048 | timeBeginPeriod(period); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1049 | timerID = timeSetEvent(1, // interval (ms) |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1050 | period, // resolution |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1051 | host_alarm_handler, // function |
| 1052 | (DWORD)&count, // user parameter |
| 1053 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1054 | if( !timerID ) { |
| 1055 | perror("failed timer alarm"); |
| 1056 | exit(1); |
| 1057 | } |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1058 | host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1059 | if (!host_alarm) { |
| 1060 | perror("failed CreateEvent"); |
| 1061 | exit(1); |
| 1062 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 1063 | qemu_add_wait_object(host_alarm, NULL, NULL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1064 | } |
| 1065 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
| 1066 | #else |
| 1067 | { |
| 1068 | struct sigaction act; |
| 1069 | struct itimerval itv; |
| 1070 | |
| 1071 | /* get times() syscall frequency */ |
| 1072 | timer_freq = sysconf(_SC_CLK_TCK); |
| 1073 | |
| 1074 | /* timer signal */ |
| 1075 | sigfillset(&act.sa_mask); |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 1076 | act.sa_flags = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1077 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1078 | act.sa_flags |= SA_ONSTACK; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1079 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1080 | act.sa_handler = host_alarm_handler; |
| 1081 | sigaction(SIGALRM, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1082 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1083 | itv.it_interval.tv_sec = 0; |
bellard | d79284e | 2005-01-26 21:56:26 +0000 | [diff] [blame] | 1084 | 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] | 1085 | itv.it_value.tv_sec = 0; |
| 1086 | itv.it_value.tv_usec = 10 * 1000; |
| 1087 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1088 | /* we probe the tick duration of the kernel to inform the user if |
| 1089 | the emulated kernel requested a too high timer frequency */ |
| 1090 | getitimer(ITIMER_REAL, &itv); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1091 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1092 | #if defined(__linux__) |
bellard | 29e3055 | 2006-05-01 13:28:36 +0000 | [diff] [blame] | 1093 | /* XXX: force /dev/rtc usage because even 2.6 kernels may not |
| 1094 | have timers with 1 ms resolution. The correct solution will |
| 1095 | be to use the POSIX real time timers available in recent |
| 1096 | 2.6 kernels */ |
| 1097 | if (itv.it_interval.tv_usec > 1000 || 1) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1098 | /* try to use /dev/rtc to have a faster timer */ |
| 1099 | if (start_rtc_timer() < 0) |
| 1100 | goto use_itimer; |
| 1101 | /* disable itimer */ |
| 1102 | itv.it_interval.tv_sec = 0; |
| 1103 | itv.it_interval.tv_usec = 0; |
| 1104 | itv.it_value.tv_sec = 0; |
| 1105 | itv.it_value.tv_usec = 0; |
| 1106 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1107 | |
| 1108 | /* use the RTC */ |
bellard | a1968d7 | 2004-06-25 17:06:27 +0000 | [diff] [blame] | 1109 | sigaction(SIGIO, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1110 | fcntl(rtc_fd, F_SETFL, O_ASYNC); |
| 1111 | fcntl(rtc_fd, F_SETOWN, getpid()); |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1112 | } else |
| 1113 | #endif /* defined(__linux__) */ |
| 1114 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1115 | use_itimer: |
| 1116 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
| 1117 | PIT_FREQ) / 1000000; |
| 1118 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1119 | } |
| 1120 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1123 | void quit_timers(void) |
| 1124 | { |
| 1125 | #ifdef _WIN32 |
| 1126 | timeKillEvent(timerID); |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1127 | timeEndPeriod(period); |
| 1128 | if (host_alarm) { |
| 1129 | CloseHandle(host_alarm); |
| 1130 | host_alarm = NULL; |
| 1131 | } |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1132 | #endif |
| 1133 | } |
| 1134 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1135 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1136 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1137 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1138 | static void qemu_chr_event(CharDriverState *s, int event) |
| 1139 | { |
| 1140 | if (!s->chr_event) |
| 1141 | return; |
| 1142 | s->chr_event(s->handler_opaque, event); |
| 1143 | } |
| 1144 | |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1145 | static void qemu_chr_reset_bh(void *opaque) |
| 1146 | { |
| 1147 | CharDriverState *s = opaque; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1148 | qemu_chr_event(s, CHR_EVENT_RESET); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1149 | qemu_bh_delete(s->bh); |
| 1150 | s->bh = NULL; |
| 1151 | } |
| 1152 | |
| 1153 | void qemu_chr_reset(CharDriverState *s) |
| 1154 | { |
| 1155 | if (s->bh == NULL) { |
| 1156 | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
| 1157 | qemu_bh_schedule(s->bh); |
| 1158 | } |
| 1159 | } |
| 1160 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1161 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1162 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1163 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1166 | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1167 | { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1168 | if (!s->chr_ioctl) |
| 1169 | return -ENOTSUP; |
| 1170 | return s->chr_ioctl(s, cmd, arg); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1173 | int qemu_chr_can_read(CharDriverState *s) |
| 1174 | { |
| 1175 | if (!s->chr_can_read) |
| 1176 | return 0; |
| 1177 | return s->chr_can_read(s->handler_opaque); |
| 1178 | } |
| 1179 | |
| 1180 | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
| 1181 | { |
| 1182 | s->chr_read(s->handler_opaque, buf, len); |
| 1183 | } |
| 1184 | |
| 1185 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1186 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1187 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1188 | char buf[4096]; |
| 1189 | va_list ap; |
| 1190 | va_start(ap, fmt); |
| 1191 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1192 | qemu_chr_write(s, buf, strlen(buf)); |
| 1193 | va_end(ap); |
| 1194 | } |
| 1195 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1196 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1197 | { |
| 1198 | if (s->chr_send_event) |
| 1199 | s->chr_send_event(s, event); |
| 1200 | } |
| 1201 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1202 | void qemu_chr_add_handlers(CharDriverState *s, |
| 1203 | IOCanRWHandler *fd_can_read, |
| 1204 | IOReadHandler *fd_read, |
| 1205 | IOEventHandler *fd_event, |
| 1206 | void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1207 | { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1208 | s->chr_can_read = fd_can_read; |
| 1209 | s->chr_read = fd_read; |
| 1210 | s->chr_event = fd_event; |
| 1211 | s->handler_opaque = opaque; |
| 1212 | if (s->chr_update_read_handler) |
| 1213 | s->chr_update_read_handler(s); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1216 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1217 | { |
| 1218 | return len; |
| 1219 | } |
| 1220 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1221 | static CharDriverState *qemu_chr_open_null(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1222 | { |
| 1223 | CharDriverState *chr; |
| 1224 | |
| 1225 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1226 | if (!chr) |
| 1227 | return NULL; |
| 1228 | chr->chr_write = null_chr_write; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1229 | return chr; |
| 1230 | } |
| 1231 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1232 | /* MUX driver for serial I/O splitting */ |
| 1233 | static int term_timestamps; |
| 1234 | static int64_t term_timestamps_start; |
| 1235 | #define MAX_MUX 2 |
| 1236 | typedef struct { |
| 1237 | IOCanRWHandler *chr_can_read[MAX_MUX]; |
| 1238 | IOReadHandler *chr_read[MAX_MUX]; |
| 1239 | IOEventHandler *chr_event[MAX_MUX]; |
| 1240 | void *ext_opaque[MAX_MUX]; |
| 1241 | CharDriverState *drv; |
| 1242 | int mux_cnt; |
| 1243 | int term_got_escape; |
| 1244 | int max_size; |
| 1245 | } MuxDriver; |
| 1246 | |
| 1247 | |
| 1248 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1249 | { |
| 1250 | MuxDriver *d = chr->opaque; |
| 1251 | int ret; |
| 1252 | if (!term_timestamps) { |
| 1253 | ret = d->drv->chr_write(d->drv, buf, len); |
| 1254 | } else { |
| 1255 | int i; |
| 1256 | |
| 1257 | ret = 0; |
| 1258 | for(i = 0; i < len; i++) { |
| 1259 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 1260 | if (buf[i] == '\n') { |
| 1261 | char buf1[64]; |
| 1262 | int64_t ti; |
| 1263 | int secs; |
| 1264 | |
| 1265 | ti = get_clock(); |
| 1266 | if (term_timestamps_start == -1) |
| 1267 | term_timestamps_start = ti; |
| 1268 | ti -= term_timestamps_start; |
| 1269 | secs = ti / 1000000000; |
| 1270 | snprintf(buf1, sizeof(buf1), |
| 1271 | "[%02d:%02d:%02d.%03d] ", |
| 1272 | secs / 3600, |
| 1273 | (secs / 60) % 60, |
| 1274 | secs % 60, |
| 1275 | (int)((ti / 1000000) % 1000)); |
| 1276 | d->drv->chr_write(d->drv, buf1, strlen(buf1)); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | static char *mux_help[] = { |
| 1284 | "% h print this help\n\r", |
| 1285 | "% x exit emulator\n\r", |
| 1286 | "% s save disk data back to file (if -snapshot)\n\r", |
| 1287 | "% t toggle console timestamps\n\r" |
| 1288 | "% b send break (magic sysrq)\n\r", |
| 1289 | "% c switch between console and monitor\n\r", |
| 1290 | "% % sends %\n\r", |
| 1291 | NULL |
| 1292 | }; |
| 1293 | |
| 1294 | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 1295 | static void mux_print_help(CharDriverState *chr) |
| 1296 | { |
| 1297 | int i, j; |
| 1298 | char ebuf[15] = "Escape-Char"; |
| 1299 | char cbuf[50] = "\n\r"; |
| 1300 | |
| 1301 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 1302 | sprintf(cbuf,"\n\r"); |
| 1303 | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
| 1304 | } else { |
| 1305 | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char); |
| 1306 | } |
| 1307 | chr->chr_write(chr, cbuf, strlen(cbuf)); |
| 1308 | for (i = 0; mux_help[i] != NULL; i++) { |
| 1309 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 1310 | if (mux_help[i][j] == '%') |
| 1311 | chr->chr_write(chr, ebuf, strlen(ebuf)); |
| 1312 | else |
| 1313 | chr->chr_write(chr, &mux_help[i][j], 1); |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 1319 | { |
| 1320 | if (d->term_got_escape) { |
| 1321 | d->term_got_escape = 0; |
| 1322 | if (ch == term_escape_char) |
| 1323 | goto send_char; |
| 1324 | switch(ch) { |
| 1325 | case '?': |
| 1326 | case 'h': |
| 1327 | mux_print_help(chr); |
| 1328 | break; |
| 1329 | case 'x': |
| 1330 | { |
| 1331 | char *term = "QEMU: Terminated\n\r"; |
| 1332 | chr->chr_write(chr,term,strlen(term)); |
| 1333 | exit(0); |
| 1334 | break; |
| 1335 | } |
| 1336 | case 's': |
| 1337 | { |
| 1338 | int i; |
| 1339 | for (i = 0; i < MAX_DISKS; i++) { |
| 1340 | if (bs_table[i]) |
| 1341 | bdrv_commit(bs_table[i]); |
| 1342 | } |
| 1343 | } |
| 1344 | break; |
| 1345 | case 'b': |
| 1346 | if (chr->chr_event) |
| 1347 | chr->chr_event(chr->opaque, CHR_EVENT_BREAK); |
| 1348 | break; |
| 1349 | case 'c': |
| 1350 | /* Switch to the next registered device */ |
| 1351 | chr->focus++; |
| 1352 | if (chr->focus >= d->mux_cnt) |
| 1353 | chr->focus = 0; |
| 1354 | break; |
| 1355 | case 't': |
| 1356 | term_timestamps = !term_timestamps; |
| 1357 | term_timestamps_start = -1; |
| 1358 | break; |
| 1359 | } |
| 1360 | } else if (ch == term_escape_char) { |
| 1361 | d->term_got_escape = 1; |
| 1362 | } else { |
| 1363 | send_char: |
| 1364 | return 1; |
| 1365 | } |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | static int mux_chr_can_read(void *opaque) |
| 1370 | { |
| 1371 | CharDriverState *chr = opaque; |
| 1372 | MuxDriver *d = chr->opaque; |
| 1373 | if (d->chr_can_read[chr->focus]) |
| 1374 | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 1379 | { |
| 1380 | CharDriverState *chr = opaque; |
| 1381 | MuxDriver *d = chr->opaque; |
| 1382 | int i; |
| 1383 | for(i = 0; i < size; i++) |
| 1384 | if (mux_proc_byte(chr, d, buf[i])) |
| 1385 | d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1); |
| 1386 | } |
| 1387 | |
| 1388 | static void mux_chr_event(void *opaque, int event) |
| 1389 | { |
| 1390 | CharDriverState *chr = opaque; |
| 1391 | MuxDriver *d = chr->opaque; |
| 1392 | int i; |
| 1393 | |
| 1394 | /* Send the event to all registered listeners */ |
| 1395 | for (i = 0; i < d->mux_cnt; i++) |
| 1396 | if (d->chr_event[i]) |
| 1397 | d->chr_event[i](d->ext_opaque[i], event); |
| 1398 | } |
| 1399 | |
| 1400 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 1401 | { |
| 1402 | MuxDriver *d = chr->opaque; |
| 1403 | |
| 1404 | if (d->mux_cnt >= MAX_MUX) { |
| 1405 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 1406 | return; |
| 1407 | } |
| 1408 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 1409 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 1410 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 1411 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 1412 | /* Fix up the real driver with mux routines */ |
| 1413 | if (d->mux_cnt == 0) { |
| 1414 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 1415 | mux_chr_event, chr); |
| 1416 | } |
| 1417 | chr->focus = d->mux_cnt; |
| 1418 | d->mux_cnt++; |
| 1419 | } |
| 1420 | |
| 1421 | CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
| 1422 | { |
| 1423 | CharDriverState *chr; |
| 1424 | MuxDriver *d; |
| 1425 | |
| 1426 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1427 | if (!chr) |
| 1428 | return NULL; |
| 1429 | d = qemu_mallocz(sizeof(MuxDriver)); |
| 1430 | if (!d) { |
| 1431 | free(chr); |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | |
| 1435 | chr->opaque = d; |
| 1436 | d->drv = drv; |
| 1437 | chr->focus = -1; |
| 1438 | chr->chr_write = mux_chr_write; |
| 1439 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 1440 | return chr; |
| 1441 | } |
| 1442 | |
| 1443 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1444 | #ifdef _WIN32 |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1445 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1446 | static void socket_cleanup(void) |
| 1447 | { |
| 1448 | WSACleanup(); |
| 1449 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1450 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1451 | static int socket_init(void) |
| 1452 | { |
| 1453 | WSADATA Data; |
| 1454 | int ret, err; |
| 1455 | |
| 1456 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1457 | if (ret != 0) { |
| 1458 | err = WSAGetLastError(); |
| 1459 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | atexit(socket_cleanup); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | static int send_all(int fd, const uint8_t *buf, int len1) |
| 1467 | { |
| 1468 | int ret, len; |
| 1469 | |
| 1470 | len = len1; |
| 1471 | while (len > 0) { |
| 1472 | ret = send(fd, buf, len, 0); |
| 1473 | if (ret < 0) { |
| 1474 | int errno; |
| 1475 | errno = WSAGetLastError(); |
| 1476 | if (errno != WSAEWOULDBLOCK) { |
| 1477 | return -1; |
| 1478 | } |
| 1479 | } else if (ret == 0) { |
| 1480 | break; |
| 1481 | } else { |
| 1482 | buf += ret; |
| 1483 | len -= ret; |
| 1484 | } |
| 1485 | } |
| 1486 | return len1 - len; |
| 1487 | } |
| 1488 | |
| 1489 | void socket_set_nonblock(int fd) |
| 1490 | { |
| 1491 | unsigned long opt = 1; |
| 1492 | ioctlsocket(fd, FIONBIO, &opt); |
| 1493 | } |
| 1494 | |
| 1495 | #else |
| 1496 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1497 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 1498 | { |
| 1499 | int ret, len; |
| 1500 | |
| 1501 | len = len1; |
| 1502 | while (len > 0) { |
| 1503 | ret = write(fd, buf, len); |
| 1504 | if (ret < 0) { |
| 1505 | if (errno != EINTR && errno != EAGAIN) |
| 1506 | return -1; |
| 1507 | } else if (ret == 0) { |
| 1508 | break; |
| 1509 | } else { |
| 1510 | buf += ret; |
| 1511 | len -= ret; |
| 1512 | } |
| 1513 | } |
| 1514 | return len1 - len; |
| 1515 | } |
| 1516 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1517 | static inline int send_all(int fd, const uint8_t *buf, int len1) |
| 1518 | { |
| 1519 | return unix_write(fd, buf, len1); |
| 1520 | } |
| 1521 | |
| 1522 | void socket_set_nonblock(int fd) |
| 1523 | { |
| 1524 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1525 | } |
| 1526 | #endif /* !_WIN32 */ |
| 1527 | |
| 1528 | #ifndef _WIN32 |
| 1529 | |
| 1530 | typedef struct { |
| 1531 | int fd_in, fd_out; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1532 | int max_size; |
| 1533 | } FDCharDriver; |
| 1534 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1535 | #define STDIO_MAX_CLIENTS 1 |
| 1536 | static int stdio_nb_clients = 0; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1537 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1538 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1539 | { |
| 1540 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1541 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1544 | static int fd_chr_read_poll(void *opaque) |
| 1545 | { |
| 1546 | CharDriverState *chr = opaque; |
| 1547 | FDCharDriver *s = chr->opaque; |
| 1548 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1549 | s->max_size = qemu_chr_can_read(chr); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1550 | return s->max_size; |
| 1551 | } |
| 1552 | |
| 1553 | static void fd_chr_read(void *opaque) |
| 1554 | { |
| 1555 | CharDriverState *chr = opaque; |
| 1556 | FDCharDriver *s = chr->opaque; |
| 1557 | int size, len; |
| 1558 | uint8_t buf[1024]; |
| 1559 | |
| 1560 | len = sizeof(buf); |
| 1561 | if (len > s->max_size) |
| 1562 | len = s->max_size; |
| 1563 | if (len == 0) |
| 1564 | return; |
| 1565 | size = read(s->fd_in, buf, len); |
pbrook | 188157f | 2006-11-01 01:44:16 +0000 | [diff] [blame] | 1566 | if (size == 0) { |
| 1567 | /* FD has been closed. Remove it from the active list. */ |
| 1568 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 1569 | return; |
| 1570 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1571 | if (size > 0) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1572 | qemu_chr_read(chr, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1576 | static void fd_chr_update_read_handler(CharDriverState *chr) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1577 | { |
| 1578 | FDCharDriver *s = chr->opaque; |
| 1579 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1580 | if (s->fd_in >= 0) { |
| 1581 | if (nographic && s->fd_in == 0) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1582 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1583 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
| 1584 | fd_chr_read, NULL, chr); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1585 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1589 | /* open a character device to a unix fd */ |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1590 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1591 | { |
| 1592 | CharDriverState *chr; |
| 1593 | FDCharDriver *s; |
| 1594 | |
| 1595 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1596 | if (!chr) |
| 1597 | return NULL; |
| 1598 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 1599 | if (!s) { |
| 1600 | free(chr); |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | s->fd_in = fd_in; |
| 1604 | s->fd_out = fd_out; |
| 1605 | chr->opaque = s; |
| 1606 | chr->chr_write = fd_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1607 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1608 | |
| 1609 | qemu_chr_reset(chr); |
| 1610 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1611 | return chr; |
| 1612 | } |
| 1613 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1614 | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1615 | { |
| 1616 | int fd_out; |
| 1617 | |
pbrook | 89ba1a7 | 2006-03-28 20:20:38 +0000 | [diff] [blame] | 1618 | 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] | 1619 | if (fd_out < 0) |
| 1620 | return NULL; |
| 1621 | return qemu_chr_open_fd(-1, fd_out); |
| 1622 | } |
| 1623 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1624 | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1625 | { |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1626 | int fd_in, fd_out; |
| 1627 | char filename_in[256], filename_out[256]; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1628 | |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1629 | snprintf(filename_in, 256, "%s.in", filename); |
| 1630 | snprintf(filename_out, 256, "%s.out", filename); |
| 1631 | fd_in = open(filename_in, O_RDWR | O_BINARY); |
| 1632 | fd_out = open(filename_out, O_RDWR | O_BINARY); |
| 1633 | if (fd_in < 0 || fd_out < 0) { |
| 1634 | if (fd_in >= 0) |
| 1635 | close(fd_in); |
| 1636 | if (fd_out >= 0) |
| 1637 | close(fd_out); |
| 1638 | fd_in = fd_out = open(filename, O_RDWR | O_BINARY); |
| 1639 | if (fd_in < 0) |
| 1640 | return NULL; |
| 1641 | } |
| 1642 | return qemu_chr_open_fd(fd_in, fd_out); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1646 | /* for STDIO, we handle the case where several clients use it |
| 1647 | (nographic mode) */ |
| 1648 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1649 | #define TERM_FIFO_MAX_SIZE 1 |
| 1650 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1651 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1652 | static int term_fifo_size; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1653 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1654 | static int stdio_read_poll(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1655 | { |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1656 | CharDriverState *chr = opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1657 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1658 | /* try to flush the queue if needed */ |
| 1659 | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
| 1660 | qemu_chr_read(chr, term_fifo, 1); |
| 1661 | term_fifo_size = 0; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1662 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1663 | /* see if we can absorb more chars */ |
| 1664 | if (term_fifo_size == 0) |
| 1665 | return 1; |
| 1666 | else |
| 1667 | return 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1670 | static void stdio_read(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1671 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1672 | int size; |
| 1673 | uint8_t buf[1]; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1674 | CharDriverState *chr = opaque; |
| 1675 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1676 | size = read(0, buf, 1); |
pbrook | 519945d | 2006-09-10 14:39:54 +0000 | [diff] [blame] | 1677 | if (size == 0) { |
| 1678 | /* stdin has been closed. Remove it from the active list. */ |
| 1679 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 1680 | return; |
| 1681 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1682 | if (size > 0) { |
| 1683 | if (qemu_chr_can_read(chr) > 0) { |
| 1684 | qemu_chr_read(chr, buf, 1); |
| 1685 | } else if (term_fifo_size == 0) { |
| 1686 | term_fifo[term_fifo_size++] = buf[0]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1687 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1691 | /* init terminal so that we can grab keys */ |
| 1692 | static struct termios oldtty; |
| 1693 | static int old_fd0_flags; |
| 1694 | |
| 1695 | static void term_exit(void) |
| 1696 | { |
| 1697 | tcsetattr (0, TCSANOW, &oldtty); |
| 1698 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1699 | } |
| 1700 | |
| 1701 | static void term_init(void) |
| 1702 | { |
| 1703 | struct termios tty; |
| 1704 | |
| 1705 | tcgetattr (0, &tty); |
| 1706 | oldtty = tty; |
| 1707 | old_fd0_flags = fcntl(0, F_GETFL); |
| 1708 | |
| 1709 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1710 | |INLCR|IGNCR|ICRNL|IXON); |
| 1711 | tty.c_oflag |= OPOST; |
| 1712 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1713 | /* if graphical mode, we allow Ctrl-C handling */ |
| 1714 | if (nographic) |
| 1715 | tty.c_lflag &= ~ISIG; |
| 1716 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1717 | tty.c_cflag |= CS8; |
| 1718 | tty.c_cc[VMIN] = 1; |
| 1719 | tty.c_cc[VTIME] = 0; |
| 1720 | |
| 1721 | tcsetattr (0, TCSANOW, &tty); |
| 1722 | |
| 1723 | atexit(term_exit); |
| 1724 | |
| 1725 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 1726 | } |
| 1727 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1728 | static CharDriverState *qemu_chr_open_stdio(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1729 | { |
| 1730 | CharDriverState *chr; |
| 1731 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1732 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 1733 | return NULL; |
| 1734 | chr = qemu_chr_open_fd(0, 1); |
| 1735 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
| 1736 | stdio_nb_clients++; |
| 1737 | term_init(); |
| 1738 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1739 | return chr; |
| 1740 | } |
| 1741 | |
| 1742 | #if defined(__linux__) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1743 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1744 | { |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1745 | struct termios tty; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1746 | char slave_name[1024]; |
| 1747 | int master_fd, slave_fd; |
| 1748 | |
| 1749 | /* Not satisfying */ |
| 1750 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 1751 | return NULL; |
| 1752 | } |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1753 | |
| 1754 | /* Disabling local echo and line-buffered output */ |
| 1755 | tcgetattr (master_fd, &tty); |
| 1756 | tty.c_lflag &= ~(ECHO|ICANON|ISIG); |
| 1757 | tty.c_cc[VMIN] = 1; |
| 1758 | tty.c_cc[VTIME] = 0; |
| 1759 | tcsetattr (master_fd, TCSAFLUSH, &tty); |
| 1760 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1761 | fprintf(stderr, "char device redirected to %s\n", slave_name); |
| 1762 | return qemu_chr_open_fd(master_fd, master_fd); |
| 1763 | } |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1764 | |
| 1765 | static void tty_serial_init(int fd, int speed, |
| 1766 | int parity, int data_bits, int stop_bits) |
| 1767 | { |
| 1768 | struct termios tty; |
| 1769 | speed_t spd; |
| 1770 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1771 | #if 0 |
| 1772 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1773 | speed, parity, data_bits, stop_bits); |
| 1774 | #endif |
| 1775 | tcgetattr (fd, &tty); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1776 | |
| 1777 | switch(speed) { |
| 1778 | case 50: |
| 1779 | spd = B50; |
| 1780 | break; |
| 1781 | case 75: |
| 1782 | spd = B75; |
| 1783 | break; |
| 1784 | case 300: |
| 1785 | spd = B300; |
| 1786 | break; |
| 1787 | case 600: |
| 1788 | spd = B600; |
| 1789 | break; |
| 1790 | case 1200: |
| 1791 | spd = B1200; |
| 1792 | break; |
| 1793 | case 2400: |
| 1794 | spd = B2400; |
| 1795 | break; |
| 1796 | case 4800: |
| 1797 | spd = B4800; |
| 1798 | break; |
| 1799 | case 9600: |
| 1800 | spd = B9600; |
| 1801 | break; |
| 1802 | case 19200: |
| 1803 | spd = B19200; |
| 1804 | break; |
| 1805 | case 38400: |
| 1806 | spd = B38400; |
| 1807 | break; |
| 1808 | case 57600: |
| 1809 | spd = B57600; |
| 1810 | break; |
| 1811 | default: |
| 1812 | case 115200: |
| 1813 | spd = B115200; |
| 1814 | break; |
| 1815 | } |
| 1816 | |
| 1817 | cfsetispeed(&tty, spd); |
| 1818 | cfsetospeed(&tty, spd); |
| 1819 | |
| 1820 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1821 | |INLCR|IGNCR|ICRNL|IXON); |
| 1822 | tty.c_oflag |= OPOST; |
| 1823 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1824 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1825 | switch(data_bits) { |
| 1826 | default: |
| 1827 | case 8: |
| 1828 | tty.c_cflag |= CS8; |
| 1829 | break; |
| 1830 | case 7: |
| 1831 | tty.c_cflag |= CS7; |
| 1832 | break; |
| 1833 | case 6: |
| 1834 | tty.c_cflag |= CS6; |
| 1835 | break; |
| 1836 | case 5: |
| 1837 | tty.c_cflag |= CS5; |
| 1838 | break; |
| 1839 | } |
| 1840 | switch(parity) { |
| 1841 | default: |
| 1842 | case 'N': |
| 1843 | break; |
| 1844 | case 'E': |
| 1845 | tty.c_cflag |= PARENB; |
| 1846 | break; |
| 1847 | case 'O': |
| 1848 | tty.c_cflag |= PARENB | PARODD; |
| 1849 | break; |
| 1850 | } |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1851 | if (stop_bits == 2) |
| 1852 | tty.c_cflag |= CSTOPB; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1853 | |
| 1854 | tcsetattr (fd, TCSANOW, &tty); |
| 1855 | } |
| 1856 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1857 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1858 | { |
| 1859 | FDCharDriver *s = chr->opaque; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1860 | |
| 1861 | switch(cmd) { |
| 1862 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1863 | { |
| 1864 | QEMUSerialSetParams *ssp = arg; |
| 1865 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
| 1866 | ssp->data_bits, ssp->stop_bits); |
| 1867 | } |
| 1868 | break; |
| 1869 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1870 | { |
| 1871 | int enable = *(int *)arg; |
| 1872 | if (enable) |
| 1873 | tcsendbreak(s->fd_in, 1); |
| 1874 | } |
| 1875 | break; |
| 1876 | default: |
| 1877 | return -ENOTSUP; |
| 1878 | } |
| 1879 | return 0; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1882 | static CharDriverState *qemu_chr_open_tty(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1883 | { |
| 1884 | CharDriverState *chr; |
| 1885 | int fd; |
| 1886 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1887 | fd = open(filename, O_RDWR | O_NONBLOCK); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1888 | if (fd < 0) |
| 1889 | return NULL; |
| 1890 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1891 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1892 | chr = qemu_chr_open_fd(fd, fd); |
| 1893 | if (!chr) |
| 1894 | return NULL; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1895 | chr->chr_ioctl = tty_serial_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1896 | qemu_chr_reset(chr); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1897 | return chr; |
| 1898 | } |
| 1899 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1900 | typedef struct { |
| 1901 | int fd; |
| 1902 | int mode; |
| 1903 | } ParallelCharDriver; |
| 1904 | |
| 1905 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1906 | { |
| 1907 | if (s->mode != mode) { |
| 1908 | int m = mode; |
| 1909 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1910 | return 0; |
| 1911 | s->mode = mode; |
| 1912 | } |
| 1913 | return 1; |
| 1914 | } |
| 1915 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1916 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1917 | { |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1918 | ParallelCharDriver *drv = chr->opaque; |
| 1919 | int fd = drv->fd; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1920 | uint8_t b; |
| 1921 | |
| 1922 | switch(cmd) { |
| 1923 | case CHR_IOCTL_PP_READ_DATA: |
| 1924 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1925 | return -ENOTSUP; |
| 1926 | *(uint8_t *)arg = b; |
| 1927 | break; |
| 1928 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1929 | b = *(uint8_t *)arg; |
| 1930 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1931 | return -ENOTSUP; |
| 1932 | break; |
| 1933 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1934 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1935 | return -ENOTSUP; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1936 | /* Linux gives only the lowest bits, and no way to know data |
| 1937 | direction! For better compatibility set the fixed upper |
| 1938 | bits. */ |
| 1939 | *(uint8_t *)arg = b | 0xc0; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1942 | b = *(uint8_t *)arg; |
| 1943 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1944 | return -ENOTSUP; |
| 1945 | break; |
| 1946 | case CHR_IOCTL_PP_READ_STATUS: |
| 1947 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1948 | return -ENOTSUP; |
| 1949 | *(uint8_t *)arg = b; |
| 1950 | break; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1951 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1952 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1953 | struct ParallelIOArg *parg = arg; |
| 1954 | int n = read(fd, parg->buffer, parg->count); |
| 1955 | if (n != parg->count) { |
| 1956 | return -EIO; |
| 1957 | } |
| 1958 | } |
| 1959 | break; |
| 1960 | case CHR_IOCTL_PP_EPP_READ: |
| 1961 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1962 | struct ParallelIOArg *parg = arg; |
| 1963 | int n = read(fd, parg->buffer, parg->count); |
| 1964 | if (n != parg->count) { |
| 1965 | return -EIO; |
| 1966 | } |
| 1967 | } |
| 1968 | break; |
| 1969 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1970 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1971 | struct ParallelIOArg *parg = arg; |
| 1972 | int n = write(fd, parg->buffer, parg->count); |
| 1973 | if (n != parg->count) { |
| 1974 | return -EIO; |
| 1975 | } |
| 1976 | } |
| 1977 | break; |
| 1978 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1979 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1980 | struct ParallelIOArg *parg = arg; |
| 1981 | int n = write(fd, parg->buffer, parg->count); |
| 1982 | if (n != parg->count) { |
| 1983 | return -EIO; |
| 1984 | } |
| 1985 | } |
| 1986 | break; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1987 | default: |
| 1988 | return -ENOTSUP; |
| 1989 | } |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1993 | static void pp_close(CharDriverState *chr) |
| 1994 | { |
| 1995 | ParallelCharDriver *drv = chr->opaque; |
| 1996 | int fd = drv->fd; |
| 1997 | |
| 1998 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 1999 | ioctl(fd, PPRELEASE); |
| 2000 | close(fd); |
| 2001 | qemu_free(drv); |
| 2002 | } |
| 2003 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2004 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2005 | { |
| 2006 | CharDriverState *chr; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2007 | ParallelCharDriver *drv; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2008 | int fd; |
| 2009 | |
| 2010 | fd = open(filename, O_RDWR); |
| 2011 | if (fd < 0) |
| 2012 | return NULL; |
| 2013 | |
| 2014 | if (ioctl(fd, PPCLAIM) < 0) { |
| 2015 | close(fd); |
| 2016 | return NULL; |
| 2017 | } |
| 2018 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2019 | drv = qemu_mallocz(sizeof(ParallelCharDriver)); |
| 2020 | if (!drv) { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2021 | close(fd); |
| 2022 | return NULL; |
| 2023 | } |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2024 | drv->fd = fd; |
| 2025 | drv->mode = IEEE1284_MODE_COMPAT; |
| 2026 | |
| 2027 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2028 | if (!chr) { |
| 2029 | qemu_free(drv); |
| 2030 | close(fd); |
| 2031 | return NULL; |
| 2032 | } |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2033 | chr->chr_write = null_chr_write; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2034 | chr->chr_ioctl = pp_ioctl; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2035 | chr->chr_close = pp_close; |
| 2036 | chr->opaque = drv; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2037 | |
| 2038 | qemu_chr_reset(chr); |
| 2039 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2040 | return chr; |
| 2041 | } |
| 2042 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2043 | #else |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2044 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2045 | { |
| 2046 | return NULL; |
| 2047 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2048 | #endif |
| 2049 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2050 | #endif /* !defined(_WIN32) */ |
| 2051 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2052 | #ifdef _WIN32 |
| 2053 | typedef struct { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2054 | int max_size; |
| 2055 | HANDLE hcom, hrecv, hsend; |
| 2056 | OVERLAPPED orecv, osend; |
| 2057 | BOOL fpipe; |
| 2058 | DWORD len; |
| 2059 | } WinCharState; |
| 2060 | |
| 2061 | #define NSENDBUF 2048 |
| 2062 | #define NRECVBUF 2048 |
| 2063 | #define MAXCONNECT 1 |
| 2064 | #define NTIMEOUT 5000 |
| 2065 | |
| 2066 | static int win_chr_poll(void *opaque); |
| 2067 | static int win_chr_pipe_poll(void *opaque); |
| 2068 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2069 | static void win_chr_close(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2070 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2071 | WinCharState *s = chr->opaque; |
| 2072 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2073 | if (s->hsend) { |
| 2074 | CloseHandle(s->hsend); |
| 2075 | s->hsend = NULL; |
| 2076 | } |
| 2077 | if (s->hrecv) { |
| 2078 | CloseHandle(s->hrecv); |
| 2079 | s->hrecv = NULL; |
| 2080 | } |
| 2081 | if (s->hcom) { |
| 2082 | CloseHandle(s->hcom); |
| 2083 | s->hcom = NULL; |
| 2084 | } |
| 2085 | if (s->fpipe) |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2086 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2087 | else |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2088 | qemu_del_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2091 | static int win_chr_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2092 | { |
| 2093 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2094 | COMMCONFIG comcfg; |
| 2095 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 2096 | COMSTAT comstat; |
| 2097 | DWORD size; |
| 2098 | DWORD err; |
| 2099 | |
| 2100 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2101 | if (!s->hsend) { |
| 2102 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2103 | goto fail; |
| 2104 | } |
| 2105 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2106 | if (!s->hrecv) { |
| 2107 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2108 | goto fail; |
| 2109 | } |
| 2110 | |
| 2111 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 2112 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 2113 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2114 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 2115 | s->hcom = NULL; |
| 2116 | goto fail; |
| 2117 | } |
| 2118 | |
| 2119 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 2120 | fprintf(stderr, "Failed SetupComm\n"); |
| 2121 | goto fail; |
| 2122 | } |
| 2123 | |
| 2124 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 2125 | size = sizeof(COMMCONFIG); |
| 2126 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 2127 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 2128 | CommConfigDialog(filename, NULL, &comcfg); |
| 2129 | |
| 2130 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 2131 | fprintf(stderr, "Failed SetCommState\n"); |
| 2132 | goto fail; |
| 2133 | } |
| 2134 | |
| 2135 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 2136 | fprintf(stderr, "Failed SetCommMask\n"); |
| 2137 | goto fail; |
| 2138 | } |
| 2139 | |
| 2140 | cto.ReadIntervalTimeout = MAXDWORD; |
| 2141 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 2142 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 2143 | goto fail; |
| 2144 | } |
| 2145 | |
| 2146 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 2147 | fprintf(stderr, "Failed ClearCommError\n"); |
| 2148 | goto fail; |
| 2149 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2150 | qemu_add_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2151 | return 0; |
| 2152 | |
| 2153 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2154 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2155 | return -1; |
| 2156 | } |
| 2157 | |
| 2158 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2159 | { |
| 2160 | WinCharState *s = chr->opaque; |
| 2161 | DWORD len, ret, size, err; |
| 2162 | |
| 2163 | len = len1; |
| 2164 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2165 | s->osend.hEvent = s->hsend; |
| 2166 | while (len > 0) { |
| 2167 | if (s->hsend) |
| 2168 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2169 | else |
| 2170 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2171 | if (!ret) { |
| 2172 | err = GetLastError(); |
| 2173 | if (err == ERROR_IO_PENDING) { |
| 2174 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2175 | if (ret) { |
| 2176 | buf += size; |
| 2177 | len -= size; |
| 2178 | } else { |
| 2179 | break; |
| 2180 | } |
| 2181 | } else { |
| 2182 | break; |
| 2183 | } |
| 2184 | } else { |
| 2185 | buf += size; |
| 2186 | len -= size; |
| 2187 | } |
| 2188 | } |
| 2189 | return len1 - len; |
| 2190 | } |
| 2191 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2192 | static int win_chr_read_poll(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2193 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2194 | WinCharState *s = chr->opaque; |
| 2195 | |
| 2196 | s->max_size = qemu_chr_can_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2197 | return s->max_size; |
| 2198 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2199 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2200 | static void win_chr_readfile(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2201 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2202 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2203 | int ret, err; |
| 2204 | uint8_t buf[1024]; |
| 2205 | DWORD size; |
| 2206 | |
| 2207 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2208 | s->orecv.hEvent = s->hrecv; |
| 2209 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2210 | if (!ret) { |
| 2211 | err = GetLastError(); |
| 2212 | if (err == ERROR_IO_PENDING) { |
| 2213 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | if (size > 0) { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2218 | qemu_chr_read(chr, buf, size); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2222 | static void win_chr_read(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2223 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2224 | WinCharState *s = chr->opaque; |
| 2225 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2226 | if (s->len > s->max_size) |
| 2227 | s->len = s->max_size; |
| 2228 | if (s->len == 0) |
| 2229 | return; |
| 2230 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2231 | win_chr_readfile(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | static int win_chr_poll(void *opaque) |
| 2235 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2236 | CharDriverState *chr = opaque; |
| 2237 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2238 | COMSTAT status; |
| 2239 | DWORD comerr; |
| 2240 | |
| 2241 | ClearCommError(s->hcom, &comerr, &status); |
| 2242 | if (status.cbInQue > 0) { |
| 2243 | s->len = status.cbInQue; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2244 | win_chr_read_poll(chr); |
| 2245 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2246 | return 1; |
| 2247 | } |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2251 | static CharDriverState *qemu_chr_open_win(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2252 | { |
| 2253 | CharDriverState *chr; |
| 2254 | WinCharState *s; |
| 2255 | |
| 2256 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2257 | if (!chr) |
| 2258 | return NULL; |
| 2259 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2260 | if (!s) { |
| 2261 | free(chr); |
| 2262 | return NULL; |
| 2263 | } |
| 2264 | chr->opaque = s; |
| 2265 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2266 | chr->chr_close = win_chr_close; |
| 2267 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2268 | if (win_chr_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2269 | free(s); |
| 2270 | free(chr); |
| 2271 | return NULL; |
| 2272 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2273 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2274 | return chr; |
| 2275 | } |
| 2276 | |
| 2277 | static int win_chr_pipe_poll(void *opaque) |
| 2278 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2279 | CharDriverState *chr = opaque; |
| 2280 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2281 | DWORD size; |
| 2282 | |
| 2283 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2284 | if (size > 0) { |
| 2285 | s->len = size; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2286 | win_chr_read_poll(chr); |
| 2287 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2288 | return 1; |
| 2289 | } |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2293 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2294 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2295 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2296 | OVERLAPPED ov; |
| 2297 | int ret; |
| 2298 | DWORD size; |
| 2299 | char openname[256]; |
| 2300 | |
| 2301 | s->fpipe = TRUE; |
| 2302 | |
| 2303 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2304 | if (!s->hsend) { |
| 2305 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2306 | goto fail; |
| 2307 | } |
| 2308 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2309 | if (!s->hrecv) { |
| 2310 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2311 | goto fail; |
| 2312 | } |
| 2313 | |
| 2314 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 2315 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2316 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2317 | PIPE_WAIT, |
| 2318 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 2319 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2320 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 2321 | s->hcom = NULL; |
| 2322 | goto fail; |
| 2323 | } |
| 2324 | |
| 2325 | ZeroMemory(&ov, sizeof(ov)); |
| 2326 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2327 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2328 | if (ret) { |
| 2329 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 2330 | goto fail; |
| 2331 | } |
| 2332 | |
| 2333 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2334 | if (!ret) { |
| 2335 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 2336 | if (ov.hEvent) { |
| 2337 | CloseHandle(ov.hEvent); |
| 2338 | ov.hEvent = NULL; |
| 2339 | } |
| 2340 | goto fail; |
| 2341 | } |
| 2342 | |
| 2343 | if (ov.hEvent) { |
| 2344 | CloseHandle(ov.hEvent); |
| 2345 | ov.hEvent = NULL; |
| 2346 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2347 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2348 | return 0; |
| 2349 | |
| 2350 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2351 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2352 | return -1; |
| 2353 | } |
| 2354 | |
| 2355 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2356 | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2357 | { |
| 2358 | CharDriverState *chr; |
| 2359 | WinCharState *s; |
| 2360 | |
| 2361 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2362 | if (!chr) |
| 2363 | return NULL; |
| 2364 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2365 | if (!s) { |
| 2366 | free(chr); |
| 2367 | return NULL; |
| 2368 | } |
| 2369 | chr->opaque = s; |
| 2370 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2371 | chr->chr_close = win_chr_close; |
| 2372 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2373 | if (win_chr_pipe_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2374 | free(s); |
| 2375 | free(chr); |
| 2376 | return NULL; |
| 2377 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2378 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2379 | return chr; |
| 2380 | } |
| 2381 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2382 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2383 | { |
| 2384 | CharDriverState *chr; |
| 2385 | WinCharState *s; |
| 2386 | |
| 2387 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2388 | if (!chr) |
| 2389 | return NULL; |
| 2390 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2391 | if (!s) { |
| 2392 | free(chr); |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | s->hcom = fd_out; |
| 2396 | chr->opaque = s; |
| 2397 | chr->chr_write = win_chr_write; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2398 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2399 | return chr; |
| 2400 | } |
| 2401 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2402 | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2403 | { |
| 2404 | HANDLE fd_out; |
| 2405 | |
| 2406 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 2407 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 2408 | if (fd_out == INVALID_HANDLE_VALUE) |
| 2409 | return NULL; |
| 2410 | |
| 2411 | return qemu_chr_open_win_file(fd_out); |
| 2412 | } |
| 2413 | #endif |
| 2414 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2415 | /***********************************************************/ |
| 2416 | /* UDP Net console */ |
| 2417 | |
| 2418 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2419 | int fd; |
| 2420 | struct sockaddr_in daddr; |
| 2421 | char buf[1024]; |
| 2422 | int bufcnt; |
| 2423 | int bufptr; |
| 2424 | int max_size; |
| 2425 | } NetCharDriver; |
| 2426 | |
| 2427 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2428 | { |
| 2429 | NetCharDriver *s = chr->opaque; |
| 2430 | |
| 2431 | return sendto(s->fd, buf, len, 0, |
| 2432 | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
| 2433 | } |
| 2434 | |
| 2435 | static int udp_chr_read_poll(void *opaque) |
| 2436 | { |
| 2437 | CharDriverState *chr = opaque; |
| 2438 | NetCharDriver *s = chr->opaque; |
| 2439 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2440 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2441 | |
| 2442 | /* If there were any stray characters in the queue process them |
| 2443 | * first |
| 2444 | */ |
| 2445 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2446 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2447 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2448 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2449 | } |
| 2450 | return s->max_size; |
| 2451 | } |
| 2452 | |
| 2453 | static void udp_chr_read(void *opaque) |
| 2454 | { |
| 2455 | CharDriverState *chr = opaque; |
| 2456 | NetCharDriver *s = chr->opaque; |
| 2457 | |
| 2458 | if (s->max_size == 0) |
| 2459 | return; |
| 2460 | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 2461 | s->bufptr = s->bufcnt; |
| 2462 | if (s->bufcnt <= 0) |
| 2463 | return; |
| 2464 | |
| 2465 | s->bufptr = 0; |
| 2466 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2467 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2468 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2469 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2473 | static void udp_chr_update_read_handler(CharDriverState *chr) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2474 | { |
| 2475 | NetCharDriver *s = chr->opaque; |
| 2476 | |
| 2477 | if (s->fd >= 0) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2478 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 2479 | udp_chr_read, NULL, chr); |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2484 | #ifndef _WIN32 |
| 2485 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
| 2486 | #endif |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2487 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 2488 | struct sockaddr_in *saddr, |
| 2489 | const char *str); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2490 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2491 | static CharDriverState *qemu_chr_open_udp(const char *def) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2492 | { |
| 2493 | CharDriverState *chr = NULL; |
| 2494 | NetCharDriver *s = NULL; |
| 2495 | int fd = -1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2496 | struct sockaddr_in saddr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2497 | |
| 2498 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2499 | if (!chr) |
| 2500 | goto return_err; |
| 2501 | s = qemu_mallocz(sizeof(NetCharDriver)); |
| 2502 | if (!s) |
| 2503 | goto return_err; |
| 2504 | |
| 2505 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 2506 | if (fd < 0) { |
| 2507 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 2508 | goto return_err; |
| 2509 | } |
| 2510 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2511 | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
| 2512 | printf("Could not parse: %s\n", def); |
| 2513 | goto return_err; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2516 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2517 | { |
| 2518 | perror("bind"); |
| 2519 | goto return_err; |
| 2520 | } |
| 2521 | |
| 2522 | s->fd = fd; |
| 2523 | s->bufcnt = 0; |
| 2524 | s->bufptr = 0; |
| 2525 | chr->opaque = s; |
| 2526 | chr->chr_write = udp_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2527 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2528 | return chr; |
| 2529 | |
| 2530 | return_err: |
| 2531 | if (chr) |
| 2532 | free(chr); |
| 2533 | if (s) |
| 2534 | free(s); |
| 2535 | if (fd >= 0) |
| 2536 | closesocket(fd); |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | |
| 2540 | /***********************************************************/ |
| 2541 | /* TCP Net console */ |
| 2542 | |
| 2543 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2544 | int fd, listen_fd; |
| 2545 | int connected; |
| 2546 | int max_size; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2547 | int do_telnetopt; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2548 | int do_nodelay; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2549 | int is_unix; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2550 | } TCPCharDriver; |
| 2551 | |
| 2552 | static void tcp_chr_accept(void *opaque); |
| 2553 | |
| 2554 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2555 | { |
| 2556 | TCPCharDriver *s = chr->opaque; |
| 2557 | if (s->connected) { |
| 2558 | return send_all(s->fd, buf, len); |
| 2559 | } else { |
| 2560 | /* XXX: indicate an error ? */ |
| 2561 | return len; |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | static int tcp_chr_read_poll(void *opaque) |
| 2566 | { |
| 2567 | CharDriverState *chr = opaque; |
| 2568 | TCPCharDriver *s = chr->opaque; |
| 2569 | if (!s->connected) |
| 2570 | return 0; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2571 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2572 | return s->max_size; |
| 2573 | } |
| 2574 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2575 | #define IAC 255 |
| 2576 | #define IAC_BREAK 243 |
| 2577 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2578 | TCPCharDriver *s, |
| 2579 | char *buf, int *size) |
| 2580 | { |
| 2581 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2582 | * char mode with no echo. All IAC options will be removed from |
| 2583 | * the buf and the do_telnetopt variable will be used to track the |
| 2584 | * state of the width of the IAC information. |
| 2585 | * |
| 2586 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2587 | * "IAC BREAK" command and the double IAC. |
| 2588 | */ |
| 2589 | |
| 2590 | int i; |
| 2591 | int j = 0; |
| 2592 | |
| 2593 | for (i = 0; i < *size; i++) { |
| 2594 | if (s->do_telnetopt > 1) { |
| 2595 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2596 | /* Double IAC means send an IAC */ |
| 2597 | if (j != i) |
| 2598 | buf[j] = buf[i]; |
| 2599 | j++; |
| 2600 | s->do_telnetopt = 1; |
| 2601 | } else { |
| 2602 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2603 | /* Handle IAC break commands by sending a serial break */ |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2604 | qemu_chr_event(chr, CHR_EVENT_BREAK); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2605 | s->do_telnetopt++; |
| 2606 | } |
| 2607 | s->do_telnetopt++; |
| 2608 | } |
| 2609 | if (s->do_telnetopt >= 4) { |
| 2610 | s->do_telnetopt = 1; |
| 2611 | } |
| 2612 | } else { |
| 2613 | if ((unsigned char)buf[i] == IAC) { |
| 2614 | s->do_telnetopt = 2; |
| 2615 | } else { |
| 2616 | if (j != i) |
| 2617 | buf[j] = buf[i]; |
| 2618 | j++; |
| 2619 | } |
| 2620 | } |
| 2621 | } |
| 2622 | *size = j; |
| 2623 | } |
| 2624 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2625 | static void tcp_chr_read(void *opaque) |
| 2626 | { |
| 2627 | CharDriverState *chr = opaque; |
| 2628 | TCPCharDriver *s = chr->opaque; |
| 2629 | uint8_t buf[1024]; |
| 2630 | int len, size; |
| 2631 | |
| 2632 | if (!s->connected || s->max_size <= 0) |
| 2633 | return; |
| 2634 | len = sizeof(buf); |
| 2635 | if (len > s->max_size) |
| 2636 | len = s->max_size; |
| 2637 | size = recv(s->fd, buf, len, 0); |
| 2638 | if (size == 0) { |
| 2639 | /* connection closed */ |
| 2640 | s->connected = 0; |
| 2641 | if (s->listen_fd >= 0) { |
| 2642 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
| 2643 | } |
| 2644 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 2645 | closesocket(s->fd); |
| 2646 | s->fd = -1; |
| 2647 | } else if (size > 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2648 | if (s->do_telnetopt) |
| 2649 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2650 | if (size > 0) |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2651 | qemu_chr_read(chr, buf, size); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2655 | static void tcp_chr_connect(void *opaque) |
| 2656 | { |
| 2657 | CharDriverState *chr = opaque; |
| 2658 | TCPCharDriver *s = chr->opaque; |
| 2659 | |
| 2660 | s->connected = 1; |
| 2661 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 2662 | tcp_chr_read, NULL, chr); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2663 | qemu_chr_reset(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2666 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2667 | static void tcp_chr_telnet_init(int fd) |
| 2668 | { |
| 2669 | char buf[3]; |
| 2670 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2671 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2672 | send(fd, (char *)buf, 3, 0); |
| 2673 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2674 | send(fd, (char *)buf, 3, 0); |
| 2675 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2676 | send(fd, (char *)buf, 3, 0); |
| 2677 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2678 | send(fd, (char *)buf, 3, 0); |
| 2679 | } |
| 2680 | |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2681 | static void socket_set_nodelay(int fd) |
| 2682 | { |
| 2683 | int val = 1; |
| 2684 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| 2685 | } |
| 2686 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2687 | static void tcp_chr_accept(void *opaque) |
| 2688 | { |
| 2689 | CharDriverState *chr = opaque; |
| 2690 | TCPCharDriver *s = chr->opaque; |
| 2691 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2692 | #ifndef _WIN32 |
| 2693 | struct sockaddr_un uaddr; |
| 2694 | #endif |
| 2695 | struct sockaddr *addr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2696 | socklen_t len; |
| 2697 | int fd; |
| 2698 | |
| 2699 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2700 | #ifndef _WIN32 |
| 2701 | if (s->is_unix) { |
| 2702 | len = sizeof(uaddr); |
| 2703 | addr = (struct sockaddr *)&uaddr; |
| 2704 | } else |
| 2705 | #endif |
| 2706 | { |
| 2707 | len = sizeof(saddr); |
| 2708 | addr = (struct sockaddr *)&saddr; |
| 2709 | } |
| 2710 | fd = accept(s->listen_fd, addr, &len); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2711 | if (fd < 0 && errno != EINTR) { |
| 2712 | return; |
| 2713 | } else if (fd >= 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2714 | if (s->do_telnetopt) |
| 2715 | tcp_chr_telnet_init(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2716 | break; |
| 2717 | } |
| 2718 | } |
| 2719 | socket_set_nonblock(fd); |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2720 | if (s->do_nodelay) |
| 2721 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2722 | s->fd = fd; |
| 2723 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 2724 | tcp_chr_connect(chr); |
| 2725 | } |
| 2726 | |
| 2727 | static void tcp_chr_close(CharDriverState *chr) |
| 2728 | { |
| 2729 | TCPCharDriver *s = chr->opaque; |
| 2730 | if (s->fd >= 0) |
| 2731 | closesocket(s->fd); |
| 2732 | if (s->listen_fd >= 0) |
| 2733 | closesocket(s->listen_fd); |
| 2734 | qemu_free(s); |
| 2735 | } |
| 2736 | |
| 2737 | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2738 | int is_telnet, |
| 2739 | int is_unix) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2740 | { |
| 2741 | CharDriverState *chr = NULL; |
| 2742 | TCPCharDriver *s = NULL; |
| 2743 | int fd = -1, ret, err, val; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2744 | int is_listen = 0; |
| 2745 | int is_waitconnect = 1; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2746 | int do_nodelay = 0; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2747 | const char *ptr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2748 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2749 | #ifndef _WIN32 |
| 2750 | struct sockaddr_un uaddr; |
| 2751 | #endif |
| 2752 | struct sockaddr *addr; |
| 2753 | socklen_t addrlen; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2754 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2755 | #ifndef _WIN32 |
| 2756 | if (is_unix) { |
| 2757 | addr = (struct sockaddr *)&uaddr; |
| 2758 | addrlen = sizeof(uaddr); |
| 2759 | if (parse_unix_path(&uaddr, host_str) < 0) |
| 2760 | goto fail; |
| 2761 | } else |
| 2762 | #endif |
| 2763 | { |
| 2764 | addr = (struct sockaddr *)&saddr; |
| 2765 | addrlen = sizeof(saddr); |
| 2766 | if (parse_host_port(&saddr, host_str) < 0) |
| 2767 | goto fail; |
| 2768 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2769 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2770 | ptr = host_str; |
| 2771 | while((ptr = strchr(ptr,','))) { |
| 2772 | ptr++; |
| 2773 | if (!strncmp(ptr,"server",6)) { |
| 2774 | is_listen = 1; |
| 2775 | } else if (!strncmp(ptr,"nowait",6)) { |
| 2776 | is_waitconnect = 0; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2777 | } else if (!strncmp(ptr,"nodelay",6)) { |
| 2778 | do_nodelay = 1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2779 | } else { |
| 2780 | printf("Unknown option: %s\n", ptr); |
| 2781 | goto fail; |
| 2782 | } |
| 2783 | } |
| 2784 | if (!is_listen) |
| 2785 | is_waitconnect = 0; |
| 2786 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2787 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2788 | if (!chr) |
| 2789 | goto fail; |
| 2790 | s = qemu_mallocz(sizeof(TCPCharDriver)); |
| 2791 | if (!s) |
| 2792 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2793 | |
| 2794 | #ifndef _WIN32 |
| 2795 | if (is_unix) |
| 2796 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 2797 | else |
| 2798 | #endif |
| 2799 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 2800 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2801 | if (fd < 0) |
| 2802 | goto fail; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2803 | |
| 2804 | if (!is_waitconnect) |
| 2805 | socket_set_nonblock(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2806 | |
| 2807 | s->connected = 0; |
| 2808 | s->fd = -1; |
| 2809 | s->listen_fd = -1; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2810 | s->is_unix = is_unix; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2811 | s->do_nodelay = do_nodelay && !is_unix; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2812 | |
| 2813 | chr->opaque = s; |
| 2814 | chr->chr_write = tcp_chr_write; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2815 | chr->chr_close = tcp_chr_close; |
| 2816 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2817 | if (is_listen) { |
| 2818 | /* allow fast reuse */ |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2819 | #ifndef _WIN32 |
| 2820 | if (is_unix) { |
| 2821 | char path[109]; |
| 2822 | strncpy(path, uaddr.sun_path, 108); |
| 2823 | path[108] = 0; |
| 2824 | unlink(path); |
| 2825 | } else |
| 2826 | #endif |
| 2827 | { |
| 2828 | val = 1; |
| 2829 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 2830 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2831 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2832 | ret = bind(fd, addr, addrlen); |
| 2833 | if (ret < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2834 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2835 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2836 | ret = listen(fd, 0); |
| 2837 | if (ret < 0) |
| 2838 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2839 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2840 | s->listen_fd = fd; |
| 2841 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2842 | if (is_telnet) |
| 2843 | s->do_telnetopt = 1; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2844 | } else { |
| 2845 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2846 | ret = connect(fd, addr, addrlen); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2847 | if (ret < 0) { |
| 2848 | err = socket_error(); |
| 2849 | if (err == EINTR || err == EWOULDBLOCK) { |
| 2850 | } else if (err == EINPROGRESS) { |
| 2851 | break; |
| 2852 | } else { |
| 2853 | goto fail; |
| 2854 | } |
| 2855 | } else { |
| 2856 | s->connected = 1; |
| 2857 | break; |
| 2858 | } |
| 2859 | } |
| 2860 | s->fd = fd; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2861 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2862 | if (s->connected) |
| 2863 | tcp_chr_connect(chr); |
| 2864 | else |
| 2865 | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr); |
| 2866 | } |
| 2867 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2868 | if (is_listen && is_waitconnect) { |
| 2869 | printf("QEMU waiting for connection on: %s\n", host_str); |
| 2870 | tcp_chr_accept(chr); |
| 2871 | socket_set_nonblock(s->listen_fd); |
| 2872 | } |
| 2873 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2874 | return chr; |
| 2875 | fail: |
| 2876 | if (fd >= 0) |
| 2877 | closesocket(fd); |
| 2878 | qemu_free(s); |
| 2879 | qemu_free(chr); |
| 2880 | return NULL; |
| 2881 | } |
| 2882 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2883 | CharDriverState *qemu_chr_open(const char *filename) |
| 2884 | { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2885 | const char *p; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2886 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2887 | if (!strcmp(filename, "vc")) { |
| 2888 | return text_console_init(&display_state); |
| 2889 | } else if (!strcmp(filename, "null")) { |
| 2890 | return qemu_chr_open_null(); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2891 | } else |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2892 | if (strstart(filename, "tcp:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2893 | return qemu_chr_open_tcp(p, 0, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2894 | } else |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2895 | if (strstart(filename, "telnet:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2896 | return qemu_chr_open_tcp(p, 1, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2897 | } else |
| 2898 | if (strstart(filename, "udp:", &p)) { |
| 2899 | return qemu_chr_open_udp(p); |
| 2900 | } else |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2901 | if (strstart(filename, "mon:", &p)) { |
| 2902 | CharDriverState *drv = qemu_chr_open(p); |
| 2903 | if (drv) { |
| 2904 | drv = qemu_chr_open_mux(drv); |
| 2905 | monitor_init(drv, !nographic); |
| 2906 | return drv; |
| 2907 | } |
| 2908 | printf("Unable to open driver: %s\n", p); |
| 2909 | return 0; |
| 2910 | } else |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2911 | #ifndef _WIN32 |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2912 | if (strstart(filename, "unix:", &p)) { |
| 2913 | return qemu_chr_open_tcp(p, 0, 1); |
| 2914 | } else if (strstart(filename, "file:", &p)) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2915 | return qemu_chr_open_file_out(p); |
| 2916 | } else if (strstart(filename, "pipe:", &p)) { |
| 2917 | return qemu_chr_open_pipe(p); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2918 | } else if (!strcmp(filename, "pty")) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2919 | return qemu_chr_open_pty(); |
| 2920 | } else if (!strcmp(filename, "stdio")) { |
| 2921 | return qemu_chr_open_stdio(); |
| 2922 | } else |
| 2923 | #endif |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2924 | #if defined(__linux__) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2925 | if (strstart(filename, "/dev/parport", NULL)) { |
| 2926 | return qemu_chr_open_pp(filename); |
| 2927 | } else |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2928 | if (strstart(filename, "/dev/", NULL)) { |
| 2929 | return qemu_chr_open_tty(filename); |
| 2930 | } else |
| 2931 | #endif |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2932 | #ifdef _WIN32 |
| 2933 | if (strstart(filename, "COM", NULL)) { |
| 2934 | return qemu_chr_open_win(filename); |
| 2935 | } else |
| 2936 | if (strstart(filename, "pipe:", &p)) { |
| 2937 | return qemu_chr_open_win_pipe(p); |
| 2938 | } else |
| 2939 | if (strstart(filename, "file:", &p)) { |
| 2940 | return qemu_chr_open_win_file_out(p); |
| 2941 | } |
| 2942 | #endif |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2943 | { |
| 2944 | return NULL; |
| 2945 | } |
| 2946 | } |
| 2947 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2948 | void qemu_chr_close(CharDriverState *chr) |
| 2949 | { |
| 2950 | if (chr->chr_close) |
| 2951 | chr->chr_close(chr); |
| 2952 | } |
| 2953 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2954 | /***********************************************************/ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2955 | /* network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2956 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2957 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2958 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2959 | int len, i, j, c; |
| 2960 | |
| 2961 | for(i=0;i<size;i+=16) { |
| 2962 | len = size - i; |
| 2963 | if (len > 16) |
| 2964 | len = 16; |
| 2965 | fprintf(f, "%08x ", i); |
| 2966 | for(j=0;j<16;j++) { |
| 2967 | if (j < len) |
| 2968 | fprintf(f, " %02x", buf[i+j]); |
| 2969 | else |
| 2970 | fprintf(f, " "); |
| 2971 | } |
| 2972 | fprintf(f, " "); |
| 2973 | for(j=0;j<len;j++) { |
| 2974 | c = buf[i+j]; |
| 2975 | if (c < ' ' || c > '~') |
| 2976 | c = '.'; |
| 2977 | fprintf(f, "%c", c); |
| 2978 | } |
| 2979 | fprintf(f, "\n"); |
| 2980 | } |
| 2981 | } |
| 2982 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2983 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2984 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2985 | int i; |
| 2986 | for(i = 0; i < 6; i++) { |
| 2987 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 2988 | if (i == 5) { |
| 2989 | if (*p != '\0') |
| 2990 | return -1; |
| 2991 | } else { |
| 2992 | if (*p != ':') |
| 2993 | return -1; |
| 2994 | p++; |
| 2995 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2996 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2997 | return 0; |
| 2998 | } |
| 2999 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3000 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 3001 | { |
| 3002 | const char *p, *p1; |
| 3003 | int len; |
| 3004 | p = *pp; |
| 3005 | p1 = strchr(p, sep); |
| 3006 | if (!p1) |
| 3007 | return -1; |
| 3008 | len = p1 - p; |
| 3009 | p1++; |
| 3010 | if (buf_size > 0) { |
| 3011 | if (len > buf_size - 1) |
| 3012 | len = buf_size - 1; |
| 3013 | memcpy(buf, p, len); |
| 3014 | buf[len] = '\0'; |
| 3015 | } |
| 3016 | *pp = p1; |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3020 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 3021 | struct sockaddr_in *saddr, |
| 3022 | const char *input_str) |
| 3023 | { |
| 3024 | char *str = strdup(input_str); |
| 3025 | char *host_str = str; |
| 3026 | char *src_str; |
| 3027 | char *ptr; |
| 3028 | |
| 3029 | /* |
| 3030 | * Chop off any extra arguments at the end of the string which |
| 3031 | * would start with a comma, then fill in the src port information |
| 3032 | * if it was provided else use the "any address" and "any port". |
| 3033 | */ |
| 3034 | if ((ptr = strchr(str,','))) |
| 3035 | *ptr = '\0'; |
| 3036 | |
| 3037 | if ((src_str = strchr(input_str,'@'))) { |
| 3038 | *src_str = '\0'; |
| 3039 | src_str++; |
| 3040 | } |
| 3041 | |
| 3042 | if (parse_host_port(haddr, host_str) < 0) |
| 3043 | goto fail; |
| 3044 | |
| 3045 | if (!src_str || *src_str == '\0') |
| 3046 | src_str = ":0"; |
| 3047 | |
| 3048 | if (parse_host_port(saddr, src_str) < 0) |
| 3049 | goto fail; |
| 3050 | |
| 3051 | free(str); |
| 3052 | return(0); |
| 3053 | |
| 3054 | fail: |
| 3055 | free(str); |
| 3056 | return -1; |
| 3057 | } |
| 3058 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3059 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 3060 | { |
| 3061 | char buf[512]; |
| 3062 | struct hostent *he; |
| 3063 | const char *p, *r; |
| 3064 | int port; |
| 3065 | |
| 3066 | p = str; |
| 3067 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3068 | return -1; |
| 3069 | saddr->sin_family = AF_INET; |
| 3070 | if (buf[0] == '\0') { |
| 3071 | saddr->sin_addr.s_addr = 0; |
| 3072 | } else { |
| 3073 | if (isdigit(buf[0])) { |
| 3074 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 3075 | return -1; |
| 3076 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3077 | if ((he = gethostbyname(buf)) == NULL) |
| 3078 | return - 1; |
| 3079 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3080 | } |
| 3081 | } |
| 3082 | port = strtol(p, (char **)&r, 0); |
| 3083 | if (r == p) |
| 3084 | return -1; |
| 3085 | saddr->sin_port = htons(port); |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3089 | #ifndef _WIN32 |
| 3090 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3091 | { |
| 3092 | const char *p; |
| 3093 | int len; |
| 3094 | |
| 3095 | len = MIN(108, strlen(str)); |
| 3096 | p = strchr(str, ','); |
| 3097 | if (p) |
| 3098 | len = MIN(len, p - str); |
| 3099 | |
| 3100 | memset(uaddr, 0, sizeof(*uaddr)); |
| 3101 | |
| 3102 | uaddr->sun_family = AF_UNIX; |
| 3103 | memcpy(uaddr->sun_path, str, len); |
| 3104 | |
| 3105 | return 0; |
| 3106 | } |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3107 | #endif |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3108 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3109 | /* find or alloc a new VLAN */ |
| 3110 | VLANState *qemu_find_vlan(int id) |
| 3111 | { |
| 3112 | VLANState **pvlan, *vlan; |
| 3113 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 3114 | if (vlan->id == id) |
| 3115 | return vlan; |
| 3116 | } |
| 3117 | vlan = qemu_mallocz(sizeof(VLANState)); |
| 3118 | if (!vlan) |
| 3119 | return NULL; |
| 3120 | vlan->id = id; |
| 3121 | vlan->next = NULL; |
| 3122 | pvlan = &first_vlan; |
| 3123 | while (*pvlan != NULL) |
| 3124 | pvlan = &(*pvlan)->next; |
| 3125 | *pvlan = vlan; |
| 3126 | return vlan; |
| 3127 | } |
| 3128 | |
| 3129 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3130 | IOReadHandler *fd_read, |
| 3131 | IOCanRWHandler *fd_can_read, |
| 3132 | void *opaque) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3133 | { |
| 3134 | VLANClientState *vc, **pvc; |
| 3135 | vc = qemu_mallocz(sizeof(VLANClientState)); |
| 3136 | if (!vc) |
| 3137 | return NULL; |
| 3138 | vc->fd_read = fd_read; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3139 | vc->fd_can_read = fd_can_read; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3140 | vc->opaque = opaque; |
| 3141 | vc->vlan = vlan; |
| 3142 | |
| 3143 | vc->next = NULL; |
| 3144 | pvc = &vlan->first_client; |
| 3145 | while (*pvc != NULL) |
| 3146 | pvc = &(*pvc)->next; |
| 3147 | *pvc = vc; |
| 3148 | return vc; |
| 3149 | } |
| 3150 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3151 | int qemu_can_send_packet(VLANClientState *vc1) |
| 3152 | { |
| 3153 | VLANState *vlan = vc1->vlan; |
| 3154 | VLANClientState *vc; |
| 3155 | |
| 3156 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3157 | if (vc != vc1) { |
| 3158 | if (vc->fd_can_read && !vc->fd_can_read(vc->opaque)) |
| 3159 | return 0; |
| 3160 | } |
| 3161 | } |
| 3162 | return 1; |
| 3163 | } |
| 3164 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3165 | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
| 3166 | { |
| 3167 | VLANState *vlan = vc1->vlan; |
| 3168 | VLANClientState *vc; |
| 3169 | |
| 3170 | #if 0 |
| 3171 | printf("vlan %d send:\n", vlan->id); |
| 3172 | hex_dump(stdout, buf, size); |
| 3173 | #endif |
| 3174 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3175 | if (vc != vc1) { |
| 3176 | vc->fd_read(vc->opaque, buf, size); |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | #if defined(CONFIG_SLIRP) |
| 3182 | |
| 3183 | /* slirp network adapter */ |
| 3184 | |
| 3185 | static int slirp_inited; |
| 3186 | static VLANClientState *slirp_vc; |
| 3187 | |
| 3188 | int slirp_can_output(void) |
| 3189 | { |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3190 | return !slirp_vc || qemu_can_send_packet(slirp_vc); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 3194 | { |
| 3195 | #if 0 |
| 3196 | printf("slirp output:\n"); |
| 3197 | hex_dump(stdout, pkt, pkt_len); |
| 3198 | #endif |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3199 | if (!slirp_vc) |
| 3200 | return; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3201 | qemu_send_packet(slirp_vc, pkt, pkt_len); |
| 3202 | } |
| 3203 | |
| 3204 | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
| 3205 | { |
| 3206 | #if 0 |
| 3207 | printf("slirp input:\n"); |
| 3208 | hex_dump(stdout, buf, size); |
| 3209 | #endif |
| 3210 | slirp_input(buf, size); |
| 3211 | } |
| 3212 | |
| 3213 | static int net_slirp_init(VLANState *vlan) |
| 3214 | { |
| 3215 | if (!slirp_inited) { |
| 3216 | slirp_inited = 1; |
| 3217 | slirp_init(); |
| 3218 | } |
| 3219 | slirp_vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3220 | slirp_receive, NULL, NULL); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3221 | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
| 3222 | return 0; |
| 3223 | } |
| 3224 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3225 | static void net_slirp_redir(const char *redir_str) |
| 3226 | { |
| 3227 | int is_udp; |
| 3228 | char buf[256], *r; |
| 3229 | const char *p; |
| 3230 | struct in_addr guest_addr; |
| 3231 | int host_port, guest_port; |
| 3232 | |
| 3233 | if (!slirp_inited) { |
| 3234 | slirp_inited = 1; |
| 3235 | slirp_init(); |
| 3236 | } |
| 3237 | |
| 3238 | p = redir_str; |
| 3239 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3240 | goto fail; |
| 3241 | if (!strcmp(buf, "tcp")) { |
| 3242 | is_udp = 0; |
| 3243 | } else if (!strcmp(buf, "udp")) { |
| 3244 | is_udp = 1; |
| 3245 | } else { |
| 3246 | goto fail; |
| 3247 | } |
| 3248 | |
| 3249 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3250 | goto fail; |
| 3251 | host_port = strtol(buf, &r, 0); |
| 3252 | if (r == buf) |
| 3253 | goto fail; |
| 3254 | |
| 3255 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3256 | goto fail; |
| 3257 | if (buf[0] == '\0') { |
| 3258 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 3259 | } |
| 3260 | if (!inet_aton(buf, &guest_addr)) |
| 3261 | goto fail; |
| 3262 | |
| 3263 | guest_port = strtol(p, &r, 0); |
| 3264 | if (r == p) |
| 3265 | goto fail; |
| 3266 | |
| 3267 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 3268 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 3269 | exit(1); |
| 3270 | } |
| 3271 | return; |
| 3272 | fail: |
| 3273 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 3274 | exit(1); |
| 3275 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3276 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3277 | #ifndef _WIN32 |
| 3278 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3279 | char smb_dir[1024]; |
| 3280 | |
| 3281 | static void smb_exit(void) |
| 3282 | { |
| 3283 | DIR *d; |
| 3284 | struct dirent *de; |
| 3285 | char filename[1024]; |
| 3286 | |
| 3287 | /* erase all the files in the directory */ |
| 3288 | d = opendir(smb_dir); |
| 3289 | for(;;) { |
| 3290 | de = readdir(d); |
| 3291 | if (!de) |
| 3292 | break; |
| 3293 | if (strcmp(de->d_name, ".") != 0 && |
| 3294 | strcmp(de->d_name, "..") != 0) { |
| 3295 | snprintf(filename, sizeof(filename), "%s/%s", |
| 3296 | smb_dir, de->d_name); |
| 3297 | unlink(filename); |
| 3298 | } |
| 3299 | } |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3300 | closedir(d); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3301 | rmdir(smb_dir); |
| 3302 | } |
| 3303 | |
| 3304 | /* automatic user mode samba server configuration */ |
| 3305 | void net_slirp_smb(const char *exported_dir) |
| 3306 | { |
| 3307 | char smb_conf[1024]; |
| 3308 | char smb_cmdline[1024]; |
| 3309 | FILE *f; |
| 3310 | |
| 3311 | if (!slirp_inited) { |
| 3312 | slirp_inited = 1; |
| 3313 | slirp_init(); |
| 3314 | } |
| 3315 | |
| 3316 | /* XXX: better tmp dir construction */ |
| 3317 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 3318 | if (mkdir(smb_dir, 0700) < 0) { |
| 3319 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 3320 | exit(1); |
| 3321 | } |
| 3322 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
| 3323 | |
| 3324 | f = fopen(smb_conf, "w"); |
| 3325 | if (!f) { |
| 3326 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 3327 | exit(1); |
| 3328 | } |
| 3329 | fprintf(f, |
| 3330 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3331 | "private dir=%s\n" |
| 3332 | "smb ports=0\n" |
| 3333 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3334 | "pid directory=%s\n" |
| 3335 | "lock directory=%s\n" |
| 3336 | "log file=%s/log.smbd\n" |
| 3337 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3338 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3339 | "[qemu]\n" |
| 3340 | "path=%s\n" |
| 3341 | "read only=no\n" |
| 3342 | "guest ok=yes\n", |
| 3343 | smb_dir, |
| 3344 | smb_dir, |
| 3345 | smb_dir, |
| 3346 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3347 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3348 | exported_dir |
| 3349 | ); |
| 3350 | fclose(f); |
| 3351 | atexit(smb_exit); |
| 3352 | |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 3353 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 3354 | SMBD_COMMAND, smb_conf); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3355 | |
| 3356 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 3357 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3358 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3359 | #endif /* !defined(_WIN32) */ |
| 3360 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3361 | #endif /* CONFIG_SLIRP */ |
| 3362 | |
| 3363 | #if !defined(_WIN32) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3364 | |
| 3365 | typedef struct TAPState { |
| 3366 | VLANClientState *vc; |
| 3367 | int fd; |
| 3368 | } TAPState; |
| 3369 | |
| 3370 | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
| 3371 | { |
| 3372 | TAPState *s = opaque; |
| 3373 | int ret; |
| 3374 | for(;;) { |
| 3375 | ret = write(s->fd, buf, size); |
| 3376 | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 3377 | } else { |
| 3378 | break; |
| 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | static void tap_send(void *opaque) |
| 3384 | { |
| 3385 | TAPState *s = opaque; |
| 3386 | uint8_t buf[4096]; |
| 3387 | int size; |
| 3388 | |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3389 | #ifdef __sun__ |
| 3390 | struct strbuf sbuf; |
| 3391 | int f = 0; |
| 3392 | sbuf.maxlen = sizeof(buf); |
| 3393 | sbuf.buf = buf; |
| 3394 | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
| 3395 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3396 | size = read(s->fd, buf, sizeof(buf)); |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3397 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3398 | if (size > 0) { |
| 3399 | qemu_send_packet(s->vc, buf, size); |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | /* fd support */ |
| 3404 | |
| 3405 | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
| 3406 | { |
| 3407 | TAPState *s; |
| 3408 | |
| 3409 | s = qemu_mallocz(sizeof(TAPState)); |
| 3410 | if (!s) |
| 3411 | return NULL; |
| 3412 | s->fd = fd; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3413 | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3414 | qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
| 3415 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
| 3416 | return s; |
| 3417 | } |
| 3418 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3419 | #ifdef _BSD |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3420 | static int tap_open(char *ifname, int ifname_size) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3421 | { |
| 3422 | int fd; |
| 3423 | char *dev; |
| 3424 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3425 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3426 | fd = open("/dev/tap", O_RDWR); |
| 3427 | if (fd < 0) { |
| 3428 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 3429 | return -1; |
| 3430 | } |
| 3431 | |
| 3432 | fstat(fd, &s); |
| 3433 | dev = devname(s.st_rdev, S_IFCHR); |
| 3434 | pstrcpy(ifname, ifname_size, dev); |
| 3435 | |
| 3436 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3437 | return fd; |
| 3438 | } |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3439 | #elif defined(__sun__) |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3440 | #define TUNNEWPPA (('T'<<16) | 0x0001) |
| 3441 | /* |
| 3442 | * Allocate TAP device, returns opened fd. |
| 3443 | * Stores dev name in the first arg(must be large enough). |
| 3444 | */ |
| 3445 | int tap_alloc(char *dev) |
| 3446 | { |
| 3447 | int tap_fd, if_fd, ppa = -1; |
| 3448 | static int ip_fd = 0; |
| 3449 | char *ptr; |
| 3450 | |
| 3451 | static int arp_fd = 0; |
| 3452 | int ip_muxid, arp_muxid; |
| 3453 | struct strioctl strioc_if, strioc_ppa; |
| 3454 | int link_type = I_PLINK;; |
| 3455 | struct lifreq ifr; |
| 3456 | char actual_name[32] = ""; |
| 3457 | |
| 3458 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3459 | |
| 3460 | if( *dev ){ |
| 3461 | ptr = dev; |
| 3462 | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
| 3463 | ppa = atoi(ptr); |
| 3464 | } |
| 3465 | |
| 3466 | /* Check if IP device was opened */ |
| 3467 | if( ip_fd ) |
| 3468 | close(ip_fd); |
| 3469 | |
| 3470 | if( (ip_fd = open("/dev/udp", O_RDWR, 0)) < 0){ |
| 3471 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); |
| 3472 | return -1; |
| 3473 | } |
| 3474 | |
| 3475 | if( (tap_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3476 | syslog(LOG_ERR, "Can't open /dev/tap"); |
| 3477 | return -1; |
| 3478 | } |
| 3479 | |
| 3480 | /* Assign a new PPA and get its unit number. */ |
| 3481 | strioc_ppa.ic_cmd = TUNNEWPPA; |
| 3482 | strioc_ppa.ic_timout = 0; |
| 3483 | strioc_ppa.ic_len = sizeof(ppa); |
| 3484 | strioc_ppa.ic_dp = (char *)&ppa; |
| 3485 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
| 3486 | syslog (LOG_ERR, "Can't assign new interface"); |
| 3487 | |
| 3488 | if( (if_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3489 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); |
| 3490 | return -1; |
| 3491 | } |
| 3492 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
| 3493 | syslog(LOG_ERR, "Can't push IP module"); |
| 3494 | return -1; |
| 3495 | } |
| 3496 | |
| 3497 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
| 3498 | syslog(LOG_ERR, "Can't get flags\n"); |
| 3499 | |
| 3500 | snprintf (actual_name, 32, "tap%d", ppa); |
| 3501 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3502 | |
| 3503 | ifr.lifr_ppa = ppa; |
| 3504 | /* Assign ppa according to the unit number returned by tun device */ |
| 3505 | |
| 3506 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
| 3507 | syslog (LOG_ERR, "Can't set PPA %d", ppa); |
| 3508 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
| 3509 | syslog (LOG_ERR, "Can't get flags\n"); |
| 3510 | /* Push arp module to if_fd */ |
| 3511 | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
| 3512 | syslog (LOG_ERR, "Can't push ARP module (2)"); |
| 3513 | |
| 3514 | /* Push arp module to ip_fd */ |
| 3515 | if (ioctl (ip_fd, I_POP, NULL) < 0) |
| 3516 | syslog (LOG_ERR, "I_POP failed\n"); |
| 3517 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
| 3518 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); |
| 3519 | /* Open arp_fd */ |
| 3520 | if ((arp_fd = open ("/dev/tap", O_RDWR, 0)) < 0) |
| 3521 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
| 3522 | |
| 3523 | /* Set ifname to arp */ |
| 3524 | strioc_if.ic_cmd = SIOCSLIFNAME; |
| 3525 | strioc_if.ic_timout = 0; |
| 3526 | strioc_if.ic_len = sizeof(ifr); |
| 3527 | strioc_if.ic_dp = (char *)𝔦 |
| 3528 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
| 3529 | syslog (LOG_ERR, "Can't set ifname to arp\n"); |
| 3530 | } |
| 3531 | |
| 3532 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
| 3533 | syslog(LOG_ERR, "Can't link TAP device to IP"); |
| 3534 | return -1; |
| 3535 | } |
| 3536 | |
| 3537 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
| 3538 | syslog (LOG_ERR, "Can't link TAP device to ARP"); |
| 3539 | |
| 3540 | close (if_fd); |
| 3541 | |
| 3542 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3543 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3544 | ifr.lifr_ip_muxid = ip_muxid; |
| 3545 | ifr.lifr_arp_muxid = arp_muxid; |
| 3546 | |
| 3547 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
| 3548 | { |
| 3549 | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
| 3550 | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
| 3551 | syslog (LOG_ERR, "Can't set multiplexor id"); |
| 3552 | } |
| 3553 | |
| 3554 | sprintf(dev, "tap%d", ppa); |
| 3555 | return tap_fd; |
| 3556 | } |
| 3557 | |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3558 | static int tap_open(char *ifname, int ifname_size) |
| 3559 | { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3560 | char dev[10]=""; |
| 3561 | int fd; |
| 3562 | if( (fd = tap_alloc(dev)) < 0 ){ |
| 3563 | fprintf(stderr, "Cannot allocate TAP device\n"); |
| 3564 | return -1; |
| 3565 | } |
| 3566 | pstrcpy(ifname, ifname_size, dev); |
| 3567 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3568 | return fd; |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3569 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3570 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3571 | static int tap_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3572 | { |
| 3573 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3574 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3575 | |
| 3576 | fd = open("/dev/net/tun", O_RDWR); |
| 3577 | if (fd < 0) { |
| 3578 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 3579 | return -1; |
| 3580 | } |
| 3581 | memset(&ifr, 0, sizeof(ifr)); |
| 3582 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3583 | if (ifname[0] != '\0') |
| 3584 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 3585 | else |
| 3586 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3587 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 3588 | if (ret != 0) { |
| 3589 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 3590 | close(fd); |
| 3591 | return -1; |
| 3592 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3593 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3594 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3595 | return fd; |
| 3596 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3597 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3598 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3599 | static int net_tap_init(VLANState *vlan, const char *ifname1, |
| 3600 | const char *setup_script) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3601 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3602 | TAPState *s; |
| 3603 | int pid, status, fd; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3604 | char *args[3]; |
| 3605 | char **parg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3606 | char ifname[128]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3607 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3608 | if (ifname1 != NULL) |
| 3609 | pstrcpy(ifname, sizeof(ifname), ifname1); |
| 3610 | else |
| 3611 | ifname[0] = '\0'; |
| 3612 | fd = tap_open(ifname, sizeof(ifname)); |
| 3613 | if (fd < 0) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3614 | return -1; |
| 3615 | |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 3616 | if (!setup_script || !strcmp(setup_script, "no")) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3617 | setup_script = ""; |
| 3618 | if (setup_script[0] != '\0') { |
| 3619 | /* try to launch network init script */ |
| 3620 | pid = fork(); |
| 3621 | if (pid >= 0) { |
| 3622 | if (pid == 0) { |
| 3623 | parg = args; |
| 3624 | *parg++ = (char *)setup_script; |
| 3625 | *parg++ = ifname; |
| 3626 | *parg++ = NULL; |
| 3627 | execv(setup_script, args); |
bellard | 4a38940 | 2005-11-26 20:10:07 +0000 | [diff] [blame] | 3628 | _exit(1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3629 | } |
| 3630 | while (waitpid(pid, &status, 0) != pid); |
| 3631 | if (!WIFEXITED(status) || |
| 3632 | WEXITSTATUS(status) != 0) { |
| 3633 | fprintf(stderr, "%s: could not launch network script\n", |
| 3634 | setup_script); |
| 3635 | return -1; |
| 3636 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3639 | s = net_tap_fd_init(vlan, fd); |
| 3640 | if (!s) |
| 3641 | return -1; |
| 3642 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3643 | "tap: ifname=%s setup_script=%s", ifname, setup_script); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3644 | return 0; |
| 3645 | } |
| 3646 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3647 | #endif /* !_WIN32 */ |
| 3648 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3649 | /* network connection */ |
| 3650 | typedef struct NetSocketState { |
| 3651 | VLANClientState *vc; |
| 3652 | int fd; |
| 3653 | int state; /* 0 = getting length, 1 = getting data */ |
| 3654 | int index; |
| 3655 | int packet_len; |
| 3656 | uint8_t buf[4096]; |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3657 | 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] | 3658 | } NetSocketState; |
| 3659 | |
| 3660 | typedef struct NetSocketListenState { |
| 3661 | VLANState *vlan; |
| 3662 | int fd; |
| 3663 | } NetSocketListenState; |
| 3664 | |
| 3665 | /* XXX: we consider we can send the whole packet without blocking */ |
| 3666 | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3667 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3668 | NetSocketState *s = opaque; |
| 3669 | uint32_t len; |
| 3670 | len = htonl(size); |
| 3671 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3672 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 3673 | send_all(s->fd, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3676 | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
| 3677 | { |
| 3678 | NetSocketState *s = opaque; |
| 3679 | sendto(s->fd, buf, size, 0, |
| 3680 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 3681 | } |
| 3682 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3683 | static void net_socket_send(void *opaque) |
| 3684 | { |
| 3685 | NetSocketState *s = opaque; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3686 | int l, size, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3687 | uint8_t buf1[4096]; |
| 3688 | const uint8_t *buf; |
| 3689 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3690 | size = recv(s->fd, buf1, sizeof(buf1), 0); |
| 3691 | if (size < 0) { |
| 3692 | err = socket_error(); |
| 3693 | if (err != EWOULDBLOCK) |
| 3694 | goto eoc; |
| 3695 | } else if (size == 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3696 | /* end of connection */ |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3697 | eoc: |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3698 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3699 | closesocket(s->fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3700 | return; |
| 3701 | } |
| 3702 | buf = buf1; |
| 3703 | while (size > 0) { |
| 3704 | /* reassemble a packet from the network */ |
| 3705 | switch(s->state) { |
| 3706 | case 0: |
| 3707 | l = 4 - s->index; |
| 3708 | if (l > size) |
| 3709 | l = size; |
| 3710 | memcpy(s->buf + s->index, buf, l); |
| 3711 | buf += l; |
| 3712 | size -= l; |
| 3713 | s->index += l; |
| 3714 | if (s->index == 4) { |
| 3715 | /* got length */ |
| 3716 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 3717 | s->index = 0; |
| 3718 | s->state = 1; |
| 3719 | } |
| 3720 | break; |
| 3721 | case 1: |
| 3722 | l = s->packet_len - s->index; |
| 3723 | if (l > size) |
| 3724 | l = size; |
| 3725 | memcpy(s->buf + s->index, buf, l); |
| 3726 | s->index += l; |
| 3727 | buf += l; |
| 3728 | size -= l; |
| 3729 | if (s->index >= s->packet_len) { |
| 3730 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 3731 | s->index = 0; |
| 3732 | s->state = 0; |
| 3733 | } |
| 3734 | break; |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3739 | static void net_socket_send_dgram(void *opaque) |
| 3740 | { |
| 3741 | NetSocketState *s = opaque; |
| 3742 | int size; |
| 3743 | |
| 3744 | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 3745 | if (size < 0) |
| 3746 | return; |
| 3747 | if (size == 0) { |
| 3748 | /* end of connection */ |
| 3749 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 3750 | return; |
| 3751 | } |
| 3752 | qemu_send_packet(s->vc, s->buf, size); |
| 3753 | } |
| 3754 | |
| 3755 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 3756 | { |
| 3757 | struct ip_mreq imr; |
| 3758 | int fd; |
| 3759 | int val, ret; |
| 3760 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 3761 | 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] | 3762 | inet_ntoa(mcastaddr->sin_addr), |
| 3763 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3764 | return -1; |
| 3765 | |
| 3766 | } |
| 3767 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 3768 | if (fd < 0) { |
| 3769 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 3770 | return -1; |
| 3771 | } |
| 3772 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3773 | val = 1; |
| 3774 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 3775 | (const char *)&val, sizeof(val)); |
| 3776 | if (ret < 0) { |
| 3777 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 3778 | goto fail; |
| 3779 | } |
| 3780 | |
| 3781 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 3782 | if (ret < 0) { |
| 3783 | perror("bind"); |
| 3784 | goto fail; |
| 3785 | } |
| 3786 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3787 | /* Add host to multicast group */ |
| 3788 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 3789 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 3790 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3791 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 3792 | (const char *)&imr, sizeof(struct ip_mreq)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3793 | if (ret < 0) { |
| 3794 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 3795 | goto fail; |
| 3796 | } |
| 3797 | |
| 3798 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 3799 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3800 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 3801 | (const char *)&val, sizeof(val)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3802 | if (ret < 0) { |
| 3803 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 3804 | goto fail; |
| 3805 | } |
| 3806 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3807 | socket_set_nonblock(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3808 | return fd; |
| 3809 | fail: |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3810 | if (fd >= 0) |
| 3811 | closesocket(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3812 | return -1; |
| 3813 | } |
| 3814 | |
| 3815 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
| 3816 | int is_connected) |
| 3817 | { |
| 3818 | struct sockaddr_in saddr; |
| 3819 | int newfd; |
| 3820 | socklen_t saddr_len; |
| 3821 | NetSocketState *s; |
| 3822 | |
| 3823 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 3824 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 3825 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 3826 | */ |
| 3827 | |
| 3828 | if (is_connected) { |
| 3829 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 3830 | /* must be bound */ |
| 3831 | if (saddr.sin_addr.s_addr==0) { |
| 3832 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 3833 | fd); |
| 3834 | return NULL; |
| 3835 | } |
| 3836 | /* clone dgram socket */ |
| 3837 | newfd = net_socket_mcast_create(&saddr); |
| 3838 | if (newfd < 0) { |
| 3839 | /* error already reported by net_socket_mcast_create() */ |
| 3840 | close(fd); |
| 3841 | return NULL; |
| 3842 | } |
| 3843 | /* clone newfd to fd, close newfd */ |
| 3844 | dup2(newfd, fd); |
| 3845 | close(newfd); |
| 3846 | |
| 3847 | } else { |
| 3848 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 3849 | fd, strerror(errno)); |
| 3850 | return NULL; |
| 3851 | } |
| 3852 | } |
| 3853 | |
| 3854 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3855 | if (!s) |
| 3856 | return NULL; |
| 3857 | s->fd = fd; |
| 3858 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3859 | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3860 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 3861 | |
| 3862 | /* mcast: save bound address as dst */ |
| 3863 | if (is_connected) s->dgram_dst=saddr; |
| 3864 | |
| 3865 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3866 | "socket: fd=%d (%s mcast=%s:%d)", |
| 3867 | fd, is_connected? "cloned" : "", |
| 3868 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3869 | return s; |
| 3870 | } |
| 3871 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3872 | static void net_socket_connect(void *opaque) |
| 3873 | { |
| 3874 | NetSocketState *s = opaque; |
| 3875 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 3876 | } |
| 3877 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3878 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3879 | int is_connected) |
| 3880 | { |
| 3881 | NetSocketState *s; |
| 3882 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3883 | if (!s) |
| 3884 | return NULL; |
| 3885 | s->fd = fd; |
| 3886 | s->vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3887 | net_socket_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3888 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3889 | "socket: fd=%d", fd); |
| 3890 | if (is_connected) { |
| 3891 | net_socket_connect(s); |
| 3892 | } else { |
| 3893 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 3894 | } |
| 3895 | return s; |
| 3896 | } |
| 3897 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3898 | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
| 3899 | int is_connected) |
| 3900 | { |
| 3901 | int so_type=-1, optlen=sizeof(so_type); |
| 3902 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3903 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) { |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3904 | fprintf(stderr, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 3905 | return NULL; |
| 3906 | } |
| 3907 | switch(so_type) { |
| 3908 | case SOCK_DGRAM: |
| 3909 | return net_socket_fd_init_dgram(vlan, fd, is_connected); |
| 3910 | case SOCK_STREAM: |
| 3911 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3912 | default: |
| 3913 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 3914 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
| 3915 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3916 | } |
| 3917 | return NULL; |
| 3918 | } |
| 3919 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3920 | static void net_socket_accept(void *opaque) |
| 3921 | { |
| 3922 | NetSocketListenState *s = opaque; |
| 3923 | NetSocketState *s1; |
| 3924 | struct sockaddr_in saddr; |
| 3925 | socklen_t len; |
| 3926 | int fd; |
| 3927 | |
| 3928 | for(;;) { |
| 3929 | len = sizeof(saddr); |
| 3930 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 3931 | if (fd < 0 && errno != EINTR) { |
| 3932 | return; |
| 3933 | } else if (fd >= 0) { |
| 3934 | break; |
| 3935 | } |
| 3936 | } |
| 3937 | s1 = net_socket_fd_init(s->vlan, fd, 1); |
| 3938 | if (!s1) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3939 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3940 | } else { |
| 3941 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 3942 | "socket: connection from %s:%d", |
| 3943 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
| 3948 | { |
| 3949 | NetSocketListenState *s; |
| 3950 | int fd, val, ret; |
| 3951 | struct sockaddr_in saddr; |
| 3952 | |
| 3953 | if (parse_host_port(&saddr, host_str) < 0) |
| 3954 | return -1; |
| 3955 | |
| 3956 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
| 3957 | if (!s) |
| 3958 | return -1; |
| 3959 | |
| 3960 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3961 | if (fd < 0) { |
| 3962 | perror("socket"); |
| 3963 | return -1; |
| 3964 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3965 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3966 | |
| 3967 | /* allow fast reuse */ |
| 3968 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3969 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3970 | |
| 3971 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 3972 | if (ret < 0) { |
| 3973 | perror("bind"); |
| 3974 | return -1; |
| 3975 | } |
| 3976 | ret = listen(fd, 0); |
| 3977 | if (ret < 0) { |
| 3978 | perror("listen"); |
| 3979 | return -1; |
| 3980 | } |
| 3981 | s->vlan = vlan; |
| 3982 | s->fd = fd; |
| 3983 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 3984 | return 0; |
| 3985 | } |
| 3986 | |
| 3987 | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
| 3988 | { |
| 3989 | NetSocketState *s; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3990 | int fd, connected, ret, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3991 | struct sockaddr_in saddr; |
| 3992 | |
| 3993 | if (parse_host_port(&saddr, host_str) < 0) |
| 3994 | return -1; |
| 3995 | |
| 3996 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3997 | if (fd < 0) { |
| 3998 | perror("socket"); |
| 3999 | return -1; |
| 4000 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4001 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4002 | |
| 4003 | connected = 0; |
| 4004 | for(;;) { |
| 4005 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 4006 | if (ret < 0) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4007 | err = socket_error(); |
| 4008 | if (err == EINTR || err == EWOULDBLOCK) { |
| 4009 | } else if (err == EINPROGRESS) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | } else { |
| 4012 | perror("connect"); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4013 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4014 | return -1; |
| 4015 | } |
| 4016 | } else { |
| 4017 | connected = 1; |
| 4018 | break; |
| 4019 | } |
| 4020 | } |
| 4021 | s = net_socket_fd_init(vlan, fd, connected); |
| 4022 | if (!s) |
| 4023 | return -1; |
| 4024 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4025 | "socket: connect to %s:%d", |
| 4026 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4027 | return 0; |
| 4028 | } |
| 4029 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4030 | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
| 4031 | { |
| 4032 | NetSocketState *s; |
| 4033 | int fd; |
| 4034 | struct sockaddr_in saddr; |
| 4035 | |
| 4036 | if (parse_host_port(&saddr, host_str) < 0) |
| 4037 | return -1; |
| 4038 | |
| 4039 | |
| 4040 | fd = net_socket_mcast_create(&saddr); |
| 4041 | if (fd < 0) |
| 4042 | return -1; |
| 4043 | |
| 4044 | s = net_socket_fd_init(vlan, fd, 0); |
| 4045 | if (!s) |
| 4046 | return -1; |
| 4047 | |
| 4048 | s->dgram_dst = saddr; |
| 4049 | |
| 4050 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4051 | "socket: mcast=%s:%d", |
| 4052 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4053 | return 0; |
| 4054 | |
| 4055 | } |
| 4056 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4057 | static int get_param_value(char *buf, int buf_size, |
| 4058 | const char *tag, const char *str) |
| 4059 | { |
| 4060 | const char *p; |
| 4061 | char *q; |
| 4062 | char option[128]; |
| 4063 | |
| 4064 | p = str; |
| 4065 | for(;;) { |
| 4066 | q = option; |
| 4067 | while (*p != '\0' && *p != '=') { |
| 4068 | if ((q - option) < sizeof(option) - 1) |
| 4069 | *q++ = *p; |
| 4070 | p++; |
| 4071 | } |
| 4072 | *q = '\0'; |
| 4073 | if (*p != '=') |
| 4074 | break; |
| 4075 | p++; |
| 4076 | if (!strcmp(tag, option)) { |
| 4077 | q = buf; |
| 4078 | while (*p != '\0' && *p != ',') { |
| 4079 | if ((q - buf) < buf_size - 1) |
| 4080 | *q++ = *p; |
| 4081 | p++; |
| 4082 | } |
| 4083 | *q = '\0'; |
| 4084 | return q - buf; |
| 4085 | } else { |
| 4086 | while (*p != '\0' && *p != ',') { |
| 4087 | p++; |
| 4088 | } |
| 4089 | } |
| 4090 | if (*p != ',') |
| 4091 | break; |
| 4092 | p++; |
| 4093 | } |
| 4094 | return 0; |
| 4095 | } |
| 4096 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 4097 | static int net_client_init(const char *str) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4098 | { |
| 4099 | const char *p; |
| 4100 | char *q; |
| 4101 | char device[64]; |
| 4102 | char buf[1024]; |
| 4103 | int vlan_id, ret; |
| 4104 | VLANState *vlan; |
| 4105 | |
| 4106 | p = str; |
| 4107 | q = device; |
| 4108 | while (*p != '\0' && *p != ',') { |
| 4109 | if ((q - device) < sizeof(device) - 1) |
| 4110 | *q++ = *p; |
| 4111 | p++; |
| 4112 | } |
| 4113 | *q = '\0'; |
| 4114 | if (*p == ',') |
| 4115 | p++; |
| 4116 | vlan_id = 0; |
| 4117 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
| 4118 | vlan_id = strtol(buf, NULL, 0); |
| 4119 | } |
| 4120 | vlan = qemu_find_vlan(vlan_id); |
| 4121 | if (!vlan) { |
| 4122 | fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
| 4123 | return -1; |
| 4124 | } |
| 4125 | if (!strcmp(device, "nic")) { |
| 4126 | NICInfo *nd; |
| 4127 | uint8_t *macaddr; |
| 4128 | |
| 4129 | if (nb_nics >= MAX_NICS) { |
| 4130 | fprintf(stderr, "Too Many NICs\n"); |
| 4131 | return -1; |
| 4132 | } |
| 4133 | nd = &nd_table[nb_nics]; |
| 4134 | macaddr = nd->macaddr; |
| 4135 | macaddr[0] = 0x52; |
| 4136 | macaddr[1] = 0x54; |
| 4137 | macaddr[2] = 0x00; |
| 4138 | macaddr[3] = 0x12; |
| 4139 | macaddr[4] = 0x34; |
| 4140 | macaddr[5] = 0x56 + nb_nics; |
| 4141 | |
| 4142 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
| 4143 | if (parse_macaddr(macaddr, buf) < 0) { |
| 4144 | fprintf(stderr, "invalid syntax for ethernet address\n"); |
| 4145 | return -1; |
| 4146 | } |
| 4147 | } |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 4148 | if (get_param_value(buf, sizeof(buf), "model", p)) { |
| 4149 | nd->model = strdup(buf); |
| 4150 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4151 | nd->vlan = vlan; |
| 4152 | nb_nics++; |
| 4153 | ret = 0; |
| 4154 | } else |
| 4155 | if (!strcmp(device, "none")) { |
| 4156 | /* does nothing. It is needed to signal that no network cards |
| 4157 | are wanted */ |
| 4158 | ret = 0; |
| 4159 | } else |
| 4160 | #ifdef CONFIG_SLIRP |
| 4161 | if (!strcmp(device, "user")) { |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4162 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
bellard | 3f423c9 | 2006-04-30 21:34:15 +0000 | [diff] [blame] | 4163 | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4164 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4165 | ret = net_slirp_init(vlan); |
| 4166 | } else |
| 4167 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 4168 | #ifdef _WIN32 |
| 4169 | if (!strcmp(device, "tap")) { |
| 4170 | char ifname[64]; |
| 4171 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4172 | fprintf(stderr, "tap: no interface name\n"); |
| 4173 | return -1; |
| 4174 | } |
| 4175 | ret = tap_win32_init(vlan, ifname); |
| 4176 | } else |
| 4177 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4178 | if (!strcmp(device, "tap")) { |
| 4179 | char ifname[64]; |
| 4180 | char setup_script[1024]; |
| 4181 | int fd; |
| 4182 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4183 | fd = strtol(buf, NULL, 0); |
| 4184 | ret = -1; |
| 4185 | if (net_tap_fd_init(vlan, fd)) |
| 4186 | ret = 0; |
| 4187 | } else { |
bellard | bf8c534 | 2007-01-09 19:44:41 +0000 | [diff] [blame] | 4188 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4189 | ifname[0] = '\0'; |
| 4190 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4191 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
| 4192 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
| 4193 | } |
| 4194 | ret = net_tap_init(vlan, ifname, setup_script); |
| 4195 | } |
| 4196 | } else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4197 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4198 | if (!strcmp(device, "socket")) { |
| 4199 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4200 | int fd; |
| 4201 | fd = strtol(buf, NULL, 0); |
| 4202 | ret = -1; |
| 4203 | if (net_socket_fd_init(vlan, fd, 1)) |
| 4204 | ret = 0; |
| 4205 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
| 4206 | ret = net_socket_listen_init(vlan, buf); |
| 4207 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
| 4208 | ret = net_socket_connect_init(vlan, buf); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4209 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
| 4210 | ret = net_socket_mcast_init(vlan, buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4211 | } else { |
| 4212 | fprintf(stderr, "Unknown socket options: %s\n", p); |
| 4213 | return -1; |
| 4214 | } |
| 4215 | } else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4216 | { |
| 4217 | fprintf(stderr, "Unknown network device: %s\n", device); |
| 4218 | return -1; |
| 4219 | } |
| 4220 | if (ret < 0) { |
| 4221 | fprintf(stderr, "Could not initialize device '%s'\n", device); |
| 4222 | } |
| 4223 | |
| 4224 | return ret; |
| 4225 | } |
| 4226 | |
| 4227 | void do_info_network(void) |
| 4228 | { |
| 4229 | VLANState *vlan; |
| 4230 | VLANClientState *vc; |
| 4231 | |
| 4232 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 4233 | term_printf("VLAN %d devices:\n", vlan->id); |
| 4234 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
| 4235 | term_printf(" %s\n", vc->info_str); |
| 4236 | } |
| 4237 | } |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 4238 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4239 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4240 | /* USB devices */ |
| 4241 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4242 | static USBPort *used_usb_ports; |
| 4243 | static USBPort *free_usb_ports; |
| 4244 | |
| 4245 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 4246 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 4247 | usb_attachfn attach) |
| 4248 | { |
| 4249 | port->opaque = opaque; |
| 4250 | port->index = index; |
| 4251 | port->attach = attach; |
| 4252 | port->next = free_usb_ports; |
| 4253 | free_usb_ports = port; |
| 4254 | } |
| 4255 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4256 | static int usb_device_add(const char *devname) |
| 4257 | { |
| 4258 | const char *p; |
| 4259 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4260 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4261 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4262 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4263 | return -1; |
| 4264 | |
| 4265 | if (strstart(devname, "host:", &p)) { |
| 4266 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4267 | } else if (!strcmp(devname, "mouse")) { |
| 4268 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 4269 | } else if (!strcmp(devname, "tablet")) { |
| 4270 | dev = usb_tablet_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 4271 | } else if (strstart(devname, "disk:", &p)) { |
| 4272 | dev = usb_msd_init(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4273 | } else { |
| 4274 | return -1; |
| 4275 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4276 | if (!dev) |
| 4277 | return -1; |
| 4278 | |
| 4279 | /* Find a USB port to add the device to. */ |
| 4280 | port = free_usb_ports; |
| 4281 | if (!port->next) { |
| 4282 | USBDevice *hub; |
| 4283 | |
| 4284 | /* Create a new hub and chain it on. */ |
| 4285 | free_usb_ports = NULL; |
| 4286 | port->next = used_usb_ports; |
| 4287 | used_usb_ports = port; |
| 4288 | |
| 4289 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 4290 | usb_attach(port, hub); |
| 4291 | port = free_usb_ports; |
| 4292 | } |
| 4293 | |
| 4294 | free_usb_ports = port->next; |
| 4295 | port->next = used_usb_ports; |
| 4296 | used_usb_ports = port; |
| 4297 | usb_attach(port, dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | static int usb_device_del(const char *devname) |
| 4302 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4303 | USBPort *port; |
| 4304 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4305 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4306 | int bus_num, addr; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4307 | const char *p; |
| 4308 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4309 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4310 | return -1; |
| 4311 | |
| 4312 | p = strchr(devname, '.'); |
| 4313 | if (!p) |
| 4314 | return -1; |
| 4315 | bus_num = strtoul(devname, NULL, 0); |
| 4316 | addr = strtoul(p + 1, NULL, 0); |
| 4317 | if (bus_num != 0) |
| 4318 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4319 | |
| 4320 | lastp = &used_usb_ports; |
| 4321 | port = used_usb_ports; |
| 4322 | while (port && port->dev->addr != addr) { |
| 4323 | lastp = &port->next; |
| 4324 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4325 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4326 | |
| 4327 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4328 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4329 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4330 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4331 | *lastp = port->next; |
| 4332 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4333 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4334 | port->next = free_usb_ports; |
| 4335 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4336 | return 0; |
| 4337 | } |
| 4338 | |
| 4339 | void do_usb_add(const char *devname) |
| 4340 | { |
| 4341 | int ret; |
| 4342 | ret = usb_device_add(devname); |
| 4343 | if (ret < 0) |
| 4344 | term_printf("Could not add USB device '%s'\n", devname); |
| 4345 | } |
| 4346 | |
| 4347 | void do_usb_del(const char *devname) |
| 4348 | { |
| 4349 | int ret; |
| 4350 | ret = usb_device_del(devname); |
| 4351 | if (ret < 0) |
| 4352 | term_printf("Could not remove USB device '%s'\n", devname); |
| 4353 | } |
| 4354 | |
| 4355 | void usb_info(void) |
| 4356 | { |
| 4357 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4358 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4359 | const char *speed_str; |
| 4360 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4361 | if (!usb_enabled) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4362 | term_printf("USB support not enabled\n"); |
| 4363 | return; |
| 4364 | } |
| 4365 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4366 | for (port = used_usb_ports; port; port = port->next) { |
| 4367 | dev = port->dev; |
| 4368 | if (!dev) |
| 4369 | continue; |
| 4370 | switch(dev->speed) { |
| 4371 | case USB_SPEED_LOW: |
| 4372 | speed_str = "1.5"; |
| 4373 | break; |
| 4374 | case USB_SPEED_FULL: |
| 4375 | speed_str = "12"; |
| 4376 | break; |
| 4377 | case USB_SPEED_HIGH: |
| 4378 | speed_str = "480"; |
| 4379 | break; |
| 4380 | default: |
| 4381 | speed_str = "?"; |
| 4382 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4383 | } |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 4384 | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 4385 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | /***********************************************************/ |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 4390 | /* pid file */ |
| 4391 | |
| 4392 | static char *pid_filename; |
| 4393 | |
| 4394 | /* Remove PID file. Called on normal exit */ |
| 4395 | |
| 4396 | static void remove_pidfile(void) |
| 4397 | { |
| 4398 | unlink (pid_filename); |
| 4399 | } |
| 4400 | |
| 4401 | static void create_pidfile(const char *filename) |
| 4402 | { |
| 4403 | struct stat pidstat; |
| 4404 | FILE *f; |
| 4405 | |
| 4406 | /* Try to write our PID to the named file */ |
| 4407 | if (stat(filename, &pidstat) < 0) { |
| 4408 | if (errno == ENOENT) { |
| 4409 | if ((f = fopen (filename, "w")) == NULL) { |
| 4410 | perror("Opening pidfile"); |
| 4411 | exit(1); |
| 4412 | } |
| 4413 | fprintf(f, "%d\n", getpid()); |
| 4414 | fclose(f); |
| 4415 | pid_filename = qemu_strdup(filename); |
| 4416 | if (!pid_filename) { |
| 4417 | fprintf(stderr, "Could not save PID filename"); |
| 4418 | exit(1); |
| 4419 | } |
| 4420 | atexit(remove_pidfile); |
| 4421 | } |
| 4422 | } else { |
| 4423 | fprintf(stderr, "%s already exists. Remove it and try again.\n", |
| 4424 | filename); |
| 4425 | exit(1); |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4430 | /* dumb display */ |
| 4431 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4432 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 4433 | { |
| 4434 | } |
| 4435 | |
| 4436 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 4437 | { |
| 4438 | } |
| 4439 | |
| 4440 | static void dumb_refresh(DisplayState *ds) |
| 4441 | { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 4442 | vga_hw_update(); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | void dumb_display_init(DisplayState *ds) |
| 4446 | { |
| 4447 | ds->data = NULL; |
| 4448 | ds->linesize = 0; |
| 4449 | ds->depth = 0; |
| 4450 | ds->dpy_update = dumb_update; |
| 4451 | ds->dpy_resize = dumb_resize; |
| 4452 | ds->dpy_refresh = dumb_refresh; |
| 4453 | } |
| 4454 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4455 | /***********************************************************/ |
| 4456 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4457 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4458 | #define MAX_IO_HANDLERS 64 |
| 4459 | |
| 4460 | typedef struct IOHandlerRecord { |
| 4461 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4462 | IOCanRWHandler *fd_read_poll; |
| 4463 | IOHandler *fd_read; |
| 4464 | IOHandler *fd_write; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4465 | void *opaque; |
| 4466 | /* temporary data */ |
| 4467 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4468 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4469 | } IOHandlerRecord; |
| 4470 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4471 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4472 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4473 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 4474 | necessary in the character devices to suppress fd_can_read(). */ |
| 4475 | int qemu_set_fd_handler2(int fd, |
| 4476 | IOCanRWHandler *fd_read_poll, |
| 4477 | IOHandler *fd_read, |
| 4478 | IOHandler *fd_write, |
| 4479 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4480 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4481 | IOHandlerRecord **pioh, *ioh; |
| 4482 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4483 | if (!fd_read && !fd_write) { |
| 4484 | pioh = &first_io_handler; |
| 4485 | for(;;) { |
| 4486 | ioh = *pioh; |
| 4487 | if (ioh == NULL) |
| 4488 | break; |
| 4489 | if (ioh->fd == fd) { |
| 4490 | *pioh = ioh->next; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4491 | qemu_free(ioh); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4492 | break; |
| 4493 | } |
| 4494 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4495 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4496 | } else { |
| 4497 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 4498 | if (ioh->fd == fd) |
| 4499 | goto found; |
| 4500 | } |
| 4501 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 4502 | if (!ioh) |
| 4503 | return -1; |
| 4504 | ioh->next = first_io_handler; |
| 4505 | first_io_handler = ioh; |
| 4506 | found: |
| 4507 | ioh->fd = fd; |
| 4508 | ioh->fd_read_poll = fd_read_poll; |
| 4509 | ioh->fd_read = fd_read; |
| 4510 | ioh->fd_write = fd_write; |
| 4511 | ioh->opaque = opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4512 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4513 | return 0; |
| 4514 | } |
| 4515 | |
| 4516 | int qemu_set_fd_handler(int fd, |
| 4517 | IOHandler *fd_read, |
| 4518 | IOHandler *fd_write, |
| 4519 | void *opaque) |
| 4520 | { |
| 4521 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4524 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4525 | /* Polling handling */ |
| 4526 | |
| 4527 | typedef struct PollingEntry { |
| 4528 | PollingFunc *func; |
| 4529 | void *opaque; |
| 4530 | struct PollingEntry *next; |
| 4531 | } PollingEntry; |
| 4532 | |
| 4533 | static PollingEntry *first_polling_entry; |
| 4534 | |
| 4535 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 4536 | { |
| 4537 | PollingEntry **ppe, *pe; |
| 4538 | pe = qemu_mallocz(sizeof(PollingEntry)); |
| 4539 | if (!pe) |
| 4540 | return -1; |
| 4541 | pe->func = func; |
| 4542 | pe->opaque = opaque; |
| 4543 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 4544 | *ppe = pe; |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
| 4548 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 4549 | { |
| 4550 | PollingEntry **ppe, *pe; |
| 4551 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 4552 | pe = *ppe; |
| 4553 | if (pe->func == func && pe->opaque == opaque) { |
| 4554 | *ppe = pe->next; |
| 4555 | qemu_free(pe); |
| 4556 | break; |
| 4557 | } |
| 4558 | } |
| 4559 | } |
| 4560 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4561 | #ifdef _WIN32 |
| 4562 | /***********************************************************/ |
| 4563 | /* Wait objects support */ |
| 4564 | typedef struct WaitObjects { |
| 4565 | int num; |
| 4566 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4567 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4568 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4569 | } WaitObjects; |
| 4570 | |
| 4571 | static WaitObjects wait_objects = {0}; |
| 4572 | |
| 4573 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4574 | { |
| 4575 | WaitObjects *w = &wait_objects; |
| 4576 | |
| 4577 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 4578 | return -1; |
| 4579 | w->events[w->num] = handle; |
| 4580 | w->func[w->num] = func; |
| 4581 | w->opaque[w->num] = opaque; |
| 4582 | w->num++; |
| 4583 | return 0; |
| 4584 | } |
| 4585 | |
| 4586 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4587 | { |
| 4588 | int i, found; |
| 4589 | WaitObjects *w = &wait_objects; |
| 4590 | |
| 4591 | found = 0; |
| 4592 | for (i = 0; i < w->num; i++) { |
| 4593 | if (w->events[i] == handle) |
| 4594 | found = 1; |
| 4595 | if (found) { |
| 4596 | w->events[i] = w->events[i + 1]; |
| 4597 | w->func[i] = w->func[i + 1]; |
| 4598 | w->opaque[i] = w->opaque[i + 1]; |
| 4599 | } |
| 4600 | } |
| 4601 | if (found) |
| 4602 | w->num--; |
| 4603 | } |
| 4604 | #endif |
| 4605 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4606 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4607 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4608 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4609 | #define IO_BUF_SIZE 32768 |
| 4610 | |
| 4611 | struct QEMUFile { |
| 4612 | FILE *outfile; |
| 4613 | BlockDriverState *bs; |
| 4614 | int is_file; |
| 4615 | int is_writable; |
| 4616 | int64_t base_offset; |
| 4617 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 4618 | when reading */ |
| 4619 | int buf_index; |
| 4620 | int buf_size; /* 0 when writing */ |
| 4621 | uint8_t buf[IO_BUF_SIZE]; |
| 4622 | }; |
| 4623 | |
| 4624 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 4625 | { |
| 4626 | QEMUFile *f; |
| 4627 | |
| 4628 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4629 | if (!f) |
| 4630 | return NULL; |
| 4631 | if (!strcmp(mode, "wb")) { |
| 4632 | f->is_writable = 1; |
| 4633 | } else if (!strcmp(mode, "rb")) { |
| 4634 | f->is_writable = 0; |
| 4635 | } else { |
| 4636 | goto fail; |
| 4637 | } |
| 4638 | f->outfile = fopen(filename, mode); |
| 4639 | if (!f->outfile) |
| 4640 | goto fail; |
| 4641 | f->is_file = 1; |
| 4642 | return f; |
| 4643 | fail: |
| 4644 | if (f->outfile) |
| 4645 | fclose(f->outfile); |
| 4646 | qemu_free(f); |
| 4647 | return NULL; |
| 4648 | } |
| 4649 | |
| 4650 | QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
| 4651 | { |
| 4652 | QEMUFile *f; |
| 4653 | |
| 4654 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4655 | if (!f) |
| 4656 | return NULL; |
| 4657 | f->is_file = 0; |
| 4658 | f->bs = bs; |
| 4659 | f->is_writable = is_writable; |
| 4660 | f->base_offset = offset; |
| 4661 | return f; |
| 4662 | } |
| 4663 | |
| 4664 | void qemu_fflush(QEMUFile *f) |
| 4665 | { |
| 4666 | if (!f->is_writable) |
| 4667 | return; |
| 4668 | if (f->buf_index > 0) { |
| 4669 | if (f->is_file) { |
| 4670 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4671 | fwrite(f->buf, 1, f->buf_index, f->outfile); |
| 4672 | } else { |
| 4673 | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
| 4674 | f->buf, f->buf_index); |
| 4675 | } |
| 4676 | f->buf_offset += f->buf_index; |
| 4677 | f->buf_index = 0; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | static void qemu_fill_buffer(QEMUFile *f) |
| 4682 | { |
| 4683 | int len; |
| 4684 | |
| 4685 | if (f->is_writable) |
| 4686 | return; |
| 4687 | if (f->is_file) { |
| 4688 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4689 | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile); |
| 4690 | if (len < 0) |
| 4691 | len = 0; |
| 4692 | } else { |
| 4693 | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
| 4694 | f->buf, IO_BUF_SIZE); |
| 4695 | if (len < 0) |
| 4696 | len = 0; |
| 4697 | } |
| 4698 | f->buf_index = 0; |
| 4699 | f->buf_size = len; |
| 4700 | f->buf_offset += len; |
| 4701 | } |
| 4702 | |
| 4703 | void qemu_fclose(QEMUFile *f) |
| 4704 | { |
| 4705 | if (f->is_writable) |
| 4706 | qemu_fflush(f); |
| 4707 | if (f->is_file) { |
| 4708 | fclose(f->outfile); |
| 4709 | } |
| 4710 | qemu_free(f); |
| 4711 | } |
| 4712 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4713 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 4714 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4715 | int l; |
| 4716 | while (size > 0) { |
| 4717 | l = IO_BUF_SIZE - f->buf_index; |
| 4718 | if (l > size) |
| 4719 | l = size; |
| 4720 | memcpy(f->buf + f->buf_index, buf, l); |
| 4721 | f->buf_index += l; |
| 4722 | buf += l; |
| 4723 | size -= l; |
| 4724 | if (f->buf_index >= IO_BUF_SIZE) |
| 4725 | qemu_fflush(f); |
| 4726 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | void qemu_put_byte(QEMUFile *f, int v) |
| 4730 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4731 | f->buf[f->buf_index++] = v; |
| 4732 | if (f->buf_index >= IO_BUF_SIZE) |
| 4733 | qemu_fflush(f); |
| 4734 | } |
| 4735 | |
| 4736 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 4737 | { |
| 4738 | int size, l; |
| 4739 | |
| 4740 | size = size1; |
| 4741 | while (size > 0) { |
| 4742 | l = f->buf_size - f->buf_index; |
| 4743 | if (l == 0) { |
| 4744 | qemu_fill_buffer(f); |
| 4745 | l = f->buf_size - f->buf_index; |
| 4746 | if (l == 0) |
| 4747 | break; |
| 4748 | } |
| 4749 | if (l > size) |
| 4750 | l = size; |
| 4751 | memcpy(buf, f->buf + f->buf_index, l); |
| 4752 | f->buf_index += l; |
| 4753 | buf += l; |
| 4754 | size -= l; |
| 4755 | } |
| 4756 | return size1 - size; |
| 4757 | } |
| 4758 | |
| 4759 | int qemu_get_byte(QEMUFile *f) |
| 4760 | { |
| 4761 | if (f->buf_index >= f->buf_size) { |
| 4762 | qemu_fill_buffer(f); |
| 4763 | if (f->buf_index >= f->buf_size) |
| 4764 | return 0; |
| 4765 | } |
| 4766 | return f->buf[f->buf_index++]; |
| 4767 | } |
| 4768 | |
| 4769 | int64_t qemu_ftell(QEMUFile *f) |
| 4770 | { |
| 4771 | return f->buf_offset - f->buf_size + f->buf_index; |
| 4772 | } |
| 4773 | |
| 4774 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 4775 | { |
| 4776 | if (whence == SEEK_SET) { |
| 4777 | /* nothing to do */ |
| 4778 | } else if (whence == SEEK_CUR) { |
| 4779 | pos += qemu_ftell(f); |
| 4780 | } else { |
| 4781 | /* SEEK_END not supported */ |
| 4782 | return -1; |
| 4783 | } |
| 4784 | if (f->is_writable) { |
| 4785 | qemu_fflush(f); |
| 4786 | f->buf_offset = pos; |
| 4787 | } else { |
| 4788 | f->buf_offset = pos; |
| 4789 | f->buf_index = 0; |
| 4790 | f->buf_size = 0; |
| 4791 | } |
| 4792 | return pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 4796 | { |
| 4797 | qemu_put_byte(f, v >> 8); |
| 4798 | qemu_put_byte(f, v); |
| 4799 | } |
| 4800 | |
| 4801 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 4802 | { |
| 4803 | qemu_put_byte(f, v >> 24); |
| 4804 | qemu_put_byte(f, v >> 16); |
| 4805 | qemu_put_byte(f, v >> 8); |
| 4806 | qemu_put_byte(f, v); |
| 4807 | } |
| 4808 | |
| 4809 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 4810 | { |
| 4811 | qemu_put_be32(f, v >> 32); |
| 4812 | qemu_put_be32(f, v); |
| 4813 | } |
| 4814 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4815 | unsigned int qemu_get_be16(QEMUFile *f) |
| 4816 | { |
| 4817 | unsigned int v; |
| 4818 | v = qemu_get_byte(f) << 8; |
| 4819 | v |= qemu_get_byte(f); |
| 4820 | return v; |
| 4821 | } |
| 4822 | |
| 4823 | unsigned int qemu_get_be32(QEMUFile *f) |
| 4824 | { |
| 4825 | unsigned int v; |
| 4826 | v = qemu_get_byte(f) << 24; |
| 4827 | v |= qemu_get_byte(f) << 16; |
| 4828 | v |= qemu_get_byte(f) << 8; |
| 4829 | v |= qemu_get_byte(f); |
| 4830 | return v; |
| 4831 | } |
| 4832 | |
| 4833 | uint64_t qemu_get_be64(QEMUFile *f) |
| 4834 | { |
| 4835 | uint64_t v; |
| 4836 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 4837 | v |= qemu_get_be32(f); |
| 4838 | return v; |
| 4839 | } |
| 4840 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4841 | typedef struct SaveStateEntry { |
| 4842 | char idstr[256]; |
| 4843 | int instance_id; |
| 4844 | int version_id; |
| 4845 | SaveStateHandler *save_state; |
| 4846 | LoadStateHandler *load_state; |
| 4847 | void *opaque; |
| 4848 | struct SaveStateEntry *next; |
| 4849 | } SaveStateEntry; |
| 4850 | |
| 4851 | static SaveStateEntry *first_se; |
| 4852 | |
| 4853 | int register_savevm(const char *idstr, |
| 4854 | int instance_id, |
| 4855 | int version_id, |
| 4856 | SaveStateHandler *save_state, |
| 4857 | LoadStateHandler *load_state, |
| 4858 | void *opaque) |
| 4859 | { |
| 4860 | SaveStateEntry *se, **pse; |
| 4861 | |
| 4862 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 4863 | if (!se) |
| 4864 | return -1; |
| 4865 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 4866 | se->instance_id = instance_id; |
| 4867 | se->version_id = version_id; |
| 4868 | se->save_state = save_state; |
| 4869 | se->load_state = load_state; |
| 4870 | se->opaque = opaque; |
| 4871 | se->next = NULL; |
| 4872 | |
| 4873 | /* add at the end of list */ |
| 4874 | pse = &first_se; |
| 4875 | while (*pse != NULL) |
| 4876 | pse = &(*pse)->next; |
| 4877 | *pse = se; |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
| 4881 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4882 | #define QEMU_VM_FILE_VERSION 0x00000002 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4883 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4884 | int qemu_savevm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4885 | { |
| 4886 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4887 | int len, ret; |
| 4888 | int64_t cur_pos, len_pos, total_len_pos; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4889 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4890 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 4891 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4892 | total_len_pos = qemu_ftell(f); |
| 4893 | qemu_put_be64(f, 0); /* total size */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4894 | |
| 4895 | for(se = first_se; se != NULL; se = se->next) { |
| 4896 | /* ID string */ |
| 4897 | len = strlen(se->idstr); |
| 4898 | qemu_put_byte(f, len); |
| 4899 | qemu_put_buffer(f, se->idstr, len); |
| 4900 | |
| 4901 | qemu_put_be32(f, se->instance_id); |
| 4902 | qemu_put_be32(f, se->version_id); |
| 4903 | |
| 4904 | /* record size: filled later */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4905 | len_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4906 | qemu_put_be32(f, 0); |
| 4907 | |
| 4908 | se->save_state(f, se->opaque); |
| 4909 | |
| 4910 | /* fill record size */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4911 | cur_pos = qemu_ftell(f); |
| 4912 | len = cur_pos - len_pos - 4; |
| 4913 | qemu_fseek(f, len_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4914 | qemu_put_be32(f, len); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4915 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4916 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4917 | cur_pos = qemu_ftell(f); |
| 4918 | qemu_fseek(f, total_len_pos, SEEK_SET); |
| 4919 | qemu_put_be64(f, cur_pos - total_len_pos - 8); |
| 4920 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4921 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4922 | ret = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4923 | return ret; |
| 4924 | } |
| 4925 | |
| 4926 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 4927 | { |
| 4928 | SaveStateEntry *se; |
| 4929 | |
| 4930 | for(se = first_se; se != NULL; se = se->next) { |
| 4931 | if (!strcmp(se->idstr, idstr) && |
| 4932 | instance_id == se->instance_id) |
| 4933 | return se; |
| 4934 | } |
| 4935 | return NULL; |
| 4936 | } |
| 4937 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4938 | int qemu_loadvm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4939 | { |
| 4940 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4941 | int len, ret, instance_id, record_len, version_id; |
| 4942 | int64_t total_len, end_pos, cur_pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4943 | unsigned int v; |
| 4944 | char idstr[256]; |
| 4945 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4946 | v = qemu_get_be32(f); |
| 4947 | if (v != QEMU_VM_FILE_MAGIC) |
| 4948 | goto fail; |
| 4949 | v = qemu_get_be32(f); |
| 4950 | if (v != QEMU_VM_FILE_VERSION) { |
| 4951 | fail: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4952 | ret = -1; |
| 4953 | goto the_end; |
| 4954 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4955 | total_len = qemu_get_be64(f); |
| 4956 | end_pos = total_len + qemu_ftell(f); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4957 | for(;;) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4958 | if (qemu_ftell(f) >= end_pos) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4959 | break; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4960 | len = qemu_get_byte(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4961 | qemu_get_buffer(f, idstr, len); |
| 4962 | idstr[len] = '\0'; |
| 4963 | instance_id = qemu_get_be32(f); |
| 4964 | version_id = qemu_get_be32(f); |
| 4965 | record_len = qemu_get_be32(f); |
| 4966 | #if 0 |
| 4967 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
| 4968 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 4969 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4970 | cur_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4971 | se = find_se(idstr, instance_id); |
| 4972 | if (!se) { |
| 4973 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
| 4974 | instance_id, idstr); |
| 4975 | } else { |
| 4976 | ret = se->load_state(f, se->opaque, version_id); |
| 4977 | if (ret < 0) { |
| 4978 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 4979 | instance_id, idstr); |
| 4980 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 4981 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4982 | /* always seek to exact end of record */ |
| 4983 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 4984 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4985 | ret = 0; |
| 4986 | the_end: |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4987 | return ret; |
| 4988 | } |
| 4989 | |
| 4990 | /* device can contain snapshots */ |
| 4991 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 4992 | { |
| 4993 | return (bs && |
| 4994 | !bdrv_is_removable(bs) && |
| 4995 | !bdrv_is_read_only(bs)); |
| 4996 | } |
| 4997 | |
| 4998 | /* device must be snapshots in order to have a reliable snapshot */ |
| 4999 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 5000 | { |
| 5001 | return (bs && |
| 5002 | !bdrv_is_removable(bs) && |
| 5003 | !bdrv_is_read_only(bs)); |
| 5004 | } |
| 5005 | |
| 5006 | static BlockDriverState *get_bs_snapshots(void) |
| 5007 | { |
| 5008 | BlockDriverState *bs; |
| 5009 | int i; |
| 5010 | |
| 5011 | if (bs_snapshots) |
| 5012 | return bs_snapshots; |
| 5013 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5014 | bs = bs_table[i]; |
| 5015 | if (bdrv_can_snapshot(bs)) |
| 5016 | goto ok; |
| 5017 | } |
| 5018 | return NULL; |
| 5019 | ok: |
| 5020 | bs_snapshots = bs; |
| 5021 | return bs; |
| 5022 | } |
| 5023 | |
| 5024 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 5025 | const char *name) |
| 5026 | { |
| 5027 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5028 | int nb_sns, i, ret; |
| 5029 | |
| 5030 | ret = -ENOENT; |
| 5031 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5032 | if (nb_sns < 0) |
| 5033 | return ret; |
| 5034 | for(i = 0; i < nb_sns; i++) { |
| 5035 | sn = &sn_tab[i]; |
| 5036 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 5037 | *sn_info = *sn; |
| 5038 | ret = 0; |
| 5039 | break; |
| 5040 | } |
| 5041 | } |
| 5042 | qemu_free(sn_tab); |
| 5043 | return ret; |
| 5044 | } |
| 5045 | |
| 5046 | void do_savevm(const char *name) |
| 5047 | { |
| 5048 | BlockDriverState *bs, *bs1; |
| 5049 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
| 5050 | int must_delete, ret, i; |
| 5051 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5052 | QEMUFile *f; |
| 5053 | int saved_vm_running; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5054 | #ifdef _WIN32 |
| 5055 | struct _timeb tb; |
| 5056 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5057 | struct timeval tv; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5058 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5059 | |
| 5060 | bs = get_bs_snapshots(); |
| 5061 | if (!bs) { |
| 5062 | term_printf("No block device can accept snapshots\n"); |
| 5063 | return; |
| 5064 | } |
| 5065 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5066 | /* ??? Should this occur after vm_stop? */ |
| 5067 | qemu_aio_flush(); |
| 5068 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5069 | saved_vm_running = vm_running; |
| 5070 | vm_stop(0); |
| 5071 | |
| 5072 | must_delete = 0; |
| 5073 | if (name) { |
| 5074 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 5075 | if (ret >= 0) { |
| 5076 | must_delete = 1; |
| 5077 | } |
| 5078 | } |
| 5079 | memset(sn, 0, sizeof(*sn)); |
| 5080 | if (must_delete) { |
| 5081 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 5082 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 5083 | } else { |
| 5084 | if (name) |
| 5085 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 5086 | } |
| 5087 | |
| 5088 | /* fill auxiliary fields */ |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5089 | #ifdef _WIN32 |
| 5090 | _ftime(&tb); |
| 5091 | sn->date_sec = tb.time; |
| 5092 | sn->date_nsec = tb.millitm * 1000000; |
| 5093 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5094 | gettimeofday(&tv, NULL); |
| 5095 | sn->date_sec = tv.tv_sec; |
| 5096 | sn->date_nsec = tv.tv_usec * 1000; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5097 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5098 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
| 5099 | |
| 5100 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5101 | term_printf("Device %s does not support VM state snapshots\n", |
| 5102 | bdrv_get_device_name(bs)); |
| 5103 | goto the_end; |
| 5104 | } |
| 5105 | |
| 5106 | /* save the VM state */ |
| 5107 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); |
| 5108 | if (!f) { |
| 5109 | term_printf("Could not open VM state file\n"); |
| 5110 | goto the_end; |
| 5111 | } |
| 5112 | ret = qemu_savevm_state(f); |
| 5113 | sn->vm_state_size = qemu_ftell(f); |
| 5114 | qemu_fclose(f); |
| 5115 | if (ret < 0) { |
| 5116 | term_printf("Error %d while writing VM\n", ret); |
| 5117 | goto the_end; |
| 5118 | } |
| 5119 | |
| 5120 | /* create the snapshots */ |
| 5121 | |
| 5122 | for(i = 0; i < MAX_DISKS; i++) { |
| 5123 | bs1 = bs_table[i]; |
| 5124 | if (bdrv_has_snapshot(bs1)) { |
| 5125 | if (must_delete) { |
| 5126 | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
| 5127 | if (ret < 0) { |
| 5128 | term_printf("Error while deleting snapshot on '%s'\n", |
| 5129 | bdrv_get_device_name(bs1)); |
| 5130 | } |
| 5131 | } |
| 5132 | ret = bdrv_snapshot_create(bs1, sn); |
| 5133 | if (ret < 0) { |
| 5134 | term_printf("Error while creating snapshot on '%s'\n", |
| 5135 | bdrv_get_device_name(bs1)); |
| 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | |
| 5140 | the_end: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5141 | if (saved_vm_running) |
| 5142 | vm_start(); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | void do_loadvm(const char *name) |
| 5146 | { |
| 5147 | BlockDriverState *bs, *bs1; |
| 5148 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5149 | QEMUFile *f; |
| 5150 | int i, ret; |
| 5151 | int saved_vm_running; |
| 5152 | |
| 5153 | bs = get_bs_snapshots(); |
| 5154 | if (!bs) { |
| 5155 | term_printf("No block device supports snapshots\n"); |
| 5156 | return; |
| 5157 | } |
| 5158 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5159 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 5160 | qemu_aio_flush(); |
| 5161 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5162 | saved_vm_running = vm_running; |
| 5163 | vm_stop(0); |
| 5164 | |
| 5165 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5166 | bs1 = bs_table[i]; |
| 5167 | if (bdrv_has_snapshot(bs1)) { |
| 5168 | ret = bdrv_snapshot_goto(bs1, name); |
| 5169 | if (ret < 0) { |
| 5170 | if (bs != bs1) |
| 5171 | term_printf("Warning: "); |
| 5172 | switch(ret) { |
| 5173 | case -ENOTSUP: |
| 5174 | term_printf("Snapshots not supported on device '%s'\n", |
| 5175 | bdrv_get_device_name(bs1)); |
| 5176 | break; |
| 5177 | case -ENOENT: |
| 5178 | term_printf("Could not find snapshot '%s' on device '%s'\n", |
| 5179 | name, bdrv_get_device_name(bs1)); |
| 5180 | break; |
| 5181 | default: |
| 5182 | term_printf("Error %d while activating snapshot on '%s'\n", |
| 5183 | ret, bdrv_get_device_name(bs1)); |
| 5184 | break; |
| 5185 | } |
| 5186 | /* fatal on snapshot block device */ |
| 5187 | if (bs == bs1) |
| 5188 | goto the_end; |
| 5189 | } |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5194 | term_printf("Device %s does not support VM state snapshots\n", |
| 5195 | bdrv_get_device_name(bs)); |
| 5196 | return; |
| 5197 | } |
| 5198 | |
| 5199 | /* restore the VM state */ |
| 5200 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); |
| 5201 | if (!f) { |
| 5202 | term_printf("Could not open VM state file\n"); |
| 5203 | goto the_end; |
| 5204 | } |
| 5205 | ret = qemu_loadvm_state(f); |
| 5206 | qemu_fclose(f); |
| 5207 | if (ret < 0) { |
| 5208 | term_printf("Error %d while loading VM state\n", ret); |
| 5209 | } |
| 5210 | the_end: |
| 5211 | if (saved_vm_running) |
| 5212 | vm_start(); |
| 5213 | } |
| 5214 | |
| 5215 | void do_delvm(const char *name) |
| 5216 | { |
| 5217 | BlockDriverState *bs, *bs1; |
| 5218 | int i, ret; |
| 5219 | |
| 5220 | bs = get_bs_snapshots(); |
| 5221 | if (!bs) { |
| 5222 | term_printf("No block device supports snapshots\n"); |
| 5223 | return; |
| 5224 | } |
| 5225 | |
| 5226 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5227 | bs1 = bs_table[i]; |
| 5228 | if (bdrv_has_snapshot(bs1)) { |
| 5229 | ret = bdrv_snapshot_delete(bs1, name); |
| 5230 | if (ret < 0) { |
| 5231 | if (ret == -ENOTSUP) |
| 5232 | term_printf("Snapshots not supported on device '%s'\n", |
| 5233 | bdrv_get_device_name(bs1)); |
| 5234 | else |
| 5235 | term_printf("Error %d while deleting snapshot on '%s'\n", |
| 5236 | ret, bdrv_get_device_name(bs1)); |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | void do_info_snapshots(void) |
| 5243 | { |
| 5244 | BlockDriverState *bs, *bs1; |
| 5245 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5246 | int nb_sns, i; |
| 5247 | char buf[256]; |
| 5248 | |
| 5249 | bs = get_bs_snapshots(); |
| 5250 | if (!bs) { |
| 5251 | term_printf("No available block device supports snapshots\n"); |
| 5252 | return; |
| 5253 | } |
| 5254 | term_printf("Snapshot devices:"); |
| 5255 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5256 | bs1 = bs_table[i]; |
| 5257 | if (bdrv_has_snapshot(bs1)) { |
| 5258 | if (bs == bs1) |
| 5259 | term_printf(" %s", bdrv_get_device_name(bs1)); |
| 5260 | } |
| 5261 | } |
| 5262 | term_printf("\n"); |
| 5263 | |
| 5264 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5265 | if (nb_sns < 0) { |
| 5266 | term_printf("bdrv_snapshot_list: error %d\n", nb_sns); |
| 5267 | return; |
| 5268 | } |
| 5269 | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs)); |
| 5270 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 5271 | for(i = 0; i < nb_sns; i++) { |
| 5272 | sn = &sn_tab[i]; |
| 5273 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 5274 | } |
| 5275 | qemu_free(sn_tab); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | /***********************************************************/ |
| 5279 | /* cpu save/restore */ |
| 5280 | |
| 5281 | #if defined(TARGET_I386) |
| 5282 | |
| 5283 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
| 5284 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5285 | qemu_put_be32(f, dt->selector); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5286 | qemu_put_betl(f, dt->base); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5287 | qemu_put_be32(f, dt->limit); |
| 5288 | qemu_put_be32(f, dt->flags); |
| 5289 | } |
| 5290 | |
| 5291 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
| 5292 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5293 | dt->selector = qemu_get_be32(f); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5294 | dt->base = qemu_get_betl(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5295 | dt->limit = qemu_get_be32(f); |
| 5296 | dt->flags = qemu_get_be32(f); |
| 5297 | } |
| 5298 | |
| 5299 | void cpu_save(QEMUFile *f, void *opaque) |
| 5300 | { |
| 5301 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5302 | uint16_t fptag, fpus, fpuc, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5303 | uint32_t hflags; |
| 5304 | int i; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5305 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5306 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5307 | qemu_put_betls(f, &env->regs[i]); |
| 5308 | qemu_put_betls(f, &env->eip); |
| 5309 | qemu_put_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5310 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ |
| 5311 | qemu_put_be32s(f, &hflags); |
| 5312 | |
| 5313 | /* FPU */ |
| 5314 | fpuc = env->fpuc; |
| 5315 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
| 5316 | fptag = 0; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5317 | for(i = 0; i < 8; i++) { |
| 5318 | fptag |= ((!env->fptags[i]) << i); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | qemu_put_be16s(f, &fpuc); |
| 5322 | qemu_put_be16s(f, &fpus); |
| 5323 | qemu_put_be16s(f, &fptag); |
| 5324 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5325 | #ifdef USE_X86LDOUBLE |
| 5326 | fpregs_format = 0; |
| 5327 | #else |
| 5328 | fpregs_format = 1; |
| 5329 | #endif |
| 5330 | qemu_put_be16s(f, &fpregs_format); |
| 5331 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5332 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5333 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5334 | { |
| 5335 | uint64_t mant; |
| 5336 | uint16_t exp; |
| 5337 | /* we save the real CPU data (in case of MMX usage only 'mant' |
| 5338 | contains the MMX register */ |
| 5339 | cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
| 5340 | qemu_put_be64(f, mant); |
| 5341 | qemu_put_be16(f, exp); |
| 5342 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5343 | #else |
| 5344 | /* if we use doubles for float emulation, we save the doubles to |
| 5345 | avoid losing information in case of MMX usage. It can give |
| 5346 | problems if the image is restored on a CPU where long |
| 5347 | doubles are used instead. */ |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5348 | qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0)); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5349 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | for(i = 0; i < 6; i++) |
| 5353 | cpu_put_seg(f, &env->segs[i]); |
| 5354 | cpu_put_seg(f, &env->ldt); |
| 5355 | cpu_put_seg(f, &env->tr); |
| 5356 | cpu_put_seg(f, &env->gdt); |
| 5357 | cpu_put_seg(f, &env->idt); |
| 5358 | |
| 5359 | qemu_put_be32s(f, &env->sysenter_cs); |
| 5360 | qemu_put_be32s(f, &env->sysenter_esp); |
| 5361 | qemu_put_be32s(f, &env->sysenter_eip); |
| 5362 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5363 | qemu_put_betls(f, &env->cr[0]); |
| 5364 | qemu_put_betls(f, &env->cr[2]); |
| 5365 | qemu_put_betls(f, &env->cr[3]); |
| 5366 | qemu_put_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5367 | |
| 5368 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5369 | qemu_put_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5370 | |
| 5371 | /* MMU */ |
| 5372 | qemu_put_be32s(f, &env->a20_mask); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5373 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5374 | /* XMM */ |
| 5375 | qemu_put_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5376 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5377 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5378 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5379 | } |
| 5380 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5381 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5382 | qemu_put_be64s(f, &env->efer); |
| 5383 | qemu_put_be64s(f, &env->star); |
| 5384 | qemu_put_be64s(f, &env->lstar); |
| 5385 | qemu_put_be64s(f, &env->cstar); |
| 5386 | qemu_put_be64s(f, &env->fmask); |
| 5387 | qemu_put_be64s(f, &env->kernelgsbase); |
| 5388 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5389 | qemu_put_be32s(f, &env->smbase); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5392 | #ifdef USE_X86LDOUBLE |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5393 | /* XXX: add that in a FPU generic layer */ |
| 5394 | union x86_longdouble { |
| 5395 | uint64_t mant; |
| 5396 | uint16_t exp; |
| 5397 | }; |
| 5398 | |
| 5399 | #define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
| 5400 | #define EXPBIAS1 1023 |
| 5401 | #define EXPD1(fp) ((fp >> 52) & 0x7FF) |
| 5402 | #define SIGND1(fp) ((fp >> 32) & 0x80000000) |
| 5403 | |
| 5404 | static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
| 5405 | { |
| 5406 | int e; |
| 5407 | /* mantissa */ |
| 5408 | p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
| 5409 | /* exponent + sign */ |
| 5410 | e = EXPD1(temp) - EXPBIAS1 + 16383; |
| 5411 | e |= SIGND1(temp) >> 16; |
| 5412 | p->exp = e; |
| 5413 | } |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5414 | #endif |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5415 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5416 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5417 | { |
| 5418 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5419 | int i, guess_mmx; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5420 | uint32_t hflags; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5421 | uint16_t fpus, fpuc, fptag, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5422 | |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5423 | if (version_id != 3 && version_id != 4) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5424 | return -EINVAL; |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5425 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5426 | qemu_get_betls(f, &env->regs[i]); |
| 5427 | qemu_get_betls(f, &env->eip); |
| 5428 | qemu_get_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5429 | qemu_get_be32s(f, &hflags); |
| 5430 | |
| 5431 | qemu_get_be16s(f, &fpuc); |
| 5432 | qemu_get_be16s(f, &fpus); |
| 5433 | qemu_get_be16s(f, &fptag); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5434 | qemu_get_be16s(f, &fpregs_format); |
| 5435 | |
| 5436 | /* NOTE: we cannot always restore the FPU state if the image come |
| 5437 | from a host with a different 'USE_X86LDOUBLE' define. We guess |
| 5438 | if we are in an MMX state to restore correctly in that case. */ |
| 5439 | guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5440 | for(i = 0; i < 8; i++) { |
| 5441 | uint64_t mant; |
| 5442 | uint16_t exp; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5443 | |
| 5444 | switch(fpregs_format) { |
| 5445 | case 0: |
| 5446 | mant = qemu_get_be64(f); |
| 5447 | exp = qemu_get_be16(f); |
| 5448 | #ifdef USE_X86LDOUBLE |
| 5449 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5450 | #else |
| 5451 | /* difficult case */ |
| 5452 | if (guess_mmx) |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5453 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5454 | else |
| 5455 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5456 | #endif |
| 5457 | break; |
| 5458 | case 1: |
| 5459 | mant = qemu_get_be64(f); |
| 5460 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5461 | { |
| 5462 | union x86_longdouble *p; |
| 5463 | /* difficult case */ |
| 5464 | p = (void *)&env->fpregs[i]; |
| 5465 | if (guess_mmx) { |
| 5466 | p->mant = mant; |
| 5467 | p->exp = 0xffff; |
| 5468 | } else { |
| 5469 | fp64_to_fp80(p, mant); |
| 5470 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5471 | } |
| 5472 | #else |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5473 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5474 | #endif |
| 5475 | break; |
| 5476 | default: |
| 5477 | return -EINVAL; |
| 5478 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | env->fpuc = fpuc; |
bellard | 7a0e1f4 | 2005-03-13 17:01:47 +0000 | [diff] [blame] | 5482 | /* XXX: restore FPU round state */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5483 | env->fpstt = (fpus >> 11) & 7; |
| 5484 | env->fpus = fpus & ~0x3800; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5485 | fptag ^= 0xff; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5486 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5487 | env->fptags[i] = (fptag >> i) & 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | for(i = 0; i < 6; i++) |
| 5491 | cpu_get_seg(f, &env->segs[i]); |
| 5492 | cpu_get_seg(f, &env->ldt); |
| 5493 | cpu_get_seg(f, &env->tr); |
| 5494 | cpu_get_seg(f, &env->gdt); |
| 5495 | cpu_get_seg(f, &env->idt); |
| 5496 | |
| 5497 | qemu_get_be32s(f, &env->sysenter_cs); |
| 5498 | qemu_get_be32s(f, &env->sysenter_esp); |
| 5499 | qemu_get_be32s(f, &env->sysenter_eip); |
| 5500 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5501 | qemu_get_betls(f, &env->cr[0]); |
| 5502 | qemu_get_betls(f, &env->cr[2]); |
| 5503 | qemu_get_betls(f, &env->cr[3]); |
| 5504 | qemu_get_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5505 | |
| 5506 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5507 | qemu_get_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5508 | |
| 5509 | /* MMU */ |
| 5510 | qemu_get_be32s(f, &env->a20_mask); |
| 5511 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5512 | qemu_get_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5513 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5514 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5515 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5516 | } |
| 5517 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5518 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5519 | qemu_get_be64s(f, &env->efer); |
| 5520 | qemu_get_be64s(f, &env->star); |
| 5521 | qemu_get_be64s(f, &env->lstar); |
| 5522 | qemu_get_be64s(f, &env->cstar); |
| 5523 | qemu_get_be64s(f, &env->fmask); |
| 5524 | qemu_get_be64s(f, &env->kernelgsbase); |
| 5525 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5526 | if (version_id >= 4) |
| 5527 | qemu_get_be32s(f, &env->smbase); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5528 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5529 | /* XXX: compute hflags from scratch, except for CPL and IIF */ |
| 5530 | env->hflags = hflags; |
| 5531 | tlb_flush(env, 1); |
| 5532 | return 0; |
| 5533 | } |
| 5534 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 5535 | #elif defined(TARGET_PPC) |
| 5536 | void cpu_save(QEMUFile *f, void *opaque) |
| 5537 | { |
| 5538 | } |
| 5539 | |
| 5540 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5541 | { |
| 5542 | return 0; |
| 5543 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 5544 | |
| 5545 | #elif defined(TARGET_MIPS) |
| 5546 | void cpu_save(QEMUFile *f, void *opaque) |
| 5547 | { |
| 5548 | } |
| 5549 | |
| 5550 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5551 | { |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5555 | #elif defined(TARGET_SPARC) |
| 5556 | void cpu_save(QEMUFile *f, void *opaque) |
| 5557 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5558 | CPUState *env = opaque; |
| 5559 | int i; |
| 5560 | uint32_t tmp; |
| 5561 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5562 | for(i = 0; i < 8; i++) |
| 5563 | qemu_put_betls(f, &env->gregs[i]); |
| 5564 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5565 | qemu_put_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5568 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5569 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5570 | float32 f; |
| 5571 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5572 | } u; |
| 5573 | u.f = env->fpr[i]; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5574 | qemu_put_be32(f, u.i); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5575 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5576 | |
| 5577 | qemu_put_betls(f, &env->pc); |
| 5578 | qemu_put_betls(f, &env->npc); |
| 5579 | qemu_put_betls(f, &env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5580 | tmp = GET_PSR(env); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5581 | qemu_put_be32(f, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5582 | qemu_put_betls(f, &env->fsr); |
| 5583 | qemu_put_betls(f, &env->tbr); |
| 5584 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5585 | qemu_put_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5586 | /* MMU */ |
| 5587 | for(i = 0; i < 16; i++) |
| 5588 | qemu_put_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5589 | #endif |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5593 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5594 | CPUState *env = opaque; |
| 5595 | int i; |
| 5596 | uint32_t tmp; |
| 5597 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5598 | for(i = 0; i < 8; i++) |
| 5599 | qemu_get_betls(f, &env->gregs[i]); |
| 5600 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5601 | qemu_get_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5602 | |
| 5603 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5604 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5605 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5606 | float32 f; |
| 5607 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5608 | } u; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5609 | u.i = qemu_get_be32(f); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5610 | env->fpr[i] = u.f; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5611 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5612 | |
| 5613 | qemu_get_betls(f, &env->pc); |
| 5614 | qemu_get_betls(f, &env->npc); |
| 5615 | qemu_get_betls(f, &env->y); |
| 5616 | tmp = qemu_get_be32(f); |
| 5617 | env->cwp = 0; /* needed to ensure that the wrapping registers are |
| 5618 | correctly updated */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5619 | PUT_PSR(env, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5620 | qemu_get_betls(f, &env->fsr); |
| 5621 | qemu_get_betls(f, &env->tbr); |
| 5622 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5623 | qemu_get_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5624 | /* MMU */ |
| 5625 | for(i = 0; i < 16; i++) |
| 5626 | qemu_get_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5627 | #endif |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5628 | tlb_flush(env, 1); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5629 | return 0; |
| 5630 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 5631 | |
| 5632 | #elif defined(TARGET_ARM) |
| 5633 | |
| 5634 | /* ??? Need to implement these. */ |
| 5635 | void cpu_save(QEMUFile *f, void *opaque) |
| 5636 | { |
| 5637 | } |
| 5638 | |
| 5639 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5640 | { |
| 5641 | return 0; |
| 5642 | } |
| 5643 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5644 | #else |
| 5645 | |
| 5646 | #warning No CPU save/restore functions |
| 5647 | |
| 5648 | #endif |
| 5649 | |
| 5650 | /***********************************************************/ |
| 5651 | /* ram save/restore */ |
| 5652 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5653 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 5654 | { |
| 5655 | int v; |
| 5656 | |
| 5657 | v = qemu_get_byte(f); |
| 5658 | switch(v) { |
| 5659 | case 0: |
| 5660 | if (qemu_get_buffer(f, buf, len) != len) |
| 5661 | return -EIO; |
| 5662 | break; |
| 5663 | case 1: |
| 5664 | v = qemu_get_byte(f); |
| 5665 | memset(buf, v, len); |
| 5666 | break; |
| 5667 | default: |
| 5668 | return -EINVAL; |
| 5669 | } |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5673 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5674 | { |
| 5675 | int i, ret; |
| 5676 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5677 | if (qemu_get_be32(f) != phys_ram_size) |
| 5678 | return -EINVAL; |
| 5679 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 5680 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 5681 | if (ret) |
| 5682 | return ret; |
| 5683 | } |
| 5684 | return 0; |
| 5685 | } |
| 5686 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5687 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 5688 | #define IOBUF_SIZE 4096 |
| 5689 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 5690 | |
| 5691 | typedef struct RamCompressState { |
| 5692 | z_stream zstream; |
| 5693 | QEMUFile *f; |
| 5694 | uint8_t buf[IOBUF_SIZE]; |
| 5695 | } RamCompressState; |
| 5696 | |
| 5697 | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
| 5698 | { |
| 5699 | int ret; |
| 5700 | memset(s, 0, sizeof(*s)); |
| 5701 | s->f = f; |
| 5702 | ret = deflateInit2(&s->zstream, 1, |
| 5703 | Z_DEFLATED, 15, |
| 5704 | 9, Z_DEFAULT_STRATEGY); |
| 5705 | if (ret != Z_OK) |
| 5706 | return -1; |
| 5707 | s->zstream.avail_out = IOBUF_SIZE; |
| 5708 | s->zstream.next_out = s->buf; |
| 5709 | return 0; |
| 5710 | } |
| 5711 | |
| 5712 | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
| 5713 | { |
| 5714 | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
| 5715 | qemu_put_be16(s->f, len); |
| 5716 | qemu_put_buffer(s->f, buf, len); |
| 5717 | } |
| 5718 | |
| 5719 | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
| 5720 | { |
| 5721 | int ret; |
| 5722 | |
| 5723 | s->zstream.avail_in = len; |
| 5724 | s->zstream.next_in = (uint8_t *)buf; |
| 5725 | while (s->zstream.avail_in > 0) { |
| 5726 | ret = deflate(&s->zstream, Z_NO_FLUSH); |
| 5727 | if (ret != Z_OK) |
| 5728 | return -1; |
| 5729 | if (s->zstream.avail_out == 0) { |
| 5730 | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
| 5731 | s->zstream.avail_out = IOBUF_SIZE; |
| 5732 | s->zstream.next_out = s->buf; |
| 5733 | } |
| 5734 | } |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
| 5738 | static void ram_compress_close(RamCompressState *s) |
| 5739 | { |
| 5740 | int len, ret; |
| 5741 | |
| 5742 | /* compress last bytes */ |
| 5743 | for(;;) { |
| 5744 | ret = deflate(&s->zstream, Z_FINISH); |
| 5745 | if (ret == Z_OK || ret == Z_STREAM_END) { |
| 5746 | len = IOBUF_SIZE - s->zstream.avail_out; |
| 5747 | if (len > 0) { |
| 5748 | ram_put_cblock(s, s->buf, len); |
| 5749 | } |
| 5750 | s->zstream.avail_out = IOBUF_SIZE; |
| 5751 | s->zstream.next_out = s->buf; |
| 5752 | if (ret == Z_STREAM_END) |
| 5753 | break; |
| 5754 | } else { |
| 5755 | goto fail; |
| 5756 | } |
| 5757 | } |
| 5758 | fail: |
| 5759 | deflateEnd(&s->zstream); |
| 5760 | } |
| 5761 | |
| 5762 | typedef struct RamDecompressState { |
| 5763 | z_stream zstream; |
| 5764 | QEMUFile *f; |
| 5765 | uint8_t buf[IOBUF_SIZE]; |
| 5766 | } RamDecompressState; |
| 5767 | |
| 5768 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 5769 | { |
| 5770 | int ret; |
| 5771 | memset(s, 0, sizeof(*s)); |
| 5772 | s->f = f; |
| 5773 | ret = inflateInit(&s->zstream); |
| 5774 | if (ret != Z_OK) |
| 5775 | return -1; |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
| 5779 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 5780 | { |
| 5781 | int ret, clen; |
| 5782 | |
| 5783 | s->zstream.avail_out = len; |
| 5784 | s->zstream.next_out = buf; |
| 5785 | while (s->zstream.avail_out > 0) { |
| 5786 | if (s->zstream.avail_in == 0) { |
| 5787 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 5788 | return -1; |
| 5789 | clen = qemu_get_be16(s->f); |
| 5790 | if (clen > IOBUF_SIZE) |
| 5791 | return -1; |
| 5792 | qemu_get_buffer(s->f, s->buf, clen); |
| 5793 | s->zstream.avail_in = clen; |
| 5794 | s->zstream.next_in = s->buf; |
| 5795 | } |
| 5796 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 5797 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 5798 | return -1; |
| 5799 | } |
| 5800 | } |
| 5801 | return 0; |
| 5802 | } |
| 5803 | |
| 5804 | static void ram_decompress_close(RamDecompressState *s) |
| 5805 | { |
| 5806 | inflateEnd(&s->zstream); |
| 5807 | } |
| 5808 | |
| 5809 | static void ram_save(QEMUFile *f, void *opaque) |
| 5810 | { |
| 5811 | int i; |
| 5812 | RamCompressState s1, *s = &s1; |
| 5813 | uint8_t buf[10]; |
| 5814 | |
| 5815 | qemu_put_be32(f, phys_ram_size); |
| 5816 | if (ram_compress_open(s, f) < 0) |
| 5817 | return; |
| 5818 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5819 | #if 0 |
| 5820 | if (tight_savevm_enabled) { |
| 5821 | int64_t sector_num; |
| 5822 | int j; |
| 5823 | |
| 5824 | /* find if the memory block is available on a virtual |
| 5825 | block device */ |
| 5826 | sector_num = -1; |
| 5827 | for(j = 0; j < MAX_DISKS; j++) { |
| 5828 | if (bs_table[j]) { |
| 5829 | sector_num = bdrv_hash_find(bs_table[j], |
| 5830 | phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5831 | if (sector_num >= 0) |
| 5832 | break; |
| 5833 | } |
| 5834 | } |
| 5835 | if (j == MAX_DISKS) |
| 5836 | goto normal_compress; |
| 5837 | buf[0] = 1; |
| 5838 | buf[1] = j; |
| 5839 | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num); |
| 5840 | ram_compress_buf(s, buf, 10); |
| 5841 | } else |
| 5842 | #endif |
| 5843 | { |
| 5844 | // normal_compress: |
| 5845 | buf[0] = 0; |
| 5846 | ram_compress_buf(s, buf, 1); |
| 5847 | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5848 | } |
| 5849 | } |
| 5850 | ram_compress_close(s); |
| 5851 | } |
| 5852 | |
| 5853 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 5854 | { |
| 5855 | RamDecompressState s1, *s = &s1; |
| 5856 | uint8_t buf[10]; |
| 5857 | int i; |
| 5858 | |
| 5859 | if (version_id == 1) |
| 5860 | return ram_load_v1(f, opaque); |
| 5861 | if (version_id != 2) |
| 5862 | return -EINVAL; |
| 5863 | if (qemu_get_be32(f) != phys_ram_size) |
| 5864 | return -EINVAL; |
| 5865 | if (ram_decompress_open(s, f) < 0) |
| 5866 | return -EINVAL; |
| 5867 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5868 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 5869 | fprintf(stderr, "Error while reading ram block header\n"); |
| 5870 | goto error; |
| 5871 | } |
| 5872 | if (buf[0] == 0) { |
| 5873 | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
| 5874 | fprintf(stderr, "Error while reading ram block address=0x%08x", i); |
| 5875 | goto error; |
| 5876 | } |
| 5877 | } else |
| 5878 | #if 0 |
| 5879 | if (buf[0] == 1) { |
| 5880 | int bs_index; |
| 5881 | int64_t sector_num; |
| 5882 | |
| 5883 | ram_decompress_buf(s, buf + 1, 9); |
| 5884 | bs_index = buf[1]; |
| 5885 | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); |
| 5886 | if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) { |
| 5887 | fprintf(stderr, "Invalid block device index %d\n", bs_index); |
| 5888 | goto error; |
| 5889 | } |
| 5890 | if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i, |
| 5891 | BDRV_HASH_BLOCK_SIZE / 512) < 0) { |
| 5892 | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n", |
| 5893 | bs_index, sector_num); |
| 5894 | goto error; |
| 5895 | } |
| 5896 | } else |
| 5897 | #endif |
| 5898 | { |
| 5899 | error: |
| 5900 | printf("Error block header\n"); |
| 5901 | return -EINVAL; |
| 5902 | } |
| 5903 | } |
| 5904 | ram_decompress_close(s); |
| 5905 | return 0; |
| 5906 | } |
| 5907 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5908 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5909 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 5910 | |
| 5911 | struct QEMUBH { |
| 5912 | QEMUBHFunc *cb; |
| 5913 | void *opaque; |
| 5914 | int scheduled; |
| 5915 | QEMUBH *next; |
| 5916 | }; |
| 5917 | |
| 5918 | static QEMUBH *first_bh = NULL; |
| 5919 | |
| 5920 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 5921 | { |
| 5922 | QEMUBH *bh; |
| 5923 | bh = qemu_mallocz(sizeof(QEMUBH)); |
| 5924 | if (!bh) |
| 5925 | return NULL; |
| 5926 | bh->cb = cb; |
| 5927 | bh->opaque = opaque; |
| 5928 | return bh; |
| 5929 | } |
| 5930 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5931 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5932 | { |
| 5933 | QEMUBH *bh, **pbh; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5934 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5935 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5936 | ret = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5937 | for(;;) { |
| 5938 | pbh = &first_bh; |
| 5939 | bh = *pbh; |
| 5940 | if (!bh) |
| 5941 | break; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5942 | ret = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5943 | *pbh = bh->next; |
| 5944 | bh->scheduled = 0; |
| 5945 | bh->cb(bh->opaque); |
| 5946 | } |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5947 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | void qemu_bh_schedule(QEMUBH *bh) |
| 5951 | { |
| 5952 | CPUState *env = cpu_single_env; |
| 5953 | if (bh->scheduled) |
| 5954 | return; |
| 5955 | bh->scheduled = 1; |
| 5956 | bh->next = first_bh; |
| 5957 | first_bh = bh; |
| 5958 | |
| 5959 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 5960 | if (env) { |
| 5961 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | void qemu_bh_cancel(QEMUBH *bh) |
| 5966 | { |
| 5967 | QEMUBH **pbh; |
| 5968 | if (bh->scheduled) { |
| 5969 | pbh = &first_bh; |
| 5970 | while (*pbh != bh) |
| 5971 | pbh = &(*pbh)->next; |
| 5972 | *pbh = bh->next; |
| 5973 | bh->scheduled = 0; |
| 5974 | } |
| 5975 | } |
| 5976 | |
| 5977 | void qemu_bh_delete(QEMUBH *bh) |
| 5978 | { |
| 5979 | qemu_bh_cancel(bh); |
| 5980 | qemu_free(bh); |
| 5981 | } |
| 5982 | |
| 5983 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5984 | /* machine registration */ |
| 5985 | |
| 5986 | QEMUMachine *first_machine = NULL; |
| 5987 | |
| 5988 | int qemu_register_machine(QEMUMachine *m) |
| 5989 | { |
| 5990 | QEMUMachine **pm; |
| 5991 | pm = &first_machine; |
| 5992 | while (*pm != NULL) |
| 5993 | pm = &(*pm)->next; |
| 5994 | m->next = NULL; |
| 5995 | *pm = m; |
| 5996 | return 0; |
| 5997 | } |
| 5998 | |
| 5999 | QEMUMachine *find_machine(const char *name) |
| 6000 | { |
| 6001 | QEMUMachine *m; |
| 6002 | |
| 6003 | for(m = first_machine; m != NULL; m = m->next) { |
| 6004 | if (!strcmp(m->name, name)) |
| 6005 | return m; |
| 6006 | } |
| 6007 | return NULL; |
| 6008 | } |
| 6009 | |
| 6010 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6011 | /* main execution loop */ |
| 6012 | |
| 6013 | void gui_update(void *opaque) |
| 6014 | { |
| 6015 | display_state.dpy_refresh(&display_state); |
| 6016 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
| 6017 | } |
| 6018 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6019 | struct vm_change_state_entry { |
| 6020 | VMChangeStateHandler *cb; |
| 6021 | void *opaque; |
| 6022 | LIST_ENTRY (vm_change_state_entry) entries; |
| 6023 | }; |
| 6024 | |
| 6025 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 6026 | |
| 6027 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 6028 | void *opaque) |
| 6029 | { |
| 6030 | VMChangeStateEntry *e; |
| 6031 | |
| 6032 | e = qemu_mallocz(sizeof (*e)); |
| 6033 | if (!e) |
| 6034 | return NULL; |
| 6035 | |
| 6036 | e->cb = cb; |
| 6037 | e->opaque = opaque; |
| 6038 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 6039 | return e; |
| 6040 | } |
| 6041 | |
| 6042 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 6043 | { |
| 6044 | LIST_REMOVE (e, entries); |
| 6045 | qemu_free (e); |
| 6046 | } |
| 6047 | |
| 6048 | static void vm_state_notify(int running) |
| 6049 | { |
| 6050 | VMChangeStateEntry *e; |
| 6051 | |
| 6052 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
| 6053 | e->cb(e->opaque, running); |
| 6054 | } |
| 6055 | } |
| 6056 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6057 | /* XXX: support several handlers */ |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6058 | static VMStopHandler *vm_stop_cb; |
| 6059 | static void *vm_stop_opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6060 | |
| 6061 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6062 | { |
| 6063 | vm_stop_cb = cb; |
| 6064 | vm_stop_opaque = opaque; |
| 6065 | return 0; |
| 6066 | } |
| 6067 | |
| 6068 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6069 | { |
| 6070 | vm_stop_cb = NULL; |
| 6071 | } |
| 6072 | |
| 6073 | void vm_start(void) |
| 6074 | { |
| 6075 | if (!vm_running) { |
| 6076 | cpu_enable_ticks(); |
| 6077 | vm_running = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6078 | vm_state_notify(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6079 | } |
| 6080 | } |
| 6081 | |
| 6082 | void vm_stop(int reason) |
| 6083 | { |
| 6084 | if (vm_running) { |
| 6085 | cpu_disable_ticks(); |
| 6086 | vm_running = 0; |
| 6087 | if (reason != 0) { |
| 6088 | if (vm_stop_cb) { |
| 6089 | vm_stop_cb(vm_stop_opaque, reason); |
| 6090 | } |
| 6091 | } |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6092 | vm_state_notify(0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6096 | /* reset/shutdown handler */ |
| 6097 | |
| 6098 | typedef struct QEMUResetEntry { |
| 6099 | QEMUResetHandler *func; |
| 6100 | void *opaque; |
| 6101 | struct QEMUResetEntry *next; |
| 6102 | } QEMUResetEntry; |
| 6103 | |
| 6104 | static QEMUResetEntry *first_reset_entry; |
| 6105 | static int reset_requested; |
| 6106 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6107 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6108 | |
| 6109 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 6110 | { |
| 6111 | QEMUResetEntry **pre, *re; |
| 6112 | |
| 6113 | pre = &first_reset_entry; |
| 6114 | while (*pre != NULL) |
| 6115 | pre = &(*pre)->next; |
| 6116 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 6117 | re->func = func; |
| 6118 | re->opaque = opaque; |
| 6119 | re->next = NULL; |
| 6120 | *pre = re; |
| 6121 | } |
| 6122 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 6123 | static void qemu_system_reset(void) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6124 | { |
| 6125 | QEMUResetEntry *re; |
| 6126 | |
| 6127 | /* reset all devices */ |
| 6128 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 6129 | re->func(re->opaque); |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | void qemu_system_reset_request(void) |
| 6134 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6135 | if (no_reboot) { |
| 6136 | shutdown_requested = 1; |
| 6137 | } else { |
| 6138 | reset_requested = 1; |
| 6139 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6140 | if (cpu_single_env) |
| 6141 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | void qemu_system_shutdown_request(void) |
| 6145 | { |
| 6146 | shutdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6147 | if (cpu_single_env) |
| 6148 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6151 | void qemu_system_powerdown_request(void) |
| 6152 | { |
| 6153 | powerdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6154 | if (cpu_single_env) |
| 6155 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6158 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6159 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6160 | IOHandlerRecord *ioh, *ioh_next; |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6161 | fd_set rfds, wfds, xfds; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6162 | int ret, nfds; |
| 6163 | struct timeval tv; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6164 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6165 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6166 | |
| 6167 | /* XXX: need to suppress polling by better using win32 events */ |
| 6168 | ret = 0; |
| 6169 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 6170 | ret |= pe->func(pe->opaque); |
| 6171 | } |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6172 | #ifdef _WIN32 |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6173 | if (ret == 0 && timeout > 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6174 | int err; |
| 6175 | WaitObjects *w = &wait_objects; |
| 6176 | |
| 6177 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
| 6178 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 6179 | if (w->func[ret - WAIT_OBJECT_0]) |
| 6180 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
| 6181 | } else if (ret == WAIT_TIMEOUT) { |
| 6182 | } else { |
| 6183 | err = GetLastError(); |
| 6184 | fprintf(stderr, "Wait error %d %d\n", ret, err); |
| 6185 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6186 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6187 | #endif |
| 6188 | /* poll any events */ |
| 6189 | /* XXX: separate device handlers from system ones */ |
| 6190 | nfds = -1; |
| 6191 | FD_ZERO(&rfds); |
| 6192 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6193 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6194 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 6195 | if (ioh->fd_read && |
| 6196 | (!ioh->fd_read_poll || |
| 6197 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 6198 | FD_SET(ioh->fd, &rfds); |
| 6199 | if (ioh->fd > nfds) |
| 6200 | nfds = ioh->fd; |
| 6201 | } |
| 6202 | if (ioh->fd_write) { |
| 6203 | FD_SET(ioh->fd, &wfds); |
| 6204 | if (ioh->fd > nfds) |
| 6205 | nfds = ioh->fd; |
| 6206 | } |
| 6207 | } |
| 6208 | |
| 6209 | tv.tv_sec = 0; |
| 6210 | #ifdef _WIN32 |
| 6211 | tv.tv_usec = 0; |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6212 | #else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6213 | tv.tv_usec = timeout * 1000; |
| 6214 | #endif |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6215 | #if defined(CONFIG_SLIRP) |
| 6216 | if (slirp_inited) { |
| 6217 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 6218 | } |
| 6219 | #endif |
| 6220 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6221 | if (ret > 0) { |
| 6222 | /* XXX: better handling of removal */ |
| 6223 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
| 6224 | ioh_next = ioh->next; |
| 6225 | if (FD_ISSET(ioh->fd, &rfds)) { |
| 6226 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6227 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6228 | if (FD_ISSET(ioh->fd, &wfds)) { |
| 6229 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6230 | } |
| 6231 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6232 | } |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6233 | #if defined(CONFIG_SLIRP) |
| 6234 | if (slirp_inited) { |
| 6235 | if (ret < 0) { |
| 6236 | FD_ZERO(&rfds); |
| 6237 | FD_ZERO(&wfds); |
| 6238 | FD_ZERO(&xfds); |
| 6239 | } |
| 6240 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 6241 | } |
| 6242 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6243 | qemu_aio_poll(); |
| 6244 | qemu_bh_poll(); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6245 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6246 | if (vm_running) { |
| 6247 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 6248 | qemu_get_clock(vm_clock)); |
| 6249 | /* run dma transfers, if any */ |
| 6250 | DMA_run(); |
| 6251 | } |
| 6252 | |
| 6253 | /* real time timers */ |
| 6254 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 6255 | qemu_get_clock(rt_clock)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6258 | static CPUState *cur_cpu; |
| 6259 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6260 | int main_loop(void) |
| 6261 | { |
| 6262 | int ret, timeout; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6263 | #ifdef CONFIG_PROFILER |
| 6264 | int64_t ti; |
| 6265 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6266 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6267 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6268 | cur_cpu = first_cpu; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6269 | for(;;) { |
| 6270 | if (vm_running) { |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6271 | |
| 6272 | env = cur_cpu; |
| 6273 | for(;;) { |
| 6274 | /* get next cpu */ |
| 6275 | env = env->next_cpu; |
| 6276 | if (!env) |
| 6277 | env = first_cpu; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6278 | #ifdef CONFIG_PROFILER |
| 6279 | ti = profile_getclock(); |
| 6280 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6281 | ret = cpu_exec(env); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6282 | #ifdef CONFIG_PROFILER |
| 6283 | qemu_time += profile_getclock() - ti; |
| 6284 | #endif |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6285 | if (ret != EXCP_HALTED) |
| 6286 | break; |
| 6287 | /* all CPUs are halted ? */ |
| 6288 | if (env == cur_cpu) { |
| 6289 | ret = EXCP_HLT; |
| 6290 | break; |
| 6291 | } |
| 6292 | } |
| 6293 | cur_cpu = env; |
| 6294 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6295 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6296 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6297 | break; |
| 6298 | } |
| 6299 | if (reset_requested) { |
| 6300 | reset_requested = 0; |
| 6301 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6302 | ret = EXCP_INTERRUPT; |
| 6303 | } |
| 6304 | if (powerdown_requested) { |
| 6305 | powerdown_requested = 0; |
| 6306 | qemu_system_powerdown(); |
| 6307 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6308 | } |
| 6309 | if (ret == EXCP_DEBUG) { |
| 6310 | vm_stop(EXCP_DEBUG); |
| 6311 | } |
| 6312 | /* if hlt instruction, we wait until the next IRQ */ |
| 6313 | /* XXX: use timeout computed from timers */ |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6314 | if (ret == EXCP_HLT) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6315 | timeout = 10; |
| 6316 | else |
| 6317 | timeout = 0; |
| 6318 | } else { |
| 6319 | timeout = 10; |
| 6320 | } |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6321 | #ifdef CONFIG_PROFILER |
| 6322 | ti = profile_getclock(); |
| 6323 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6324 | main_loop_wait(timeout); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6325 | #ifdef CONFIG_PROFILER |
| 6326 | dev_time += profile_getclock() - ti; |
| 6327 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6328 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 6329 | cpu_disable_ticks(); |
| 6330 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6333 | void help(void) |
| 6334 | { |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 6335 | 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] | 6336 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6337 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6338 | "'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] | 6339 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6340 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6341 | "-M machine select emulated machine (-M ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6342 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6343 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 6344 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6345 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 6346 | "-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] | 6347 | "-snapshot write to temporary files instead of disk image files\n" |
| 6348 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6349 | "-no-frame open SDL window without a frame and window decorations\n" |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6350 | "-no-quit disable SDL window close capability\n" |
| 6351 | #endif |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6352 | #ifdef TARGET_I386 |
| 6353 | "-no-fd-bootchk disable boot signature checking for floppy disks\n" |
| 6354 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6355 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 6356 | "-smp n set the number of CPUs to 'n' [default=1]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6357 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6358 | #ifndef _WIN32 |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6359 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6360 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6361 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6362 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 6363 | "-soundhw c1,... enable audio support\n" |
| 6364 | " and only specified sound cards (comma separated list)\n" |
| 6365 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6366 | " use -soundhw all to enable all of them\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6367 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 6368 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6369 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6370 | #ifdef TARGET_I386 |
| 6371 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 6372 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 6373 | "-usb enable the USB driver (will be the default soon)\n" |
| 6374 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6375 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 6376 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6377 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6378 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6379 | "Network options:\n" |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 6380 | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6381 | " create a new Network Interface Card and connect it to VLAN 'n'\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6382 | #ifdef CONFIG_SLIRP |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 6383 | "-net user[,vlan=n][,hostname=host]\n" |
| 6384 | " connect the user mode network stack to VLAN 'n' and send\n" |
| 6385 | " hostname 'host' to DHCP clients\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6386 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6387 | #ifdef _WIN32 |
| 6388 | "-net tap[,vlan=n],ifname=name\n" |
| 6389 | " connect the host TAP network interface to VLAN 'n'\n" |
| 6390 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6391 | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n" |
| 6392 | " connect the host TAP network interface to VLAN 'n' and use\n" |
| 6393 | " the network script 'file' (default=%s);\n" |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 6394 | " use 'script=no' to disable script execution;\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6395 | " use 'fd=h' to connect to an already opened TAP interface\n" |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6396 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6397 | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6398 | " connect the vlan 'n' to another VLAN using a socket connection\n" |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 6399 | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" |
| 6400 | " connect the vlan 'n' to multicast maddr and port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6401 | "-net none use it alone to have zero network devices; if no -net option\n" |
| 6402 | " is provided, the default is '-net nic -net user'\n" |
| 6403 | "\n" |
| 6404 | #ifdef CONFIG_SLIRP |
| 6405 | "-tftp prefix allow tftp access to files starting with prefix [-net user]\n" |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame^] | 6406 | "-bootp file advertise file in BOOTP replies\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6407 | #ifndef _WIN32 |
| 6408 | "-smb dir allow SMB access to files in 'dir' [-net user]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6409 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6410 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6411 | " redirect TCP or UDP connections from host to guest [-net user]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6412 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6413 | "\n" |
| 6414 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6415 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 6416 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 6417 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6418 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6419 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6420 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 6421 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6422 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6423 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6424 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6425 | "-s wait gdb connection to port %d\n" |
| 6426 | "-p port change gdb connection port\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6427 | "-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] | 6428 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 6429 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | 87b4735 | 2006-08-17 17:22:54 +0000 | [diff] [blame] | 6430 | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6431 | #ifdef USE_KQEMU |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6432 | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6433 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 6434 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6435 | #ifdef USE_CODE_COPY |
| 6436 | "-no-code-copy disable code copy acceleration\n" |
| 6437 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6438 | #ifdef TARGET_I386 |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6439 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 6440 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6441 | "-no-acpi disable ACPI\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6442 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6443 | "-no-reboot exit instead of rebooting\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6444 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6445 | "-vnc display start a VNC server on display\n" |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6446 | #ifndef _WIN32 |
| 6447 | "-daemonize daemonize QEMU after initializing\n" |
| 6448 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6449 | "-option-rom rom load a file, rom, into the option ROM space\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6450 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6451 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 6452 | "ctrl-alt-f toggle full screen\n" |
| 6453 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 6454 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6455 | "\n" |
| 6456 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 6457 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6458 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6459 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6460 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6461 | DEFAULT_NETWORK_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6462 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 6463 | DEFAULT_GDBSTUB_PORT, |
| 6464 | "/tmp/qemu.log"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6465 | exit(1); |
| 6466 | } |
| 6467 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6468 | #define HAS_ARG 0x0001 |
| 6469 | |
| 6470 | enum { |
| 6471 | QEMU_OPTION_h, |
| 6472 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6473 | QEMU_OPTION_M, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6474 | QEMU_OPTION_fda, |
| 6475 | QEMU_OPTION_fdb, |
| 6476 | QEMU_OPTION_hda, |
| 6477 | QEMU_OPTION_hdb, |
| 6478 | QEMU_OPTION_hdc, |
| 6479 | QEMU_OPTION_hdd, |
| 6480 | QEMU_OPTION_cdrom, |
| 6481 | QEMU_OPTION_boot, |
| 6482 | QEMU_OPTION_snapshot, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6483 | #ifdef TARGET_I386 |
| 6484 | QEMU_OPTION_no_fd_bootchk, |
| 6485 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6486 | QEMU_OPTION_m, |
| 6487 | QEMU_OPTION_nographic, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6488 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6489 | QEMU_OPTION_audio_help, |
| 6490 | QEMU_OPTION_soundhw, |
| 6491 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6492 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6493 | QEMU_OPTION_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6494 | QEMU_OPTION_tftp, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame^] | 6495 | QEMU_OPTION_bootp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6496 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6497 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6498 | |
| 6499 | QEMU_OPTION_kernel, |
| 6500 | QEMU_OPTION_append, |
| 6501 | QEMU_OPTION_initrd, |
| 6502 | |
| 6503 | QEMU_OPTION_S, |
| 6504 | QEMU_OPTION_s, |
| 6505 | QEMU_OPTION_p, |
| 6506 | QEMU_OPTION_d, |
| 6507 | QEMU_OPTION_hdachs, |
| 6508 | QEMU_OPTION_L, |
| 6509 | QEMU_OPTION_no_code_copy, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6510 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6511 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6512 | QEMU_OPTION_cirrusvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6513 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6514 | QEMU_OPTION_std_vga, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6515 | QEMU_OPTION_echr, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6516 | QEMU_OPTION_monitor, |
| 6517 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6518 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6519 | QEMU_OPTION_loadvm, |
| 6520 | QEMU_OPTION_full_screen, |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6521 | QEMU_OPTION_no_frame, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6522 | QEMU_OPTION_no_quit, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6523 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6524 | QEMU_OPTION_no_kqemu, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6525 | QEMU_OPTION_kernel_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6526 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 6527 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6528 | QEMU_OPTION_usbdevice, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6529 | QEMU_OPTION_smp, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6530 | QEMU_OPTION_vnc, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6531 | QEMU_OPTION_no_acpi, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6532 | QEMU_OPTION_no_reboot, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6533 | QEMU_OPTION_daemonize, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6534 | QEMU_OPTION_option_rom, |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 6535 | QEMU_OPTION_semihosting |
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 }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6549 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 6550 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 6551 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 6552 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 6553 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 6554 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
| 6555 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
| 6556 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 6557 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6558 | #ifdef TARGET_I386 |
| 6559 | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
| 6560 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6561 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 6562 | { "nographic", 0, QEMU_OPTION_nographic }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6563 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6564 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6565 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 6566 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 6567 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6568 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6569 | { "net", HAS_ARG, QEMU_OPTION_net}, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6570 | #ifdef CONFIG_SLIRP |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6571 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame^] | 6572 | { "bootp", HAS_ARG, QEMU_OPTION_bootp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6573 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6574 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6575 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6576 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6577 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6578 | |
| 6579 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 6580 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 6581 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 6582 | |
| 6583 | { "S", 0, QEMU_OPTION_S }, |
| 6584 | { "s", 0, QEMU_OPTION_s }, |
| 6585 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 6586 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 6587 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 6588 | { "L", HAS_ARG, QEMU_OPTION_L }, |
| 6589 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6590 | #ifdef USE_KQEMU |
| 6591 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6592 | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6593 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6594 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6595 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 6596 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6597 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6598 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6599 | { "echr", 1, QEMU_OPTION_echr }, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6600 | { "monitor", 1, QEMU_OPTION_monitor }, |
| 6601 | { "serial", 1, QEMU_OPTION_serial }, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6602 | { "parallel", 1, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6603 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 6604 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6605 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 6606 | { "no-frame", 0, QEMU_OPTION_no_frame }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6607 | { "no-quit", 0, QEMU_OPTION_no_quit }, |
| 6608 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6609 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6610 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6611 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6612 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6613 | { "vnc", HAS_ARG, QEMU_OPTION_vnc }, |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6614 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6615 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6616 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6617 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6618 | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6619 | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6620 | { "daemonize", 0, QEMU_OPTION_daemonize }, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6621 | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom }, |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 6622 | #if defined(TARGET_ARM) |
| 6623 | { "semihosting", 0, QEMU_OPTION_semihosting }, |
| 6624 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6625 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6626 | }; |
| 6627 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6628 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 6629 | |
| 6630 | /* this stack is only used during signal handling */ |
| 6631 | #define SIGNAL_STACK_SIZE 32768 |
| 6632 | |
| 6633 | static uint8_t *signal_stack; |
| 6634 | |
| 6635 | #endif |
| 6636 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6637 | /* password input */ |
| 6638 | |
| 6639 | static BlockDriverState *get_bdrv(int index) |
| 6640 | { |
| 6641 | BlockDriverState *bs; |
| 6642 | |
| 6643 | if (index < 4) { |
| 6644 | bs = bs_table[index]; |
| 6645 | } else if (index < 6) { |
| 6646 | bs = fd_table[index - 4]; |
| 6647 | } else { |
| 6648 | bs = NULL; |
| 6649 | } |
| 6650 | return bs; |
| 6651 | } |
| 6652 | |
| 6653 | static void read_passwords(void) |
| 6654 | { |
| 6655 | BlockDriverState *bs; |
| 6656 | int i, j; |
| 6657 | char password[256]; |
| 6658 | |
| 6659 | for(i = 0; i < 6; i++) { |
| 6660 | bs = get_bdrv(i); |
| 6661 | if (bs && bdrv_is_encrypted(bs)) { |
| 6662 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); |
| 6663 | for(j = 0; j < 3; j++) { |
| 6664 | monitor_readline("Password: ", |
| 6665 | 1, password, sizeof(password)); |
| 6666 | if (bdrv_set_key(bs, password) == 0) |
| 6667 | break; |
| 6668 | term_printf("invalid password\n"); |
| 6669 | } |
| 6670 | } |
| 6671 | } |
| 6672 | } |
| 6673 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6674 | /* XXX: currently we cannot use simultaneously different CPUs */ |
| 6675 | void register_machines(void) |
| 6676 | { |
| 6677 | #if defined(TARGET_I386) |
| 6678 | qemu_register_machine(&pc_machine); |
bellard | 3dbbdc2 | 2005-11-06 18:20:37 +0000 | [diff] [blame] | 6679 | qemu_register_machine(&isapc_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6680 | #elif defined(TARGET_PPC) |
| 6681 | qemu_register_machine(&heathrow_machine); |
| 6682 | qemu_register_machine(&core99_machine); |
| 6683 | qemu_register_machine(&prep_machine); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 6684 | #elif defined(TARGET_MIPS) |
| 6685 | qemu_register_machine(&mips_machine); |
ths | 5856de8 | 2007-01-15 23:58:11 +0000 | [diff] [blame] | 6686 | qemu_register_machine(&mips_malta_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6687 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6688 | #ifdef TARGET_SPARC64 |
| 6689 | qemu_register_machine(&sun4u_machine); |
| 6690 | #else |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6691 | qemu_register_machine(&sun4m_machine); |
| 6692 | #endif |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6693 | #elif defined(TARGET_ARM) |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 6694 | qemu_register_machine(&integratorcp926_machine); |
| 6695 | qemu_register_machine(&integratorcp1026_machine); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 6696 | qemu_register_machine(&versatilepb_machine); |
pbrook | 1640695 | 2006-04-27 23:15:07 +0000 | [diff] [blame] | 6697 | qemu_register_machine(&versatileab_machine); |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 6698 | qemu_register_machine(&realview_machine); |
bellard | 27c7ca7 | 2006-04-27 21:32:09 +0000 | [diff] [blame] | 6699 | #elif defined(TARGET_SH4) |
| 6700 | qemu_register_machine(&shix_machine); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6701 | #else |
| 6702 | #error unsupported CPU |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6703 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6706 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6707 | struct soundhw soundhw[] = { |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 6708 | #ifdef TARGET_I386 |
| 6709 | { |
| 6710 | "pcspk", |
| 6711 | "PC speaker", |
| 6712 | 0, |
| 6713 | 1, |
| 6714 | { .init_isa = pcspk_audio_init } |
| 6715 | }, |
| 6716 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6717 | { |
| 6718 | "sb16", |
| 6719 | "Creative Sound Blaster 16", |
| 6720 | 0, |
| 6721 | 1, |
| 6722 | { .init_isa = SB16_init } |
| 6723 | }, |
| 6724 | |
| 6725 | #ifdef CONFIG_ADLIB |
| 6726 | { |
| 6727 | "adlib", |
| 6728 | #ifdef HAS_YMF262 |
| 6729 | "Yamaha YMF262 (OPL3)", |
| 6730 | #else |
| 6731 | "Yamaha YM3812 (OPL2)", |
| 6732 | #endif |
| 6733 | 0, |
| 6734 | 1, |
| 6735 | { .init_isa = Adlib_init } |
| 6736 | }, |
| 6737 | #endif |
| 6738 | |
| 6739 | #ifdef CONFIG_GUS |
| 6740 | { |
| 6741 | "gus", |
| 6742 | "Gravis Ultrasound GF1", |
| 6743 | 0, |
| 6744 | 1, |
| 6745 | { .init_isa = GUS_init } |
| 6746 | }, |
| 6747 | #endif |
| 6748 | |
| 6749 | { |
| 6750 | "es1370", |
| 6751 | "ENSONIQ AudioPCI ES1370", |
| 6752 | 0, |
| 6753 | 0, |
| 6754 | { .init_pci = es1370_init } |
| 6755 | }, |
| 6756 | |
| 6757 | { NULL, NULL, 0, 0, { NULL } } |
| 6758 | }; |
| 6759 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6760 | static void select_soundhw (const char *optarg) |
| 6761 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6762 | struct soundhw *c; |
| 6763 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6764 | if (*optarg == '?') { |
| 6765 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6766 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6767 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6768 | for (c = soundhw; c->name; ++c) { |
| 6769 | printf ("%-11s %s\n", c->name, c->descr); |
| 6770 | } |
| 6771 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6772 | exit (*optarg != '?'); |
| 6773 | } |
| 6774 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6775 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6776 | const char *p; |
| 6777 | char *e; |
| 6778 | int bad_card = 0; |
| 6779 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6780 | if (!strcmp (optarg, "all")) { |
| 6781 | for (c = soundhw; c->name; ++c) { |
| 6782 | c->enabled = 1; |
| 6783 | } |
| 6784 | return; |
| 6785 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6786 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6787 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6788 | while (*p) { |
| 6789 | e = strchr (p, ','); |
| 6790 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6791 | |
| 6792 | for (c = soundhw; c->name; ++c) { |
| 6793 | if (!strncmp (c->name, p, l)) { |
| 6794 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6795 | break; |
| 6796 | } |
| 6797 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6798 | |
| 6799 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6800 | if (l > 80) { |
| 6801 | fprintf (stderr, |
| 6802 | "Unknown sound card name (too big to show)\n"); |
| 6803 | } |
| 6804 | else { |
| 6805 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 6806 | (int) l, p); |
| 6807 | } |
| 6808 | bad_card = 1; |
| 6809 | } |
| 6810 | p += l + (e != NULL); |
| 6811 | } |
| 6812 | |
| 6813 | if (bad_card) |
| 6814 | goto show_valid_cards; |
| 6815 | } |
| 6816 | } |
| 6817 | #endif |
| 6818 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6819 | #ifdef _WIN32 |
| 6820 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 6821 | { |
| 6822 | exit(STATUS_CONTROL_C_EXIT); |
| 6823 | return TRUE; |
| 6824 | } |
| 6825 | #endif |
| 6826 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6827 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6828 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6829 | int main(int argc, char **argv) |
| 6830 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6831 | #ifdef CONFIG_GDBSTUB |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 6832 | int use_gdbstub, gdbstub_port; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6833 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6834 | int i, cdrom_index; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 6835 | int snapshot, linux_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 6836 | const char *initrd_filename; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6837 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6838 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6839 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6840 | int cyls, heads, secs, translation; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6841 | char net_clients[MAX_NET_CLIENTS][256]; |
| 6842 | int nb_net_clients; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6843 | int optind; |
| 6844 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6845 | CharDriverState *monitor_hd; |
| 6846 | char monitor_device[128]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6847 | char serial_devices[MAX_SERIAL_PORTS][128]; |
| 6848 | int serial_device_index; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6849 | char parallel_devices[MAX_PARALLEL_PORTS][128]; |
| 6850 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6851 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6852 | QEMUMachine *machine; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 6853 | char usb_devices[MAX_USB_CMDLINE][128]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6854 | int usb_devices_index; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6855 | int fds[2]; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6856 | |
| 6857 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6858 | #ifndef _WIN32 |
| 6859 | { |
| 6860 | struct sigaction act; |
| 6861 | sigfillset(&act.sa_mask); |
| 6862 | act.sa_flags = 0; |
| 6863 | act.sa_handler = SIG_IGN; |
| 6864 | sigaction(SIGPIPE, &act, NULL); |
| 6865 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6866 | #else |
| 6867 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 6868 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 6869 | QEMU to run on a single CPU */ |
| 6870 | { |
| 6871 | HANDLE h; |
| 6872 | DWORD mask, smask; |
| 6873 | int i; |
| 6874 | h = GetCurrentProcess(); |
| 6875 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 6876 | for(i = 0; i < 32; i++) { |
| 6877 | if (mask & (1 << i)) |
| 6878 | break; |
| 6879 | } |
| 6880 | if (i != 32) { |
| 6881 | mask = 1 << i; |
| 6882 | SetProcessAffinityMask(h, mask); |
| 6883 | } |
| 6884 | } |
| 6885 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6886 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6887 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6888 | register_machines(); |
| 6889 | machine = first_machine; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6890 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6891 | for(i = 0; i < MAX_FD; i++) |
| 6892 | fd_filename[i] = NULL; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6893 | for(i = 0; i < MAX_DISKS; i++) |
| 6894 | hd_filename[i] = NULL; |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6895 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6896 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6897 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6898 | use_gdbstub = 0; |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 6899 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6900 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6901 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6902 | nographic = 0; |
| 6903 | kernel_filename = NULL; |
| 6904 | kernel_cmdline = ""; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6905 | #ifdef TARGET_PPC |
| 6906 | cdrom_index = 1; |
| 6907 | #else |
| 6908 | cdrom_index = 2; |
| 6909 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6910 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6911 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6912 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6913 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6914 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
| 6915 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
| 6916 | serial_devices[i][0] = '\0'; |
| 6917 | serial_device_index = 0; |
| 6918 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6919 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
| 6920 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
| 6921 | parallel_devices[i][0] = '\0'; |
| 6922 | parallel_device_index = 0; |
| 6923 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6924 | usb_devices_index = 0; |
| 6925 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6926 | nb_net_clients = 0; |
| 6927 | |
| 6928 | nb_nics = 0; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 6929 | /* default mac address of the first network interface */ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6930 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6931 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6932 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6933 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6934 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6935 | r = argv[optind]; |
| 6936 | if (r[0] != '-') { |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6937 | hd_filename[0] = argv[optind++]; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6938 | } else { |
| 6939 | const QEMUOption *popt; |
| 6940 | |
| 6941 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 6942 | /* Treat --foo the same as -foo. */ |
| 6943 | if (r[1] == '-') |
| 6944 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6945 | popt = qemu_options; |
| 6946 | for(;;) { |
| 6947 | if (!popt->name) { |
| 6948 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 6949 | argv[0], r); |
| 6950 | exit(1); |
| 6951 | } |
| 6952 | if (!strcmp(popt->name, r + 1)) |
| 6953 | break; |
| 6954 | popt++; |
| 6955 | } |
| 6956 | if (popt->flags & HAS_ARG) { |
| 6957 | if (optind >= argc) { |
| 6958 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 6959 | argv[0], r); |
| 6960 | exit(1); |
| 6961 | } |
| 6962 | optarg = argv[optind++]; |
| 6963 | } else { |
| 6964 | optarg = NULL; |
| 6965 | } |
| 6966 | |
| 6967 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6968 | case QEMU_OPTION_M: |
| 6969 | machine = find_machine(optarg); |
| 6970 | if (!machine) { |
| 6971 | QEMUMachine *m; |
| 6972 | printf("Supported machines are:\n"); |
| 6973 | for(m = first_machine; m != NULL; m = m->next) { |
| 6974 | printf("%-10s %s%s\n", |
| 6975 | m->name, m->desc, |
| 6976 | m == first_machine ? " (default)" : ""); |
| 6977 | } |
| 6978 | exit(1); |
| 6979 | } |
| 6980 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6981 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6982 | initrd_filename = optarg; |
| 6983 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6984 | case QEMU_OPTION_hda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6985 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6986 | case QEMU_OPTION_hdc: |
| 6987 | case QEMU_OPTION_hdd: |
| 6988 | { |
| 6989 | int hd_index; |
| 6990 | hd_index = popt->index - QEMU_OPTION_hda; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6991 | hd_filename[hd_index] = optarg; |
| 6992 | if (hd_index == cdrom_index) |
| 6993 | cdrom_index = -1; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6994 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6995 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6996 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6997 | snapshot = 1; |
| 6998 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6999 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7000 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7001 | const char *p; |
| 7002 | p = optarg; |
| 7003 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7004 | if (cyls < 1 || cyls > 16383) |
| 7005 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7006 | if (*p != ',') |
| 7007 | goto chs_fail; |
| 7008 | p++; |
| 7009 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7010 | if (heads < 1 || heads > 16) |
| 7011 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7012 | if (*p != ',') |
| 7013 | goto chs_fail; |
| 7014 | p++; |
| 7015 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7016 | if (secs < 1 || secs > 63) |
| 7017 | goto chs_fail; |
| 7018 | if (*p == ',') { |
| 7019 | p++; |
| 7020 | if (!strcmp(p, "none")) |
| 7021 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 7022 | else if (!strcmp(p, "lba")) |
| 7023 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 7024 | else if (!strcmp(p, "auto")) |
| 7025 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 7026 | else |
| 7027 | goto chs_fail; |
| 7028 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7029 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7030 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 7031 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7032 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7033 | } |
| 7034 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7035 | case QEMU_OPTION_nographic: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7036 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7037 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7038 | nographic = 1; |
| 7039 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7040 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7041 | kernel_filename = optarg; |
| 7042 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7043 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7044 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7045 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7046 | case QEMU_OPTION_cdrom: |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7047 | if (cdrom_index >= 0) { |
| 7048 | hd_filename[cdrom_index] = optarg; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7049 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7050 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7051 | case QEMU_OPTION_boot: |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7052 | boot_device = optarg[0]; |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 7053 | if (boot_device != 'a' && |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7054 | #if defined(TARGET_SPARC) || defined(TARGET_I386) |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 7055 | // Network boot |
| 7056 | boot_device != 'n' && |
| 7057 | #endif |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7058 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7059 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 7060 | exit(1); |
| 7061 | } |
| 7062 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7063 | case QEMU_OPTION_fda: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7064 | fd_filename[0] = optarg; |
| 7065 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7066 | case QEMU_OPTION_fdb: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7067 | fd_filename[1] = optarg; |
| 7068 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7069 | #ifdef TARGET_I386 |
| 7070 | case QEMU_OPTION_no_fd_bootchk: |
| 7071 | fd_bootchk = 0; |
| 7072 | break; |
| 7073 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7074 | case QEMU_OPTION_no_code_copy: |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 7075 | code_copy_enabled = 0; |
| 7076 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7077 | case QEMU_OPTION_net: |
| 7078 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 7079 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7080 | exit(1); |
| 7081 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7082 | pstrcpy(net_clients[nb_net_clients], |
| 7083 | sizeof(net_clients[0]), |
| 7084 | optarg); |
| 7085 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7086 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7087 | #ifdef CONFIG_SLIRP |
| 7088 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7089 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7090 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame^] | 7091 | case QEMU_OPTION_bootp: |
| 7092 | bootp_filename = optarg; |
| 7093 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7094 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7095 | case QEMU_OPTION_smb: |
| 7096 | net_slirp_smb(optarg); |
| 7097 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7098 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7099 | case QEMU_OPTION_redir: |
| 7100 | net_slirp_redir(optarg); |
| 7101 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7102 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7103 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7104 | case QEMU_OPTION_audio_help: |
| 7105 | AUD_help (); |
| 7106 | exit (0); |
| 7107 | break; |
| 7108 | case QEMU_OPTION_soundhw: |
| 7109 | select_soundhw (optarg); |
| 7110 | break; |
| 7111 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7112 | case QEMU_OPTION_h: |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7113 | help(); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7114 | break; |
| 7115 | case QEMU_OPTION_m: |
| 7116 | ram_size = atoi(optarg) * 1024 * 1024; |
| 7117 | if (ram_size <= 0) |
| 7118 | help(); |
| 7119 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
| 7120 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
| 7121 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 7122 | exit(1); |
| 7123 | } |
| 7124 | break; |
| 7125 | case QEMU_OPTION_d: |
| 7126 | { |
| 7127 | int mask; |
| 7128 | CPULogItem *item; |
| 7129 | |
| 7130 | mask = cpu_str_to_log_mask(optarg); |
| 7131 | if (!mask) { |
| 7132 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7133 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 7134 | printf("%-10s %s\n", item->name, item->help); |
| 7135 | } |
| 7136 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7137 | } |
| 7138 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7139 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7140 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7141 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7142 | case QEMU_OPTION_s: |
| 7143 | use_gdbstub = 1; |
| 7144 | break; |
| 7145 | case QEMU_OPTION_p: |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7146 | gdbstub_port = atoi(optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7147 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7148 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7149 | case QEMU_OPTION_L: |
| 7150 | bios_dir = optarg; |
| 7151 | break; |
| 7152 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7153 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7154 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 7155 | case QEMU_OPTION_k: |
| 7156 | keyboard_layout = optarg; |
| 7157 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 7158 | case QEMU_OPTION_localtime: |
| 7159 | rtc_utc = 0; |
| 7160 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7161 | case QEMU_OPTION_cirrusvga: |
| 7162 | cirrus_vga_enabled = 1; |
| 7163 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7164 | case QEMU_OPTION_std_vga: |
| 7165 | cirrus_vga_enabled = 0; |
| 7166 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 7167 | case QEMU_OPTION_g: |
| 7168 | { |
| 7169 | const char *p; |
| 7170 | int w, h, depth; |
| 7171 | p = optarg; |
| 7172 | w = strtol(p, (char **)&p, 10); |
| 7173 | if (w <= 0) { |
| 7174 | graphic_error: |
| 7175 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 7176 | exit(1); |
| 7177 | } |
| 7178 | if (*p != 'x') |
| 7179 | goto graphic_error; |
| 7180 | p++; |
| 7181 | h = strtol(p, (char **)&p, 10); |
| 7182 | if (h <= 0) |
| 7183 | goto graphic_error; |
| 7184 | if (*p == 'x') { |
| 7185 | p++; |
| 7186 | depth = strtol(p, (char **)&p, 10); |
| 7187 | if (depth != 8 && depth != 15 && depth != 16 && |
| 7188 | depth != 24 && depth != 32) |
| 7189 | goto graphic_error; |
| 7190 | } else if (*p == '\0') { |
| 7191 | depth = graphic_depth; |
| 7192 | } else { |
| 7193 | goto graphic_error; |
| 7194 | } |
| 7195 | |
| 7196 | graphic_width = w; |
| 7197 | graphic_height = h; |
| 7198 | graphic_depth = depth; |
| 7199 | } |
| 7200 | break; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7201 | case QEMU_OPTION_echr: |
| 7202 | { |
| 7203 | char *r; |
| 7204 | term_escape_char = strtol(optarg, &r, 0); |
| 7205 | if (r == optarg) |
| 7206 | printf("Bad argument to echr\n"); |
| 7207 | break; |
| 7208 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7209 | case QEMU_OPTION_monitor: |
| 7210 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); |
| 7211 | break; |
| 7212 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7213 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 7214 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 7215 | exit(1); |
| 7216 | } |
| 7217 | pstrcpy(serial_devices[serial_device_index], |
| 7218 | sizeof(serial_devices[0]), optarg); |
| 7219 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7220 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7221 | case QEMU_OPTION_parallel: |
| 7222 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 7223 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 7224 | exit(1); |
| 7225 | } |
| 7226 | pstrcpy(parallel_devices[parallel_device_index], |
| 7227 | sizeof(parallel_devices[0]), optarg); |
| 7228 | parallel_device_index++; |
| 7229 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7230 | case QEMU_OPTION_loadvm: |
| 7231 | loadvm = optarg; |
| 7232 | break; |
| 7233 | case QEMU_OPTION_full_screen: |
| 7234 | full_screen = 1; |
| 7235 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7236 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7237 | case QEMU_OPTION_no_frame: |
| 7238 | no_frame = 1; |
| 7239 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7240 | case QEMU_OPTION_no_quit: |
| 7241 | no_quit = 1; |
| 7242 | break; |
| 7243 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7244 | case QEMU_OPTION_pidfile: |
| 7245 | create_pidfile(optarg); |
| 7246 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7247 | #ifdef TARGET_I386 |
| 7248 | case QEMU_OPTION_win2k_hack: |
| 7249 | win2k_install_hack = 1; |
| 7250 | break; |
| 7251 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7252 | #ifdef USE_KQEMU |
| 7253 | case QEMU_OPTION_no_kqemu: |
| 7254 | kqemu_allowed = 0; |
| 7255 | break; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7256 | case QEMU_OPTION_kernel_kqemu: |
| 7257 | kqemu_allowed = 2; |
| 7258 | break; |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7259 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 7260 | case QEMU_OPTION_usb: |
| 7261 | usb_enabled = 1; |
| 7262 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7263 | case QEMU_OPTION_usbdevice: |
| 7264 | usb_enabled = 1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7265 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7266 | fprintf(stderr, "Too many USB devices\n"); |
| 7267 | exit(1); |
| 7268 | } |
| 7269 | pstrcpy(usb_devices[usb_devices_index], |
| 7270 | sizeof(usb_devices[usb_devices_index]), |
| 7271 | optarg); |
| 7272 | usb_devices_index++; |
| 7273 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7274 | case QEMU_OPTION_smp: |
| 7275 | smp_cpus = atoi(optarg); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 7276 | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7277 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 7278 | exit(1); |
| 7279 | } |
| 7280 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7281 | case QEMU_OPTION_vnc: |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7282 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7283 | break; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7284 | case QEMU_OPTION_no_acpi: |
| 7285 | acpi_enabled = 0; |
| 7286 | break; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7287 | case QEMU_OPTION_no_reboot: |
| 7288 | no_reboot = 1; |
| 7289 | break; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7290 | case QEMU_OPTION_daemonize: |
| 7291 | daemonize = 1; |
| 7292 | break; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7293 | case QEMU_OPTION_option_rom: |
| 7294 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 7295 | fprintf(stderr, "Too many option ROMs\n"); |
| 7296 | exit(1); |
| 7297 | } |
| 7298 | option_rom[nb_option_roms] = optarg; |
| 7299 | nb_option_roms++; |
| 7300 | break; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 7301 | case QEMU_OPTION_semihosting: |
| 7302 | semihosting_enabled = 1; |
| 7303 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7304 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7305 | } |
| 7306 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7307 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7308 | #ifndef _WIN32 |
| 7309 | if (daemonize && !nographic && vnc_display == NULL) { |
| 7310 | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n"); |
| 7311 | daemonize = 0; |
| 7312 | } |
| 7313 | |
| 7314 | if (daemonize) { |
| 7315 | pid_t pid; |
| 7316 | |
| 7317 | if (pipe(fds) == -1) |
| 7318 | exit(1); |
| 7319 | |
| 7320 | pid = fork(); |
| 7321 | if (pid > 0) { |
| 7322 | uint8_t status; |
| 7323 | ssize_t len; |
| 7324 | |
| 7325 | close(fds[1]); |
| 7326 | |
| 7327 | again: |
| 7328 | len = read(fds[0], &status, 1); |
| 7329 | if (len == -1 && (errno == EINTR)) |
| 7330 | goto again; |
| 7331 | |
| 7332 | if (len != 1 || status != 0) |
| 7333 | exit(1); |
| 7334 | else |
| 7335 | exit(0); |
| 7336 | } else if (pid < 0) |
| 7337 | exit(1); |
| 7338 | |
| 7339 | setsid(); |
| 7340 | |
| 7341 | pid = fork(); |
| 7342 | if (pid > 0) |
| 7343 | exit(0); |
| 7344 | else if (pid < 0) |
| 7345 | exit(1); |
| 7346 | |
| 7347 | umask(027); |
| 7348 | chdir("/"); |
| 7349 | |
| 7350 | signal(SIGTSTP, SIG_IGN); |
| 7351 | signal(SIGTTOU, SIG_IGN); |
| 7352 | signal(SIGTTIN, SIG_IGN); |
| 7353 | } |
| 7354 | #endif |
| 7355 | |
bellard | ff3fbb3 | 2006-01-08 10:53:14 +0000 | [diff] [blame] | 7356 | #ifdef USE_KQEMU |
| 7357 | if (smp_cpus > 1) |
| 7358 | kqemu_allowed = 0; |
| 7359 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7360 | linux_boot = (kernel_filename != NULL); |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 7361 | |
| 7362 | if (!linux_boot && |
ths | d84fe7a | 2007-02-19 01:10:26 +0000 | [diff] [blame] | 7363 | boot_device != 'n' && |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7364 | hd_filename[0] == '\0' && |
| 7365 | (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7366 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7367 | help(); |
| 7368 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7369 | /* boot to floppy or the default cd if no hard disk defined yet */ |
| 7370 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 7371 | if (fd_filename[0] != '\0') |
| 7372 | boot_device = 'a'; |
| 7373 | else |
| 7374 | boot_device = 'd'; |
| 7375 | } |
| 7376 | |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 7377 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7378 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7379 | init_timers(); |
| 7380 | init_timer_alarm(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7381 | qemu_aio_init(); |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7382 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7383 | #ifdef _WIN32 |
| 7384 | socket_init(); |
| 7385 | #endif |
| 7386 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7387 | /* init network clients */ |
| 7388 | if (nb_net_clients == 0) { |
| 7389 | /* if no clients, we use a default config */ |
| 7390 | pstrcpy(net_clients[0], sizeof(net_clients[0]), |
| 7391 | "nic"); |
| 7392 | pstrcpy(net_clients[1], sizeof(net_clients[0]), |
| 7393 | "user"); |
| 7394 | nb_net_clients = 2; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7397 | for(i = 0;i < nb_net_clients; i++) { |
| 7398 | if (net_client_init(net_clients[i]) < 0) |
| 7399 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7400 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 7401 | |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7402 | #ifdef TARGET_I386 |
| 7403 | if (boot_device == 'n') { |
| 7404 | for (i = 0; i < nb_nics; i++) { |
| 7405 | const char *model = nd_table[i].model; |
| 7406 | char buf[1024]; |
| 7407 | if (model == NULL) |
| 7408 | model = "ne2k_pci"; |
| 7409 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
| 7410 | if (get_image_size(buf) > 0) { |
| 7411 | option_rom[nb_option_roms] = strdup(buf); |
| 7412 | nb_option_roms++; |
| 7413 | break; |
| 7414 | } |
| 7415 | } |
| 7416 | if (i == nb_nics) { |
| 7417 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
| 7418 | exit(1); |
| 7419 | } |
| 7420 | boot_device = 'c'; /* to prevent confusion by the BIOS */ |
| 7421 | } |
| 7422 | #endif |
| 7423 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7424 | /* init the memory */ |
bellard | 970ac5a | 2007-02-08 23:09:59 +0000 | [diff] [blame] | 7425 | phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7426 | |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7427 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7428 | if (!phys_ram_base) { |
| 7429 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7430 | exit(1); |
| 7431 | } |
| 7432 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7433 | /* we always create the cdrom drive, even if no disk is there */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7434 | bdrv_init(); |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7435 | if (cdrom_index >= 0) { |
| 7436 | bs_table[cdrom_index] = bdrv_new("cdrom"); |
| 7437 | bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7440 | /* open the virtual block devices */ |
| 7441 | for(i = 0; i < MAX_DISKS; i++) { |
| 7442 | if (hd_filename[i]) { |
| 7443 | if (!bs_table[i]) { |
| 7444 | char buf[64]; |
| 7445 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 7446 | bs_table[i] = bdrv_new(buf); |
| 7447 | } |
| 7448 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
| 7449 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", |
| 7450 | hd_filename[i]); |
| 7451 | exit(1); |
| 7452 | } |
| 7453 | if (i == 0 && cyls != 0) { |
| 7454 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
| 7455 | bdrv_set_translation_hint(bs_table[i], translation); |
| 7456 | } |
| 7457 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | /* we always create at least one floppy disk */ |
| 7461 | fd_table[0] = bdrv_new("fda"); |
| 7462 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 7463 | |
| 7464 | for(i = 0; i < MAX_FD; i++) { |
| 7465 | if (fd_filename[i]) { |
| 7466 | if (!fd_table[i]) { |
| 7467 | char buf[64]; |
| 7468 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 7469 | fd_table[i] = bdrv_new(buf); |
| 7470 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 7471 | } |
| 7472 | if (fd_filename[i] != '\0') { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7473 | if (bdrv_open(fd_table[i], fd_filename[i], |
| 7474 | snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7475 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7476 | fd_filename[i]); |
| 7477 | exit(1); |
| 7478 | } |
| 7479 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7480 | } |
| 7481 | } |
| 7482 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 7483 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
| 7484 | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7485 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7486 | init_ioports(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7487 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7488 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7489 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7490 | dumb_display_init(ds); |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7491 | } else if (vnc_display != NULL) { |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7492 | vnc_display_init(ds, vnc_display); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7493 | } else { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7494 | #if defined(CONFIG_SDL) |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7495 | sdl_display_init(ds, full_screen, no_frame); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7496 | #elif defined(CONFIG_COCOA) |
| 7497 | cocoa_display_init(ds, full_screen); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7498 | #else |
| 7499 | dumb_display_init(ds); |
| 7500 | #endif |
| 7501 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7502 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7503 | /* Maintain compatibility with multiple stdio monitors */ |
| 7504 | if (!strcmp(monitor_device,"stdio")) { |
| 7505 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 7506 | if (!strcmp(serial_devices[i],"mon:stdio")) { |
| 7507 | monitor_device[0] = '\0'; |
| 7508 | break; |
| 7509 | } else if (!strcmp(serial_devices[i],"stdio")) { |
| 7510 | monitor_device[0] = '\0'; |
| 7511 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio"); |
| 7512 | break; |
| 7513 | } |
| 7514 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7515 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7516 | if (monitor_device[0] != '\0') { |
| 7517 | monitor_hd = qemu_chr_open(monitor_device); |
| 7518 | if (!monitor_hd) { |
| 7519 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 7520 | exit(1); |
| 7521 | } |
| 7522 | monitor_init(monitor_hd, !nographic); |
| 7523 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7524 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7525 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7526 | const char *devname = serial_devices[i]; |
| 7527 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7528 | serial_hds[i] = qemu_chr_open(devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7529 | if (!serial_hds[i]) { |
| 7530 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7531 | devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7532 | exit(1); |
| 7533 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7534 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7535 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7536 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7537 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7538 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7539 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7540 | const char *devname = parallel_devices[i]; |
| 7541 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7542 | parallel_hds[i] = qemu_chr_open(devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7543 | if (!parallel_hds[i]) { |
| 7544 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7545 | devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7546 | exit(1); |
| 7547 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7548 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7549 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7553 | machine->init(ram_size, vga_ram_size, boot_device, |
| 7554 | ds, fd_filename, snapshot, |
| 7555 | kernel_filename, kernel_cmdline, initrd_filename); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 7556 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7557 | /* init USB devices */ |
| 7558 | if (usb_enabled) { |
| 7559 | for(i = 0; i < usb_devices_index; i++) { |
| 7560 | if (usb_device_add(usb_devices[i]) < 0) { |
| 7561 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 7562 | usb_devices[i]); |
| 7563 | } |
| 7564 | } |
| 7565 | } |
| 7566 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7567 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
| 7568 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7569 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7570 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7571 | if (use_gdbstub) { |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7572 | /* XXX: use standard host:port notation and modify options |
| 7573 | accordingly. */ |
| 7574 | if (gdbserver_start_port(gdbstub_port) < 0) { |
| 7575 | fprintf(stderr, "qemu: could not open gdbstub device on port '%d'\n", |
| 7576 | gdbstub_port); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7577 | exit(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7578 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7579 | } else |
| 7580 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7581 | if (loadvm) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 7582 | do_loadvm(loadvm); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7583 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7584 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7585 | /* XXX: simplify init */ |
| 7586 | read_passwords(); |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7587 | if (autostart) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7588 | vm_start(); |
| 7589 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7590 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 7591 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7592 | if (daemonize) { |
| 7593 | uint8_t status = 0; |
| 7594 | ssize_t len; |
| 7595 | int fd; |
| 7596 | |
| 7597 | again1: |
| 7598 | len = write(fds[1], &status, 1); |
| 7599 | if (len == -1 && (errno == EINTR)) |
| 7600 | goto again1; |
| 7601 | |
| 7602 | if (len != 1) |
| 7603 | exit(1); |
| 7604 | |
| 7605 | fd = open("/dev/null", O_RDWR); |
| 7606 | if (fd == -1) |
| 7607 | exit(1); |
| 7608 | |
| 7609 | dup2(fd, 0); |
| 7610 | dup2(fd, 1); |
| 7611 | dup2(fd, 2); |
| 7612 | |
| 7613 | close(fd); |
| 7614 | } |
| 7615 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7616 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 7617 | quit_timers(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7618 | return 0; |
| 7619 | } |