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 | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 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> |
| 32 | |
| 33 | #ifndef _WIN32 |
| 34 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 35 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 36 | #include <termios.h> |
| 37 | #include <sys/poll.h> |
| 38 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 39 | #include <sys/ioctl.h> |
| 40 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 41 | #include <netinet/in.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 42 | #include <dirent.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 43 | #ifdef _BSD |
| 44 | #include <sys/stat.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 45 | #ifndef __APPLE__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 46 | #include <libutil.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 47 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 48 | #else |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 49 | #include <linux/if.h> |
| 50 | #include <linux/if_tun.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 51 | #include <pty.h> |
| 52 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 53 | #include <linux/rtc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 54 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 55 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 56 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 57 | #if defined(CONFIG_SLIRP) |
| 58 | #include "libslirp.h" |
| 59 | #endif |
| 60 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 61 | #ifdef _WIN32 |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 62 | #include <malloc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 63 | #include <sys/timeb.h> |
| 64 | #include <windows.h> |
| 65 | #define getopt_long_only getopt_long |
| 66 | #define memalign(align, size) malloc(size) |
| 67 | #endif |
| 68 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 69 | #ifdef CONFIG_SDL |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 70 | #ifdef __APPLE__ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 71 | #include <SDL/SDL.h> |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 72 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 73 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 74 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_COCOA |
| 76 | #undef main |
| 77 | #define main qemu_main |
| 78 | #endif /* CONFIG_COCOA */ |
| 79 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 80 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 81 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 82 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 83 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 84 | //#define DO_TB_FLUSH |
| 85 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 86 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 87 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 88 | //#define DEBUG_UNUSED_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 89 | //#define DEBUG_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 90 | |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 91 | #if !defined(CONFIG_SOFTMMU) |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 92 | #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 93 | #else |
| 94 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
| 95 | #endif |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 96 | |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 97 | #ifdef TARGET_PPC |
| 98 | #define DEFAULT_RAM_SIZE 144 |
| 99 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 100 | #define DEFAULT_RAM_SIZE 128 |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 101 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 102 | /* in ms */ |
| 103 | #define GUI_REFRESH_INTERVAL 30 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 104 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 105 | /* XXX: use a two level table to limit memory usage */ |
| 106 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 107 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 108 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 109 | char phys_ram_file[1024]; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 110 | CPUState *global_env; |
| 111 | CPUState *cpu_single_env; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 112 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 113 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 114 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 115 | BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 116 | int vga_ram_size; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 117 | int bios_size; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 118 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 119 | int nographic; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 120 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 121 | int64_t ticks_per_sec; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 122 | int boot_device = 'c'; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 123 | int ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 124 | static char network_script[1024]; |
| 125 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 126 | int nb_nics; |
| 127 | NetDriverState nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 128 | QEMUTimer *gui_timer; |
| 129 | int vm_running; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 130 | #ifdef HAS_AUDIO |
bellard | aaaa7df | 2004-04-26 20:56:53 +0000 | [diff] [blame] | 131 | int audio_enabled = 0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 132 | int sb16_enabled = 0; |
| 133 | int adlib_enabled = 0; |
| 134 | int gus_enabled = 0; |
| 135 | int es1370_enabled = 0; |
| 136 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 137 | int rtc_utc = 1; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 138 | int cirrus_vga_enabled = 1; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 139 | #ifdef TARGET_SPARC |
| 140 | int graphic_width = 1024; |
| 141 | int graphic_height = 768; |
| 142 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 143 | int graphic_width = 800; |
| 144 | int graphic_height = 600; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 145 | #endif |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 146 | int graphic_depth = 15; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 147 | int full_screen = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 148 | TextConsole *vga_console; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 149 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 150 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 151 | #ifdef TARGET_I386 |
| 152 | int win2k_install_hack = 0; |
| 153 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 154 | int usb_enabled = 0; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 155 | USBPort *vm_usb_ports[MAX_VM_USB_PORTS]; |
| 156 | USBDevice *vm_usb_hub; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 157 | |
| 158 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 159 | /* x86 ISA bus support */ |
| 160 | |
| 161 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 162 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 163 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 164 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 165 | { |
| 166 | #ifdef DEBUG_UNUSED_IOPORT |
| 167 | fprintf(stderr, "inb: port=0x%04x\n", address); |
| 168 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 169 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 170 | } |
| 171 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 172 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 173 | { |
| 174 | #ifdef DEBUG_UNUSED_IOPORT |
| 175 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 180 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 181 | { |
| 182 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 183 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 184 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 185 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 186 | return data; |
| 187 | } |
| 188 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 189 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 190 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 191 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 192 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 193 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 196 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 197 | { |
| 198 | #ifdef DEBUG_UNUSED_IOPORT |
| 199 | fprintf(stderr, "inl: port=0x%04x\n", address); |
| 200 | #endif |
| 201 | return 0xffffffff; |
| 202 | } |
| 203 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 204 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 205 | { |
| 206 | #ifdef DEBUG_UNUSED_IOPORT |
| 207 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); |
| 208 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void init_ioports(void) |
| 212 | { |
| 213 | int i; |
| 214 | |
| 215 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 216 | ioport_read_table[0][i] = default_ioport_readb; |
| 217 | ioport_write_table[0][i] = default_ioport_writeb; |
| 218 | ioport_read_table[1][i] = default_ioport_readw; |
| 219 | ioport_write_table[1][i] = default_ioport_writew; |
| 220 | ioport_read_table[2][i] = default_ioport_readl; |
| 221 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 225 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 226 | int register_ioport_read(int start, int length, int size, |
| 227 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 228 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 229 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 230 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 231 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 232 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 233 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 234 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 235 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 236 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 237 | } else { |
| 238 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 239 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 240 | } |
| 241 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 242 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 243 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 244 | hw_error("register_ioport_read: invalid opaque"); |
| 245 | ioport_opaque[i] = opaque; |
| 246 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 250 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 251 | int register_ioport_write(int start, int length, int size, |
| 252 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 253 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 254 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 255 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 256 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 257 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 258 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 259 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 260 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 261 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 262 | } else { |
| 263 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 264 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 265 | } |
| 266 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 267 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 268 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 269 | hw_error("register_ioport_read: invalid opaque"); |
| 270 | ioport_opaque[i] = opaque; |
| 271 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 275 | void isa_unassign_ioport(int start, int length) |
| 276 | { |
| 277 | int i; |
| 278 | |
| 279 | for(i = start; i < start + length; i++) { |
| 280 | ioport_read_table[0][i] = default_ioport_readb; |
| 281 | ioport_read_table[1][i] = default_ioport_readw; |
| 282 | ioport_read_table[2][i] = default_ioport_readl; |
| 283 | |
| 284 | ioport_write_table[0][i] = default_ioport_writeb; |
| 285 | ioport_write_table[1][i] = default_ioport_writew; |
| 286 | ioport_write_table[2][i] = default_ioport_writel; |
| 287 | } |
| 288 | } |
| 289 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 290 | /***********************************************************/ |
| 291 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 292 | void pstrcpy(char *buf, int buf_size, const char *str) |
| 293 | { |
| 294 | int c; |
| 295 | char *q = buf; |
| 296 | |
| 297 | if (buf_size <= 0) |
| 298 | return; |
| 299 | |
| 300 | for(;;) { |
| 301 | c = *str++; |
| 302 | if (c == 0 || q >= buf + buf_size - 1) |
| 303 | break; |
| 304 | *q++ = c; |
| 305 | } |
| 306 | *q = '\0'; |
| 307 | } |
| 308 | |
| 309 | /* strcat and truncate. */ |
| 310 | char *pstrcat(char *buf, int buf_size, const char *s) |
| 311 | { |
| 312 | int len; |
| 313 | len = strlen(buf); |
| 314 | if (len < buf_size) |
| 315 | pstrcpy(buf + len, buf_size - len, s); |
| 316 | return buf; |
| 317 | } |
| 318 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 319 | int strstart(const char *str, const char *val, const char **ptr) |
| 320 | { |
| 321 | const char *p, *q; |
| 322 | p = str; |
| 323 | q = val; |
| 324 | while (*q != '\0') { |
| 325 | if (*p != *q) |
| 326 | return 0; |
| 327 | p++; |
| 328 | q++; |
| 329 | } |
| 330 | if (ptr) |
| 331 | *ptr = p; |
| 332 | return 1; |
| 333 | } |
| 334 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 335 | /* return the size or -1 if error */ |
bellard | 7587cf4 | 2004-06-20 13:43:27 +0000 | [diff] [blame] | 336 | int get_image_size(const char *filename) |
| 337 | { |
| 338 | int fd, size; |
| 339 | fd = open(filename, O_RDONLY | O_BINARY); |
| 340 | if (fd < 0) |
| 341 | return -1; |
| 342 | size = lseek(fd, 0, SEEK_END); |
| 343 | close(fd); |
| 344 | return size; |
| 345 | } |
| 346 | |
| 347 | /* return the size or -1 if error */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 348 | int load_image(const char *filename, uint8_t *addr) |
| 349 | { |
| 350 | int fd, size; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 351 | fd = open(filename, O_RDONLY | O_BINARY); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 352 | if (fd < 0) |
| 353 | return -1; |
| 354 | size = lseek(fd, 0, SEEK_END); |
| 355 | lseek(fd, 0, SEEK_SET); |
| 356 | if (read(fd, addr, size) != size) { |
| 357 | close(fd); |
| 358 | return -1; |
| 359 | } |
| 360 | close(fd); |
| 361 | return size; |
| 362 | } |
| 363 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 364 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 365 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 366 | #ifdef DEBUG_IOPORT |
| 367 | if (loglevel & CPU_LOG_IOPORT) |
| 368 | fprintf(logfile, "outb: %04x %02x\n", addr, val); |
| 369 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 370 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 371 | } |
| 372 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 373 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 374 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 375 | #ifdef DEBUG_IOPORT |
| 376 | if (loglevel & CPU_LOG_IOPORT) |
| 377 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
| 378 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 379 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 380 | } |
| 381 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 382 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 383 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 384 | #ifdef DEBUG_IOPORT |
| 385 | if (loglevel & CPU_LOG_IOPORT) |
| 386 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 387 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 388 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 389 | } |
| 390 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 391 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 392 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 393 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 394 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 395 | #ifdef DEBUG_IOPORT |
| 396 | if (loglevel & CPU_LOG_IOPORT) |
| 397 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 398 | #endif |
| 399 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 400 | } |
| 401 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 402 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 403 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 404 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 405 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 406 | #ifdef DEBUG_IOPORT |
| 407 | if (loglevel & CPU_LOG_IOPORT) |
| 408 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 409 | #endif |
| 410 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 413 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 414 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 415 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 416 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 417 | #ifdef DEBUG_IOPORT |
| 418 | if (loglevel & CPU_LOG_IOPORT) |
| 419 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 420 | #endif |
| 421 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 425 | void hw_error(const char *fmt, ...) |
| 426 | { |
| 427 | va_list ap; |
| 428 | |
| 429 | va_start(ap, fmt); |
| 430 | fprintf(stderr, "qemu: hardware error: "); |
| 431 | vfprintf(stderr, fmt, ap); |
| 432 | fprintf(stderr, "\n"); |
| 433 | #ifdef TARGET_I386 |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 434 | cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 435 | #else |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 436 | cpu_dump_state(global_env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 437 | #endif |
| 438 | va_end(ap); |
| 439 | abort(); |
| 440 | } |
| 441 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 442 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 443 | /* keyboard/mouse */ |
| 444 | |
| 445 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 446 | static void *qemu_put_kbd_event_opaque; |
| 447 | static QEMUPutMouseEvent *qemu_put_mouse_event; |
| 448 | static void *qemu_put_mouse_event_opaque; |
| 449 | |
| 450 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 451 | { |
| 452 | qemu_put_kbd_event_opaque = opaque; |
| 453 | qemu_put_kbd_event = func; |
| 454 | } |
| 455 | |
| 456 | void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque) |
| 457 | { |
| 458 | qemu_put_mouse_event_opaque = opaque; |
| 459 | qemu_put_mouse_event = func; |
| 460 | } |
| 461 | |
| 462 | void kbd_put_keycode(int keycode) |
| 463 | { |
| 464 | if (qemu_put_kbd_event) { |
| 465 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 470 | { |
| 471 | if (qemu_put_mouse_event) { |
| 472 | qemu_put_mouse_event(qemu_put_mouse_event_opaque, |
| 473 | dx, dy, dz, buttons_state); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 478 | /* timers */ |
| 479 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 480 | #if defined(__powerpc__) |
| 481 | |
| 482 | static inline uint32_t get_tbl(void) |
| 483 | { |
| 484 | uint32_t tbl; |
| 485 | asm volatile("mftb %0" : "=r" (tbl)); |
| 486 | return tbl; |
| 487 | } |
| 488 | |
| 489 | static inline uint32_t get_tbu(void) |
| 490 | { |
| 491 | uint32_t tbl; |
| 492 | asm volatile("mftbu %0" : "=r" (tbl)); |
| 493 | return tbl; |
| 494 | } |
| 495 | |
| 496 | int64_t cpu_get_real_ticks(void) |
| 497 | { |
| 498 | uint32_t l, h, h1; |
| 499 | /* NOTE: we test if wrapping has occurred */ |
| 500 | do { |
| 501 | h = get_tbu(); |
| 502 | l = get_tbl(); |
| 503 | h1 = get_tbu(); |
| 504 | } while (h != h1); |
| 505 | return ((int64_t)h << 32) | l; |
| 506 | } |
| 507 | |
| 508 | #elif defined(__i386__) |
| 509 | |
| 510 | int64_t cpu_get_real_ticks(void) |
| 511 | { |
| 512 | int64_t val; |
bellard | e463b58 | 2004-04-02 20:57:58 +0000 | [diff] [blame] | 513 | asm volatile ("rdtsc" : "=A" (val)); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 514 | return val; |
| 515 | } |
| 516 | |
bellard | 1115dde | 2004-04-25 18:57:49 +0000 | [diff] [blame] | 517 | #elif defined(__x86_64__) |
| 518 | |
| 519 | int64_t cpu_get_real_ticks(void) |
| 520 | { |
| 521 | uint32_t low,high; |
| 522 | int64_t val; |
| 523 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
| 524 | val = high; |
| 525 | val <<= 32; |
| 526 | val |= low; |
| 527 | return val; |
| 528 | } |
| 529 | |
bellard | b8076a7 | 2005-04-07 22:20:31 +0000 | [diff] [blame] | 530 | #elif defined(__ia64) |
| 531 | |
| 532 | int64_t cpu_get_real_ticks(void) |
| 533 | { |
| 534 | int64_t val; |
| 535 | asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory"); |
| 536 | return val; |
| 537 | } |
| 538 | |
bellard | 90cb949 | 2005-07-24 15:11:38 +0000 | [diff] [blame] | 539 | #elif defined(__s390__) |
| 540 | |
| 541 | int64_t cpu_get_real_ticks(void) |
| 542 | { |
| 543 | int64_t val; |
| 544 | asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc"); |
| 545 | return val; |
| 546 | } |
| 547 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 548 | #else |
| 549 | #error unsupported CPU |
| 550 | #endif |
| 551 | |
| 552 | static int64_t cpu_ticks_offset; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 553 | static int cpu_ticks_enabled; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 554 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 555 | static inline int64_t cpu_get_ticks(void) |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 556 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 557 | if (!cpu_ticks_enabled) { |
| 558 | return cpu_ticks_offset; |
| 559 | } else { |
| 560 | return cpu_get_real_ticks() + cpu_ticks_offset; |
| 561 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* enable cpu_get_ticks() */ |
| 565 | void cpu_enable_ticks(void) |
| 566 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 567 | if (!cpu_ticks_enabled) { |
| 568 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 569 | cpu_ticks_enabled = 1; |
| 570 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 574 | cpu_get_ticks() after that. */ |
| 575 | void cpu_disable_ticks(void) |
| 576 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 577 | if (cpu_ticks_enabled) { |
| 578 | cpu_ticks_offset = cpu_get_ticks(); |
| 579 | cpu_ticks_enabled = 0; |
| 580 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 581 | } |
| 582 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 583 | static int64_t get_clock(void) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 584 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 585 | #ifdef _WIN32 |
| 586 | struct _timeb tb; |
| 587 | _ftime(&tb); |
| 588 | return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000; |
| 589 | #else |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 590 | struct timeval tv; |
| 591 | gettimeofday(&tv, NULL); |
| 592 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 593 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 594 | } |
| 595 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 596 | void cpu_calibrate_ticks(void) |
| 597 | { |
| 598 | int64_t usec, ticks; |
| 599 | |
| 600 | usec = get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 601 | ticks = cpu_get_real_ticks(); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 602 | #ifdef _WIN32 |
| 603 | Sleep(50); |
| 604 | #else |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 605 | usleep(50 * 1000); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 606 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 607 | usec = get_clock() - usec; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 608 | ticks = cpu_get_real_ticks() - ticks; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 609 | ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
| 610 | } |
| 611 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 612 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 613 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 614 | { |
| 615 | union { |
| 616 | uint64_t ll; |
| 617 | struct { |
| 618 | #ifdef WORDS_BIGENDIAN |
| 619 | uint32_t high, low; |
| 620 | #else |
| 621 | uint32_t low, high; |
| 622 | #endif |
| 623 | } l; |
| 624 | } u, res; |
| 625 | uint64_t rl, rh; |
| 626 | |
| 627 | u.ll = a; |
| 628 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 629 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 630 | rh += (rl >> 32); |
| 631 | res.l.high = rh / c; |
| 632 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 633 | return res.ll; |
| 634 | } |
| 635 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 636 | #define QEMU_TIMER_REALTIME 0 |
| 637 | #define QEMU_TIMER_VIRTUAL 1 |
| 638 | |
| 639 | struct QEMUClock { |
| 640 | int type; |
| 641 | /* XXX: add frequency */ |
| 642 | }; |
| 643 | |
| 644 | struct QEMUTimer { |
| 645 | QEMUClock *clock; |
| 646 | int64_t expire_time; |
| 647 | QEMUTimerCB *cb; |
| 648 | void *opaque; |
| 649 | struct QEMUTimer *next; |
| 650 | }; |
| 651 | |
| 652 | QEMUClock *rt_clock; |
| 653 | QEMUClock *vm_clock; |
| 654 | |
| 655 | static QEMUTimer *active_timers[2]; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 656 | #ifdef _WIN32 |
| 657 | static MMRESULT timerID; |
| 658 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 659 | /* frequency of the times() clock tick */ |
| 660 | static int timer_freq; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 661 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 662 | |
| 663 | QEMUClock *qemu_new_clock(int type) |
| 664 | { |
| 665 | QEMUClock *clock; |
| 666 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 667 | if (!clock) |
| 668 | return NULL; |
| 669 | clock->type = type; |
| 670 | return clock; |
| 671 | } |
| 672 | |
| 673 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 674 | { |
| 675 | QEMUTimer *ts; |
| 676 | |
| 677 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 678 | ts->clock = clock; |
| 679 | ts->cb = cb; |
| 680 | ts->opaque = opaque; |
| 681 | return ts; |
| 682 | } |
| 683 | |
| 684 | void qemu_free_timer(QEMUTimer *ts) |
| 685 | { |
| 686 | qemu_free(ts); |
| 687 | } |
| 688 | |
| 689 | /* stop a timer, but do not dealloc it */ |
| 690 | void qemu_del_timer(QEMUTimer *ts) |
| 691 | { |
| 692 | QEMUTimer **pt, *t; |
| 693 | |
| 694 | /* NOTE: this code must be signal safe because |
| 695 | qemu_timer_expired() can be called from a signal. */ |
| 696 | pt = &active_timers[ts->clock->type]; |
| 697 | for(;;) { |
| 698 | t = *pt; |
| 699 | if (!t) |
| 700 | break; |
| 701 | if (t == ts) { |
| 702 | *pt = t->next; |
| 703 | break; |
| 704 | } |
| 705 | pt = &t->next; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* modify the current timer so that it will be fired when current_time |
| 710 | >= expire_time. The corresponding callback will be called. */ |
| 711 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 712 | { |
| 713 | QEMUTimer **pt, *t; |
| 714 | |
| 715 | qemu_del_timer(ts); |
| 716 | |
| 717 | /* add the timer in the sorted list */ |
| 718 | /* NOTE: this code must be signal safe because |
| 719 | qemu_timer_expired() can be called from a signal. */ |
| 720 | pt = &active_timers[ts->clock->type]; |
| 721 | for(;;) { |
| 722 | t = *pt; |
| 723 | if (!t) |
| 724 | break; |
| 725 | if (t->expire_time > expire_time) |
| 726 | break; |
| 727 | pt = &t->next; |
| 728 | } |
| 729 | ts->expire_time = expire_time; |
| 730 | ts->next = *pt; |
| 731 | *pt = ts; |
| 732 | } |
| 733 | |
| 734 | int qemu_timer_pending(QEMUTimer *ts) |
| 735 | { |
| 736 | QEMUTimer *t; |
| 737 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 738 | if (t == ts) |
| 739 | return 1; |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 745 | { |
| 746 | if (!timer_head) |
| 747 | return 0; |
| 748 | return (timer_head->expire_time <= current_time); |
| 749 | } |
| 750 | |
| 751 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 752 | { |
| 753 | QEMUTimer *ts; |
| 754 | |
| 755 | for(;;) { |
| 756 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 757 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 758 | break; |
| 759 | /* remove timer from the list before calling the callback */ |
| 760 | *ptimer_head = ts->next; |
| 761 | ts->next = NULL; |
| 762 | |
| 763 | /* run the callback (the timer list can be modified) */ |
| 764 | ts->cb(ts->opaque); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | int64_t qemu_get_clock(QEMUClock *clock) |
| 769 | { |
| 770 | switch(clock->type) { |
| 771 | case QEMU_TIMER_REALTIME: |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 772 | #ifdef _WIN32 |
| 773 | return GetTickCount(); |
| 774 | #else |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 775 | { |
| 776 | struct tms tp; |
| 777 | |
| 778 | /* Note that using gettimeofday() is not a good solution |
| 779 | for timers because its value change when the date is |
| 780 | modified. */ |
| 781 | if (timer_freq == 100) { |
| 782 | return times(&tp) * 10; |
| 783 | } else { |
| 784 | return ((int64_t)times(&tp) * 1000) / timer_freq; |
| 785 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 786 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 787 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 788 | default: |
| 789 | case QEMU_TIMER_VIRTUAL: |
| 790 | return cpu_get_ticks(); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /* save a timer */ |
| 795 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 796 | { |
| 797 | uint64_t expire_time; |
| 798 | |
| 799 | if (qemu_timer_pending(ts)) { |
| 800 | expire_time = ts->expire_time; |
| 801 | } else { |
| 802 | expire_time = -1; |
| 803 | } |
| 804 | qemu_put_be64(f, expire_time); |
| 805 | } |
| 806 | |
| 807 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 808 | { |
| 809 | uint64_t expire_time; |
| 810 | |
| 811 | expire_time = qemu_get_be64(f); |
| 812 | if (expire_time != -1) { |
| 813 | qemu_mod_timer(ts, expire_time); |
| 814 | } else { |
| 815 | qemu_del_timer(ts); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | static void timer_save(QEMUFile *f, void *opaque) |
| 820 | { |
| 821 | if (cpu_ticks_enabled) { |
| 822 | hw_error("cannot save state if virtual timers are running"); |
| 823 | } |
| 824 | qemu_put_be64s(f, &cpu_ticks_offset); |
| 825 | qemu_put_be64s(f, &ticks_per_sec); |
| 826 | } |
| 827 | |
| 828 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 829 | { |
| 830 | if (version_id != 1) |
| 831 | return -EINVAL; |
| 832 | if (cpu_ticks_enabled) { |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | qemu_get_be64s(f, &cpu_ticks_offset); |
| 836 | qemu_get_be64s(f, &ticks_per_sec); |
| 837 | return 0; |
| 838 | } |
| 839 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 840 | #ifdef _WIN32 |
| 841 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 842 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 843 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 844 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 845 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 846 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 847 | #if 0 |
| 848 | #define DISP_FREQ 1000 |
| 849 | { |
| 850 | static int64_t delta_min = INT64_MAX; |
| 851 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 852 | static int count; |
| 853 | ti = qemu_get_clock(vm_clock); |
| 854 | if (last_clock != 0) { |
| 855 | delta = ti - last_clock; |
| 856 | if (delta < delta_min) |
| 857 | delta_min = delta; |
| 858 | if (delta > delta_max) |
| 859 | delta_max = delta; |
| 860 | delta_cum += delta; |
| 861 | if (++count == DISP_FREQ) { |
| 862 | printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n", |
| 863 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 864 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 865 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 866 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 867 | count = 0; |
| 868 | delta_min = INT64_MAX; |
| 869 | delta_max = 0; |
| 870 | delta_cum = 0; |
| 871 | } |
| 872 | } |
| 873 | last_clock = ti; |
| 874 | } |
| 875 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 876 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 877 | qemu_get_clock(vm_clock)) || |
| 878 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 879 | qemu_get_clock(rt_clock))) { |
| 880 | /* stop the cpu because a timer occured */ |
| 881 | cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 882 | #ifdef USE_KQEMU |
bellard | 8289336 | 2005-09-03 21:33:43 +0000 | [diff] [blame] | 883 | if (global_env->kqemu_enabled) { |
| 884 | kqemu_cpu_interrupt(global_env); |
| 885 | } |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 886 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 890 | #ifndef _WIN32 |
| 891 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 892 | #if defined(__linux__) |
| 893 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 894 | #define RTC_FREQ 1024 |
| 895 | |
| 896 | static int rtc_fd; |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 897 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 898 | static int start_rtc_timer(void) |
| 899 | { |
| 900 | rtc_fd = open("/dev/rtc", O_RDONLY); |
| 901 | if (rtc_fd < 0) |
| 902 | return -1; |
| 903 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
| 904 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 905 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 906 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 907 | goto fail; |
| 908 | } |
| 909 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 910 | fail: |
| 911 | close(rtc_fd); |
| 912 | return -1; |
| 913 | } |
| 914 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
| 915 | return 0; |
| 916 | } |
| 917 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 918 | #else |
| 919 | |
| 920 | static int start_rtc_timer(void) |
| 921 | { |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | #endif /* !defined(__linux__) */ |
| 926 | |
| 927 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 928 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 929 | static void init_timers(void) |
| 930 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 931 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 932 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 933 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 934 | #ifdef _WIN32 |
| 935 | { |
| 936 | int count=0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 937 | timerID = timeSetEvent(1, // interval (ms) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 938 | 0, // resolution |
| 939 | host_alarm_handler, // function |
| 940 | (DWORD)&count, // user parameter |
| 941 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 942 | if( !timerID ) { |
| 943 | perror("failed timer alarm"); |
| 944 | exit(1); |
| 945 | } |
| 946 | } |
| 947 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
| 948 | #else |
| 949 | { |
| 950 | struct sigaction act; |
| 951 | struct itimerval itv; |
| 952 | |
| 953 | /* get times() syscall frequency */ |
| 954 | timer_freq = sysconf(_SC_CLK_TCK); |
| 955 | |
| 956 | /* timer signal */ |
| 957 | sigfillset(&act.sa_mask); |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 958 | act.sa_flags = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 959 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 960 | act.sa_flags |= SA_ONSTACK; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 961 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 962 | act.sa_handler = host_alarm_handler; |
| 963 | sigaction(SIGALRM, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 964 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 965 | itv.it_interval.tv_sec = 0; |
bellard | d79284e | 2005-01-26 21:56:26 +0000 | [diff] [blame] | 966 | 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] | 967 | itv.it_value.tv_sec = 0; |
| 968 | itv.it_value.tv_usec = 10 * 1000; |
| 969 | setitimer(ITIMER_REAL, &itv, NULL); |
| 970 | /* we probe the tick duration of the kernel to inform the user if |
| 971 | the emulated kernel requested a too high timer frequency */ |
| 972 | getitimer(ITIMER_REAL, &itv); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 973 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 974 | #if defined(__linux__) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 975 | if (itv.it_interval.tv_usec > 1000) { |
| 976 | /* try to use /dev/rtc to have a faster timer */ |
| 977 | if (start_rtc_timer() < 0) |
| 978 | goto use_itimer; |
| 979 | /* disable itimer */ |
| 980 | itv.it_interval.tv_sec = 0; |
| 981 | itv.it_interval.tv_usec = 0; |
| 982 | itv.it_value.tv_sec = 0; |
| 983 | itv.it_value.tv_usec = 0; |
| 984 | setitimer(ITIMER_REAL, &itv, NULL); |
| 985 | |
| 986 | /* use the RTC */ |
bellard | a1968d7 | 2004-06-25 17:06:27 +0000 | [diff] [blame] | 987 | sigaction(SIGIO, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 988 | fcntl(rtc_fd, F_SETFL, O_ASYNC); |
| 989 | fcntl(rtc_fd, F_SETOWN, getpid()); |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 990 | } else |
| 991 | #endif /* defined(__linux__) */ |
| 992 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 993 | use_itimer: |
| 994 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
| 995 | PIT_FREQ) / 1000000; |
| 996 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 997 | } |
| 998 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1001 | void quit_timers(void) |
| 1002 | { |
| 1003 | #ifdef _WIN32 |
| 1004 | timeKillEvent(timerID); |
| 1005 | #endif |
| 1006 | } |
| 1007 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1008 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1009 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1010 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1011 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1012 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1013 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1016 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1017 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1018 | char buf[4096]; |
| 1019 | va_list ap; |
| 1020 | va_start(ap, fmt); |
| 1021 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1022 | qemu_chr_write(s, buf, strlen(buf)); |
| 1023 | va_end(ap); |
| 1024 | } |
| 1025 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1026 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1027 | { |
| 1028 | if (s->chr_send_event) |
| 1029 | s->chr_send_event(s, event); |
| 1030 | } |
| 1031 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1032 | void qemu_chr_add_read_handler(CharDriverState *s, |
| 1033 | IOCanRWHandler *fd_can_read, |
| 1034 | IOReadHandler *fd_read, void *opaque) |
| 1035 | { |
| 1036 | s->chr_add_read_handler(s, fd_can_read, fd_read, opaque); |
| 1037 | } |
| 1038 | |
| 1039 | void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event) |
| 1040 | { |
| 1041 | s->chr_event = chr_event; |
| 1042 | } |
| 1043 | |
| 1044 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1045 | { |
| 1046 | return len; |
| 1047 | } |
| 1048 | |
| 1049 | static void null_chr_add_read_handler(CharDriverState *chr, |
| 1050 | IOCanRWHandler *fd_can_read, |
| 1051 | IOReadHandler *fd_read, void *opaque) |
| 1052 | { |
| 1053 | } |
| 1054 | |
| 1055 | CharDriverState *qemu_chr_open_null(void) |
| 1056 | { |
| 1057 | CharDriverState *chr; |
| 1058 | |
| 1059 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1060 | if (!chr) |
| 1061 | return NULL; |
| 1062 | chr->chr_write = null_chr_write; |
| 1063 | chr->chr_add_read_handler = null_chr_add_read_handler; |
| 1064 | return chr; |
| 1065 | } |
| 1066 | |
| 1067 | #ifndef _WIN32 |
| 1068 | |
| 1069 | typedef struct { |
| 1070 | int fd_in, fd_out; |
| 1071 | /* for nographic stdio only */ |
| 1072 | IOCanRWHandler *fd_can_read; |
| 1073 | IOReadHandler *fd_read; |
| 1074 | void *fd_opaque; |
| 1075 | } FDCharDriver; |
| 1076 | |
| 1077 | #define STDIO_MAX_CLIENTS 2 |
| 1078 | |
| 1079 | static int stdio_nb_clients; |
| 1080 | static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS]; |
| 1081 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1082 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 1083 | { |
| 1084 | int ret, len; |
| 1085 | |
| 1086 | len = len1; |
| 1087 | while (len > 0) { |
| 1088 | ret = write(fd, buf, len); |
| 1089 | if (ret < 0) { |
| 1090 | if (errno != EINTR && errno != EAGAIN) |
| 1091 | return -1; |
| 1092 | } else if (ret == 0) { |
| 1093 | break; |
| 1094 | } else { |
| 1095 | buf += ret; |
| 1096 | len -= ret; |
| 1097 | } |
| 1098 | } |
| 1099 | return len1 - len; |
| 1100 | } |
| 1101 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1102 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1103 | { |
| 1104 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1105 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static void fd_chr_add_read_handler(CharDriverState *chr, |
| 1109 | IOCanRWHandler *fd_can_read, |
| 1110 | IOReadHandler *fd_read, void *opaque) |
| 1111 | { |
| 1112 | FDCharDriver *s = chr->opaque; |
| 1113 | |
| 1114 | if (nographic && s->fd_in == 0) { |
| 1115 | s->fd_can_read = fd_can_read; |
| 1116 | s->fd_read = fd_read; |
| 1117 | s->fd_opaque = opaque; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1118 | } else { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1119 | qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1123 | /* open a character device to a unix fd */ |
| 1124 | CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
| 1125 | { |
| 1126 | CharDriverState *chr; |
| 1127 | FDCharDriver *s; |
| 1128 | |
| 1129 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1130 | if (!chr) |
| 1131 | return NULL; |
| 1132 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 1133 | if (!s) { |
| 1134 | free(chr); |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | s->fd_in = fd_in; |
| 1138 | s->fd_out = fd_out; |
| 1139 | chr->opaque = s; |
| 1140 | chr->chr_write = fd_chr_write; |
| 1141 | chr->chr_add_read_handler = fd_chr_add_read_handler; |
| 1142 | return chr; |
| 1143 | } |
| 1144 | |
| 1145 | /* for STDIO, we handle the case where several clients use it |
| 1146 | (nographic mode) */ |
| 1147 | |
| 1148 | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ |
| 1149 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1150 | #define TERM_FIFO_MAX_SIZE 1 |
| 1151 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1152 | static int term_got_escape, client_index; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1153 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
| 1154 | int term_fifo_size; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1155 | |
| 1156 | void term_print_help(void) |
| 1157 | { |
| 1158 | printf("\n" |
| 1159 | "C-a h print this help\n" |
| 1160 | "C-a x exit emulator\n" |
| 1161 | "C-a s save disk data back to file (if -snapshot)\n" |
| 1162 | "C-a b send break (magic sysrq)\n" |
| 1163 | "C-a c switch between console and monitor\n" |
| 1164 | "C-a C-a send C-a\n" |
| 1165 | ); |
| 1166 | } |
| 1167 | |
| 1168 | /* called when a char is received */ |
| 1169 | static void stdio_received_byte(int ch) |
| 1170 | { |
| 1171 | if (term_got_escape) { |
| 1172 | term_got_escape = 0; |
| 1173 | switch(ch) { |
| 1174 | case 'h': |
| 1175 | term_print_help(); |
| 1176 | break; |
| 1177 | case 'x': |
| 1178 | exit(0); |
| 1179 | break; |
| 1180 | case 's': |
| 1181 | { |
| 1182 | int i; |
| 1183 | for (i = 0; i < MAX_DISKS; i++) { |
| 1184 | if (bs_table[i]) |
| 1185 | bdrv_commit(bs_table[i]); |
| 1186 | } |
| 1187 | } |
| 1188 | break; |
| 1189 | case 'b': |
| 1190 | if (client_index < stdio_nb_clients) { |
| 1191 | CharDriverState *chr; |
| 1192 | FDCharDriver *s; |
| 1193 | |
| 1194 | chr = stdio_clients[client_index]; |
| 1195 | s = chr->opaque; |
| 1196 | chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK); |
| 1197 | } |
| 1198 | break; |
| 1199 | case 'c': |
| 1200 | client_index++; |
| 1201 | if (client_index >= stdio_nb_clients) |
| 1202 | client_index = 0; |
| 1203 | if (client_index == 0) { |
| 1204 | /* send a new line in the monitor to get the prompt */ |
| 1205 | ch = '\r'; |
| 1206 | goto send_char; |
| 1207 | } |
| 1208 | break; |
| 1209 | case TERM_ESCAPE: |
| 1210 | goto send_char; |
| 1211 | } |
| 1212 | } else if (ch == TERM_ESCAPE) { |
| 1213 | term_got_escape = 1; |
| 1214 | } else { |
| 1215 | send_char: |
| 1216 | if (client_index < stdio_nb_clients) { |
| 1217 | uint8_t buf[1]; |
| 1218 | CharDriverState *chr; |
| 1219 | FDCharDriver *s; |
| 1220 | |
| 1221 | chr = stdio_clients[client_index]; |
| 1222 | s = chr->opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1223 | if (s->fd_can_read(s->fd_opaque) > 0) { |
| 1224 | buf[0] = ch; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1225 | s->fd_read(s->fd_opaque, buf, 1); |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1226 | } else if (term_fifo_size == 0) { |
| 1227 | term_fifo[term_fifo_size++] = ch; |
| 1228 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | static int stdio_can_read(void *opaque) |
| 1234 | { |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1235 | CharDriverState *chr; |
| 1236 | FDCharDriver *s; |
| 1237 | |
| 1238 | if (client_index < stdio_nb_clients) { |
| 1239 | chr = stdio_clients[client_index]; |
| 1240 | s = chr->opaque; |
| 1241 | /* try to flush the queue if needed */ |
| 1242 | if (term_fifo_size != 0 && s->fd_can_read(s->fd_opaque) > 0) { |
| 1243 | s->fd_read(s->fd_opaque, term_fifo, 1); |
| 1244 | term_fifo_size = 0; |
| 1245 | } |
| 1246 | /* see if we can absorb more chars */ |
| 1247 | if (term_fifo_size == 0) |
| 1248 | return 1; |
| 1249 | else |
| 1250 | return 0; |
| 1251 | } else { |
| 1252 | return 1; |
| 1253 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | static void stdio_read(void *opaque, const uint8_t *buf, int size) |
| 1257 | { |
| 1258 | int i; |
| 1259 | for(i = 0; i < size; i++) |
| 1260 | stdio_received_byte(buf[i]); |
| 1261 | } |
| 1262 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1263 | /* init terminal so that we can grab keys */ |
| 1264 | static struct termios oldtty; |
| 1265 | static int old_fd0_flags; |
| 1266 | |
| 1267 | static void term_exit(void) |
| 1268 | { |
| 1269 | tcsetattr (0, TCSANOW, &oldtty); |
| 1270 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1271 | } |
| 1272 | |
| 1273 | static void term_init(void) |
| 1274 | { |
| 1275 | struct termios tty; |
| 1276 | |
| 1277 | tcgetattr (0, &tty); |
| 1278 | oldtty = tty; |
| 1279 | old_fd0_flags = fcntl(0, F_GETFL); |
| 1280 | |
| 1281 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1282 | |INLCR|IGNCR|ICRNL|IXON); |
| 1283 | tty.c_oflag |= OPOST; |
| 1284 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1285 | /* if graphical mode, we allow Ctrl-C handling */ |
| 1286 | if (nographic) |
| 1287 | tty.c_lflag &= ~ISIG; |
| 1288 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1289 | tty.c_cflag |= CS8; |
| 1290 | tty.c_cc[VMIN] = 1; |
| 1291 | tty.c_cc[VTIME] = 0; |
| 1292 | |
| 1293 | tcsetattr (0, TCSANOW, &tty); |
| 1294 | |
| 1295 | atexit(term_exit); |
| 1296 | |
| 1297 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 1298 | } |
| 1299 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1300 | CharDriverState *qemu_chr_open_stdio(void) |
| 1301 | { |
| 1302 | CharDriverState *chr; |
| 1303 | |
| 1304 | if (nographic) { |
| 1305 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 1306 | return NULL; |
| 1307 | chr = qemu_chr_open_fd(0, 1); |
| 1308 | if (stdio_nb_clients == 0) |
| 1309 | qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL); |
| 1310 | client_index = stdio_nb_clients; |
| 1311 | } else { |
| 1312 | if (stdio_nb_clients != 0) |
| 1313 | return NULL; |
| 1314 | chr = qemu_chr_open_fd(0, 1); |
| 1315 | } |
| 1316 | stdio_clients[stdio_nb_clients++] = chr; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1317 | if (stdio_nb_clients == 1) { |
| 1318 | /* set the terminal in raw mode */ |
| 1319 | term_init(); |
| 1320 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1321 | return chr; |
| 1322 | } |
| 1323 | |
| 1324 | #if defined(__linux__) |
| 1325 | CharDriverState *qemu_chr_open_pty(void) |
| 1326 | { |
| 1327 | char slave_name[1024]; |
| 1328 | int master_fd, slave_fd; |
| 1329 | |
| 1330 | /* Not satisfying */ |
| 1331 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | fprintf(stderr, "char device redirected to %s\n", slave_name); |
| 1335 | return qemu_chr_open_fd(master_fd, master_fd); |
| 1336 | } |
| 1337 | #else |
| 1338 | CharDriverState *qemu_chr_open_pty(void) |
| 1339 | { |
| 1340 | return NULL; |
| 1341 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1342 | #endif |
| 1343 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1344 | #endif /* !defined(_WIN32) */ |
| 1345 | |
| 1346 | CharDriverState *qemu_chr_open(const char *filename) |
| 1347 | { |
| 1348 | if (!strcmp(filename, "vc")) { |
| 1349 | return text_console_init(&display_state); |
| 1350 | } else if (!strcmp(filename, "null")) { |
| 1351 | return qemu_chr_open_null(); |
| 1352 | } else |
| 1353 | #ifndef _WIN32 |
| 1354 | if (!strcmp(filename, "pty")) { |
| 1355 | return qemu_chr_open_pty(); |
| 1356 | } else if (!strcmp(filename, "stdio")) { |
| 1357 | return qemu_chr_open_stdio(); |
| 1358 | } else |
| 1359 | #endif |
| 1360 | { |
| 1361 | return NULL; |
| 1362 | } |
| 1363 | } |
| 1364 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1365 | /***********************************************************/ |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1366 | /* Linux network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1367 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1368 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1369 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1370 | int len, i, j, c; |
| 1371 | |
| 1372 | for(i=0;i<size;i+=16) { |
| 1373 | len = size - i; |
| 1374 | if (len > 16) |
| 1375 | len = 16; |
| 1376 | fprintf(f, "%08x ", i); |
| 1377 | for(j=0;j<16;j++) { |
| 1378 | if (j < len) |
| 1379 | fprintf(f, " %02x", buf[i+j]); |
| 1380 | else |
| 1381 | fprintf(f, " "); |
| 1382 | } |
| 1383 | fprintf(f, " "); |
| 1384 | for(j=0;j<len;j++) { |
| 1385 | c = buf[i+j]; |
| 1386 | if (c < ' ' || c > '~') |
| 1387 | c = '.'; |
| 1388 | fprintf(f, "%c", c); |
| 1389 | } |
| 1390 | fprintf(f, "\n"); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
| 1395 | { |
| 1396 | nd->send_packet(nd, buf, size); |
| 1397 | } |
| 1398 | |
| 1399 | void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, |
| 1400 | IOReadHandler *fd_read, void *opaque) |
| 1401 | { |
| 1402 | nd->add_read_packet(nd, fd_can_read, fd_read, opaque); |
| 1403 | } |
| 1404 | |
| 1405 | /* dummy network adapter */ |
| 1406 | |
| 1407 | static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
| 1408 | { |
| 1409 | } |
| 1410 | |
| 1411 | static void dummy_add_read_packet(NetDriverState *nd, |
| 1412 | IOCanRWHandler *fd_can_read, |
| 1413 | IOReadHandler *fd_read, void *opaque) |
| 1414 | { |
| 1415 | } |
| 1416 | |
| 1417 | static int net_dummy_init(NetDriverState *nd) |
| 1418 | { |
| 1419 | nd->send_packet = dummy_send_packet; |
| 1420 | nd->add_read_packet = dummy_add_read_packet; |
| 1421 | pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy"); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1422 | return 0; |
| 1423 | } |
| 1424 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1425 | #if defined(CONFIG_SLIRP) |
| 1426 | |
| 1427 | /* slirp network adapter */ |
| 1428 | |
| 1429 | static void *slirp_fd_opaque; |
| 1430 | static IOCanRWHandler *slirp_fd_can_read; |
| 1431 | static IOReadHandler *slirp_fd_read; |
| 1432 | static int slirp_inited; |
| 1433 | |
| 1434 | int slirp_can_output(void) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1435 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1436 | return slirp_fd_can_read(slirp_fd_opaque); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1439 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 1440 | { |
| 1441 | #if 0 |
| 1442 | printf("output:\n"); |
| 1443 | hex_dump(stdout, pkt, pkt_len); |
| 1444 | #endif |
| 1445 | slirp_fd_read(slirp_fd_opaque, pkt, pkt_len); |
| 1446 | } |
| 1447 | |
| 1448 | static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
| 1449 | { |
| 1450 | #if 0 |
| 1451 | printf("input:\n"); |
| 1452 | hex_dump(stdout, buf, size); |
| 1453 | #endif |
| 1454 | slirp_input(buf, size); |
| 1455 | } |
| 1456 | |
| 1457 | static void slirp_add_read_packet(NetDriverState *nd, |
| 1458 | IOCanRWHandler *fd_can_read, |
| 1459 | IOReadHandler *fd_read, void *opaque) |
| 1460 | { |
| 1461 | slirp_fd_opaque = opaque; |
| 1462 | slirp_fd_can_read = fd_can_read; |
| 1463 | slirp_fd_read = fd_read; |
| 1464 | } |
| 1465 | |
| 1466 | static int net_slirp_init(NetDriverState *nd) |
| 1467 | { |
| 1468 | if (!slirp_inited) { |
| 1469 | slirp_inited = 1; |
| 1470 | slirp_init(); |
| 1471 | } |
| 1472 | nd->send_packet = slirp_send_packet; |
| 1473 | nd->add_read_packet = slirp_add_read_packet; |
| 1474 | pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp"); |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1478 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 1479 | { |
| 1480 | const char *p, *p1; |
| 1481 | int len; |
| 1482 | p = *pp; |
| 1483 | p1 = strchr(p, sep); |
| 1484 | if (!p1) |
| 1485 | return -1; |
| 1486 | len = p1 - p; |
| 1487 | p1++; |
| 1488 | if (buf_size > 0) { |
| 1489 | if (len > buf_size - 1) |
| 1490 | len = buf_size - 1; |
| 1491 | memcpy(buf, p, len); |
| 1492 | buf[len] = '\0'; |
| 1493 | } |
| 1494 | *pp = p1; |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | static void net_slirp_redir(const char *redir_str) |
| 1499 | { |
| 1500 | int is_udp; |
| 1501 | char buf[256], *r; |
| 1502 | const char *p; |
| 1503 | struct in_addr guest_addr; |
| 1504 | int host_port, guest_port; |
| 1505 | |
| 1506 | if (!slirp_inited) { |
| 1507 | slirp_inited = 1; |
| 1508 | slirp_init(); |
| 1509 | } |
| 1510 | |
| 1511 | p = redir_str; |
| 1512 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 1513 | goto fail; |
| 1514 | if (!strcmp(buf, "tcp")) { |
| 1515 | is_udp = 0; |
| 1516 | } else if (!strcmp(buf, "udp")) { |
| 1517 | is_udp = 1; |
| 1518 | } else { |
| 1519 | goto fail; |
| 1520 | } |
| 1521 | |
| 1522 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 1523 | goto fail; |
| 1524 | host_port = strtol(buf, &r, 0); |
| 1525 | if (r == buf) |
| 1526 | goto fail; |
| 1527 | |
| 1528 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 1529 | goto fail; |
| 1530 | if (buf[0] == '\0') { |
| 1531 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 1532 | } |
| 1533 | if (!inet_aton(buf, &guest_addr)) |
| 1534 | goto fail; |
| 1535 | |
| 1536 | guest_port = strtol(p, &r, 0); |
| 1537 | if (r == p) |
| 1538 | goto fail; |
| 1539 | |
| 1540 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 1541 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 1542 | exit(1); |
| 1543 | } |
| 1544 | return; |
| 1545 | fail: |
| 1546 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 1547 | exit(1); |
| 1548 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1549 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 1550 | #ifndef _WIN32 |
| 1551 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1552 | char smb_dir[1024]; |
| 1553 | |
| 1554 | static void smb_exit(void) |
| 1555 | { |
| 1556 | DIR *d; |
| 1557 | struct dirent *de; |
| 1558 | char filename[1024]; |
| 1559 | |
| 1560 | /* erase all the files in the directory */ |
| 1561 | d = opendir(smb_dir); |
| 1562 | for(;;) { |
| 1563 | de = readdir(d); |
| 1564 | if (!de) |
| 1565 | break; |
| 1566 | if (strcmp(de->d_name, ".") != 0 && |
| 1567 | strcmp(de->d_name, "..") != 0) { |
| 1568 | snprintf(filename, sizeof(filename), "%s/%s", |
| 1569 | smb_dir, de->d_name); |
| 1570 | unlink(filename); |
| 1571 | } |
| 1572 | } |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 1573 | closedir(d); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1574 | rmdir(smb_dir); |
| 1575 | } |
| 1576 | |
| 1577 | /* automatic user mode samba server configuration */ |
| 1578 | void net_slirp_smb(const char *exported_dir) |
| 1579 | { |
| 1580 | char smb_conf[1024]; |
| 1581 | char smb_cmdline[1024]; |
| 1582 | FILE *f; |
| 1583 | |
| 1584 | if (!slirp_inited) { |
| 1585 | slirp_inited = 1; |
| 1586 | slirp_init(); |
| 1587 | } |
| 1588 | |
| 1589 | /* XXX: better tmp dir construction */ |
| 1590 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 1591 | if (mkdir(smb_dir, 0700) < 0) { |
| 1592 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 1593 | exit(1); |
| 1594 | } |
| 1595 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
| 1596 | |
| 1597 | f = fopen(smb_conf, "w"); |
| 1598 | if (!f) { |
| 1599 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 1600 | exit(1); |
| 1601 | } |
| 1602 | fprintf(f, |
| 1603 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 1604 | "private dir=%s\n" |
| 1605 | "smb ports=0\n" |
| 1606 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1607 | "pid directory=%s\n" |
| 1608 | "lock directory=%s\n" |
| 1609 | "log file=%s/log.smbd\n" |
| 1610 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 1611 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1612 | "[qemu]\n" |
| 1613 | "path=%s\n" |
| 1614 | "read only=no\n" |
| 1615 | "guest ok=yes\n", |
| 1616 | smb_dir, |
| 1617 | smb_dir, |
| 1618 | smb_dir, |
| 1619 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 1620 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 1621 | exported_dir |
| 1622 | ); |
| 1623 | fclose(f); |
| 1624 | atexit(smb_exit); |
| 1625 | |
| 1626 | snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s", |
| 1627 | smb_conf); |
| 1628 | |
| 1629 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 1630 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1631 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 1632 | #endif /* !defined(_WIN32) */ |
| 1633 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1634 | #endif /* CONFIG_SLIRP */ |
| 1635 | |
| 1636 | #if !defined(_WIN32) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 1637 | #ifdef _BSD |
| 1638 | static int tun_open(char *ifname, int ifname_size) |
| 1639 | { |
| 1640 | int fd; |
| 1641 | char *dev; |
| 1642 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1643 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 1644 | fd = open("/dev/tap", O_RDWR); |
| 1645 | if (fd < 0) { |
| 1646 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 1647 | return -1; |
| 1648 | } |
| 1649 | |
| 1650 | fstat(fd, &s); |
| 1651 | dev = devname(s.st_rdev, S_IFCHR); |
| 1652 | pstrcpy(ifname, ifname_size, dev); |
| 1653 | |
| 1654 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1655 | return fd; |
| 1656 | } |
| 1657 | #else |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1658 | static int tun_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1659 | { |
| 1660 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1661 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1662 | |
| 1663 | fd = open("/dev/net/tun", O_RDWR); |
| 1664 | if (fd < 0) { |
| 1665 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 1666 | return -1; |
| 1667 | } |
| 1668 | memset(&ifr, 0, sizeof(ifr)); |
| 1669 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 1670 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d"); |
| 1671 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 1672 | if (ret != 0) { |
| 1673 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 1674 | close(fd); |
| 1675 | return -1; |
| 1676 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1677 | printf("Connected to host network interface: %s\n", ifr.ifr_name); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1678 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1679 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1680 | return fd; |
| 1681 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 1682 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1683 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1684 | static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1685 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1686 | write(nd->fd, buf, size); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 1689 | static void tun_add_read_packet(NetDriverState *nd, |
| 1690 | IOCanRWHandler *fd_can_read, |
| 1691 | IOReadHandler *fd_read, void *opaque) |
| 1692 | { |
| 1693 | qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque); |
| 1694 | } |
| 1695 | |
| 1696 | static int net_tun_init(NetDriverState *nd) |
| 1697 | { |
| 1698 | int pid, status; |
| 1699 | char *args[3]; |
| 1700 | char **parg; |
| 1701 | |
| 1702 | nd->fd = tun_open(nd->ifname, sizeof(nd->ifname)); |
| 1703 | if (nd->fd < 0) |
| 1704 | return -1; |
| 1705 | |
| 1706 | /* try to launch network init script */ |
| 1707 | pid = fork(); |
| 1708 | if (pid >= 0) { |
| 1709 | if (pid == 0) { |
| 1710 | parg = args; |
| 1711 | *parg++ = network_script; |
| 1712 | *parg++ = nd->ifname; |
| 1713 | *parg++ = NULL; |
| 1714 | execv(network_script, args); |
| 1715 | exit(1); |
| 1716 | } |
| 1717 | while (waitpid(pid, &status, 0) != pid); |
| 1718 | if (!WIFEXITED(status) || |
| 1719 | WEXITSTATUS(status) != 0) { |
| 1720 | fprintf(stderr, "%s: could not launch network script\n", |
| 1721 | network_script); |
| 1722 | } |
| 1723 | } |
| 1724 | nd->send_packet = tun_send_packet; |
| 1725 | nd->add_read_packet = tun_add_read_packet; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | static int net_fd_init(NetDriverState *nd, int fd) |
| 1730 | { |
| 1731 | nd->fd = fd; |
| 1732 | nd->send_packet = tun_send_packet; |
| 1733 | nd->add_read_packet = tun_add_read_packet; |
| 1734 | pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd"); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | #endif /* !_WIN32 */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1739 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1740 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 1741 | /* USB devices */ |
| 1742 | |
| 1743 | static int usb_device_add(const char *devname) |
| 1744 | { |
| 1745 | const char *p; |
| 1746 | USBDevice *dev; |
| 1747 | int i; |
| 1748 | |
| 1749 | if (!vm_usb_hub) |
| 1750 | return -1; |
| 1751 | for(i = 0;i < MAX_VM_USB_PORTS; i++) { |
| 1752 | if (!vm_usb_ports[i]->dev) |
| 1753 | break; |
| 1754 | } |
| 1755 | if (i == MAX_VM_USB_PORTS) |
| 1756 | return -1; |
| 1757 | |
| 1758 | if (strstart(devname, "host:", &p)) { |
| 1759 | dev = usb_host_device_open(p); |
| 1760 | if (!dev) |
| 1761 | return -1; |
| 1762 | } else if (!strcmp(devname, "mouse")) { |
| 1763 | dev = usb_mouse_init(); |
| 1764 | if (!dev) |
| 1765 | return -1; |
| 1766 | } else { |
| 1767 | return -1; |
| 1768 | } |
| 1769 | usb_attach(vm_usb_ports[i], dev); |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | static int usb_device_del(const char *devname) |
| 1774 | { |
| 1775 | USBDevice *dev; |
| 1776 | int bus_num, addr, i; |
| 1777 | const char *p; |
| 1778 | |
| 1779 | if (!vm_usb_hub) |
| 1780 | return -1; |
| 1781 | |
| 1782 | p = strchr(devname, '.'); |
| 1783 | if (!p) |
| 1784 | return -1; |
| 1785 | bus_num = strtoul(devname, NULL, 0); |
| 1786 | addr = strtoul(p + 1, NULL, 0); |
| 1787 | if (bus_num != 0) |
| 1788 | return -1; |
| 1789 | for(i = 0;i < MAX_VM_USB_PORTS; i++) { |
| 1790 | dev = vm_usb_ports[i]->dev; |
| 1791 | if (dev && dev->addr == addr) |
| 1792 | break; |
| 1793 | } |
| 1794 | if (i == MAX_VM_USB_PORTS) |
| 1795 | return -1; |
| 1796 | usb_attach(vm_usb_ports[i], NULL); |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | void do_usb_add(const char *devname) |
| 1801 | { |
| 1802 | int ret; |
| 1803 | ret = usb_device_add(devname); |
| 1804 | if (ret < 0) |
| 1805 | term_printf("Could not add USB device '%s'\n", devname); |
| 1806 | } |
| 1807 | |
| 1808 | void do_usb_del(const char *devname) |
| 1809 | { |
| 1810 | int ret; |
| 1811 | ret = usb_device_del(devname); |
| 1812 | if (ret < 0) |
| 1813 | term_printf("Could not remove USB device '%s'\n", devname); |
| 1814 | } |
| 1815 | |
| 1816 | void usb_info(void) |
| 1817 | { |
| 1818 | USBDevice *dev; |
| 1819 | int i; |
| 1820 | const char *speed_str; |
| 1821 | |
| 1822 | if (!vm_usb_hub) { |
| 1823 | term_printf("USB support not enabled\n"); |
| 1824 | return; |
| 1825 | } |
| 1826 | |
| 1827 | for(i = 0; i < MAX_VM_USB_PORTS; i++) { |
| 1828 | dev = vm_usb_ports[i]->dev; |
| 1829 | if (dev) { |
| 1830 | term_printf("Hub port %d:\n", i); |
| 1831 | switch(dev->speed) { |
| 1832 | case USB_SPEED_LOW: |
| 1833 | speed_str = "1.5"; |
| 1834 | break; |
| 1835 | case USB_SPEED_FULL: |
| 1836 | speed_str = "12"; |
| 1837 | break; |
| 1838 | case USB_SPEED_HIGH: |
| 1839 | speed_str = "480"; |
| 1840 | break; |
| 1841 | default: |
| 1842 | speed_str = "?"; |
| 1843 | break; |
| 1844 | } |
| 1845 | term_printf(" Device %d.%d, speed %s Mb/s\n", |
| 1846 | 0, dev->addr, speed_str); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | /***********************************************************/ |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 1852 | /* pid file */ |
| 1853 | |
| 1854 | static char *pid_filename; |
| 1855 | |
| 1856 | /* Remove PID file. Called on normal exit */ |
| 1857 | |
| 1858 | static void remove_pidfile(void) |
| 1859 | { |
| 1860 | unlink (pid_filename); |
| 1861 | } |
| 1862 | |
| 1863 | static void create_pidfile(const char *filename) |
| 1864 | { |
| 1865 | struct stat pidstat; |
| 1866 | FILE *f; |
| 1867 | |
| 1868 | /* Try to write our PID to the named file */ |
| 1869 | if (stat(filename, &pidstat) < 0) { |
| 1870 | if (errno == ENOENT) { |
| 1871 | if ((f = fopen (filename, "w")) == NULL) { |
| 1872 | perror("Opening pidfile"); |
| 1873 | exit(1); |
| 1874 | } |
| 1875 | fprintf(f, "%d\n", getpid()); |
| 1876 | fclose(f); |
| 1877 | pid_filename = qemu_strdup(filename); |
| 1878 | if (!pid_filename) { |
| 1879 | fprintf(stderr, "Could not save PID filename"); |
| 1880 | exit(1); |
| 1881 | } |
| 1882 | atexit(remove_pidfile); |
| 1883 | } |
| 1884 | } else { |
| 1885 | fprintf(stderr, "%s already exists. Remove it and try again.\n", |
| 1886 | filename); |
| 1887 | exit(1); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1892 | /* dumb display */ |
| 1893 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1894 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 1895 | { |
| 1896 | } |
| 1897 | |
| 1898 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 1899 | { |
| 1900 | } |
| 1901 | |
| 1902 | static void dumb_refresh(DisplayState *ds) |
| 1903 | { |
| 1904 | vga_update_display(); |
| 1905 | } |
| 1906 | |
| 1907 | void dumb_display_init(DisplayState *ds) |
| 1908 | { |
| 1909 | ds->data = NULL; |
| 1910 | ds->linesize = 0; |
| 1911 | ds->depth = 0; |
| 1912 | ds->dpy_update = dumb_update; |
| 1913 | ds->dpy_resize = dumb_resize; |
| 1914 | ds->dpy_refresh = dumb_refresh; |
| 1915 | } |
| 1916 | |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 1917 | #if !defined(CONFIG_SOFTMMU) |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1918 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1919 | /* cpu signal handler */ |
| 1920 | static void host_segv_handler(int host_signum, siginfo_t *info, |
| 1921 | void *puc) |
| 1922 | { |
| 1923 | if (cpu_signal_handler(host_signum, info, puc)) |
| 1924 | return; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1925 | if (stdio_nb_clients > 0) |
| 1926 | term_exit(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1927 | abort(); |
| 1928 | } |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 1929 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1930 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1931 | /***********************************************************/ |
| 1932 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1933 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1934 | #define MAX_IO_HANDLERS 64 |
| 1935 | |
| 1936 | typedef struct IOHandlerRecord { |
| 1937 | int fd; |
| 1938 | IOCanRWHandler *fd_can_read; |
| 1939 | IOReadHandler *fd_read; |
| 1940 | void *opaque; |
| 1941 | /* temporary data */ |
| 1942 | struct pollfd *ufd; |
| 1943 | int max_size; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1944 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1945 | } IOHandlerRecord; |
| 1946 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1947 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1948 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1949 | int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, |
| 1950 | IOReadHandler *fd_read, void *opaque) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1951 | { |
| 1952 | IOHandlerRecord *ioh; |
| 1953 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1954 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 1955 | if (!ioh) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1956 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1957 | ioh->fd = fd; |
| 1958 | ioh->fd_can_read = fd_can_read; |
| 1959 | ioh->fd_read = fd_read; |
| 1960 | ioh->opaque = opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1961 | ioh->next = first_io_handler; |
| 1962 | first_io_handler = ioh; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1963 | return 0; |
| 1964 | } |
| 1965 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1966 | void qemu_del_fd_read_handler(int fd) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1967 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1968 | IOHandlerRecord **pioh, *ioh; |
| 1969 | |
| 1970 | pioh = &first_io_handler; |
| 1971 | for(;;) { |
| 1972 | ioh = *pioh; |
| 1973 | if (ioh == NULL) |
| 1974 | break; |
| 1975 | if (ioh->fd == fd) { |
| 1976 | *pioh = ioh->next; |
| 1977 | break; |
| 1978 | } |
| 1979 | pioh = &ioh->next; |
| 1980 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1983 | /***********************************************************/ |
| 1984 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1985 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1986 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 1987 | { |
| 1988 | fwrite(buf, 1, size, f); |
| 1989 | } |
| 1990 | |
| 1991 | void qemu_put_byte(QEMUFile *f, int v) |
| 1992 | { |
| 1993 | fputc(v, f); |
| 1994 | } |
| 1995 | |
| 1996 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 1997 | { |
| 1998 | qemu_put_byte(f, v >> 8); |
| 1999 | qemu_put_byte(f, v); |
| 2000 | } |
| 2001 | |
| 2002 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 2003 | { |
| 2004 | qemu_put_byte(f, v >> 24); |
| 2005 | qemu_put_byte(f, v >> 16); |
| 2006 | qemu_put_byte(f, v >> 8); |
| 2007 | qemu_put_byte(f, v); |
| 2008 | } |
| 2009 | |
| 2010 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 2011 | { |
| 2012 | qemu_put_be32(f, v >> 32); |
| 2013 | qemu_put_be32(f, v); |
| 2014 | } |
| 2015 | |
| 2016 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) |
| 2017 | { |
| 2018 | return fread(buf, 1, size, f); |
| 2019 | } |
| 2020 | |
| 2021 | int qemu_get_byte(QEMUFile *f) |
| 2022 | { |
| 2023 | int v; |
| 2024 | v = fgetc(f); |
| 2025 | if (v == EOF) |
| 2026 | return 0; |
| 2027 | else |
| 2028 | return v; |
| 2029 | } |
| 2030 | |
| 2031 | unsigned int qemu_get_be16(QEMUFile *f) |
| 2032 | { |
| 2033 | unsigned int v; |
| 2034 | v = qemu_get_byte(f) << 8; |
| 2035 | v |= qemu_get_byte(f); |
| 2036 | return v; |
| 2037 | } |
| 2038 | |
| 2039 | unsigned int qemu_get_be32(QEMUFile *f) |
| 2040 | { |
| 2041 | unsigned int v; |
| 2042 | v = qemu_get_byte(f) << 24; |
| 2043 | v |= qemu_get_byte(f) << 16; |
| 2044 | v |= qemu_get_byte(f) << 8; |
| 2045 | v |= qemu_get_byte(f); |
| 2046 | return v; |
| 2047 | } |
| 2048 | |
| 2049 | uint64_t qemu_get_be64(QEMUFile *f) |
| 2050 | { |
| 2051 | uint64_t v; |
| 2052 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 2053 | v |= qemu_get_be32(f); |
| 2054 | return v; |
| 2055 | } |
| 2056 | |
| 2057 | int64_t qemu_ftell(QEMUFile *f) |
| 2058 | { |
| 2059 | return ftell(f); |
| 2060 | } |
| 2061 | |
| 2062 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 2063 | { |
| 2064 | if (fseek(f, pos, whence) < 0) |
| 2065 | return -1; |
| 2066 | return ftell(f); |
| 2067 | } |
| 2068 | |
| 2069 | typedef struct SaveStateEntry { |
| 2070 | char idstr[256]; |
| 2071 | int instance_id; |
| 2072 | int version_id; |
| 2073 | SaveStateHandler *save_state; |
| 2074 | LoadStateHandler *load_state; |
| 2075 | void *opaque; |
| 2076 | struct SaveStateEntry *next; |
| 2077 | } SaveStateEntry; |
| 2078 | |
| 2079 | static SaveStateEntry *first_se; |
| 2080 | |
| 2081 | int register_savevm(const char *idstr, |
| 2082 | int instance_id, |
| 2083 | int version_id, |
| 2084 | SaveStateHandler *save_state, |
| 2085 | LoadStateHandler *load_state, |
| 2086 | void *opaque) |
| 2087 | { |
| 2088 | SaveStateEntry *se, **pse; |
| 2089 | |
| 2090 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 2091 | if (!se) |
| 2092 | return -1; |
| 2093 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 2094 | se->instance_id = instance_id; |
| 2095 | se->version_id = version_id; |
| 2096 | se->save_state = save_state; |
| 2097 | se->load_state = load_state; |
| 2098 | se->opaque = opaque; |
| 2099 | se->next = NULL; |
| 2100 | |
| 2101 | /* add at the end of list */ |
| 2102 | pse = &first_se; |
| 2103 | while (*pse != NULL) |
| 2104 | pse = &(*pse)->next; |
| 2105 | *pse = se; |
| 2106 | return 0; |
| 2107 | } |
| 2108 | |
| 2109 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
| 2110 | #define QEMU_VM_FILE_VERSION 0x00000001 |
| 2111 | |
| 2112 | int qemu_savevm(const char *filename) |
| 2113 | { |
| 2114 | SaveStateEntry *se; |
| 2115 | QEMUFile *f; |
| 2116 | int len, len_pos, cur_pos, saved_vm_running, ret; |
| 2117 | |
| 2118 | saved_vm_running = vm_running; |
| 2119 | vm_stop(0); |
| 2120 | |
| 2121 | f = fopen(filename, "wb"); |
| 2122 | if (!f) { |
| 2123 | ret = -1; |
| 2124 | goto the_end; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2127 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 2128 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
| 2129 | |
| 2130 | for(se = first_se; se != NULL; se = se->next) { |
| 2131 | /* ID string */ |
| 2132 | len = strlen(se->idstr); |
| 2133 | qemu_put_byte(f, len); |
| 2134 | qemu_put_buffer(f, se->idstr, len); |
| 2135 | |
| 2136 | qemu_put_be32(f, se->instance_id); |
| 2137 | qemu_put_be32(f, se->version_id); |
| 2138 | |
| 2139 | /* record size: filled later */ |
| 2140 | len_pos = ftell(f); |
| 2141 | qemu_put_be32(f, 0); |
| 2142 | |
| 2143 | se->save_state(f, se->opaque); |
| 2144 | |
| 2145 | /* fill record size */ |
| 2146 | cur_pos = ftell(f); |
| 2147 | len = ftell(f) - len_pos - 4; |
| 2148 | fseek(f, len_pos, SEEK_SET); |
| 2149 | qemu_put_be32(f, len); |
| 2150 | fseek(f, cur_pos, SEEK_SET); |
| 2151 | } |
| 2152 | |
| 2153 | fclose(f); |
| 2154 | ret = 0; |
| 2155 | the_end: |
| 2156 | if (saved_vm_running) |
| 2157 | vm_start(); |
| 2158 | return ret; |
| 2159 | } |
| 2160 | |
| 2161 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 2162 | { |
| 2163 | SaveStateEntry *se; |
| 2164 | |
| 2165 | for(se = first_se; se != NULL; se = se->next) { |
| 2166 | if (!strcmp(se->idstr, idstr) && |
| 2167 | instance_id == se->instance_id) |
| 2168 | return se; |
| 2169 | } |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | |
| 2173 | int qemu_loadvm(const char *filename) |
| 2174 | { |
| 2175 | SaveStateEntry *se; |
| 2176 | QEMUFile *f; |
| 2177 | int len, cur_pos, ret, instance_id, record_len, version_id; |
| 2178 | int saved_vm_running; |
| 2179 | unsigned int v; |
| 2180 | char idstr[256]; |
| 2181 | |
| 2182 | saved_vm_running = vm_running; |
| 2183 | vm_stop(0); |
| 2184 | |
| 2185 | f = fopen(filename, "rb"); |
| 2186 | if (!f) { |
| 2187 | ret = -1; |
| 2188 | goto the_end; |
| 2189 | } |
| 2190 | |
| 2191 | v = qemu_get_be32(f); |
| 2192 | if (v != QEMU_VM_FILE_MAGIC) |
| 2193 | goto fail; |
| 2194 | v = qemu_get_be32(f); |
| 2195 | if (v != QEMU_VM_FILE_VERSION) { |
| 2196 | fail: |
| 2197 | fclose(f); |
| 2198 | ret = -1; |
| 2199 | goto the_end; |
| 2200 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2201 | for(;;) { |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 2202 | #if defined (DO_TB_FLUSH) |
bellard | d927637 | 2004-04-26 19:44:57 +0000 | [diff] [blame] | 2203 | tb_flush(global_env); |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 2204 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2205 | len = qemu_get_byte(f); |
| 2206 | if (feof(f)) |
| 2207 | break; |
| 2208 | qemu_get_buffer(f, idstr, len); |
| 2209 | idstr[len] = '\0'; |
| 2210 | instance_id = qemu_get_be32(f); |
| 2211 | version_id = qemu_get_be32(f); |
| 2212 | record_len = qemu_get_be32(f); |
| 2213 | #if 0 |
| 2214 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
| 2215 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 2216 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2217 | cur_pos = ftell(f); |
| 2218 | se = find_se(idstr, instance_id); |
| 2219 | if (!se) { |
| 2220 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
| 2221 | instance_id, idstr); |
| 2222 | } else { |
| 2223 | ret = se->load_state(f, se->opaque, version_id); |
| 2224 | if (ret < 0) { |
| 2225 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 2226 | instance_id, idstr); |
| 2227 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 2228 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2229 | /* always seek to exact end of record */ |
| 2230 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 2231 | } |
| 2232 | fclose(f); |
| 2233 | ret = 0; |
| 2234 | the_end: |
| 2235 | if (saved_vm_running) |
| 2236 | vm_start(); |
| 2237 | return ret; |
| 2238 | } |
| 2239 | |
| 2240 | /***********************************************************/ |
| 2241 | /* cpu save/restore */ |
| 2242 | |
| 2243 | #if defined(TARGET_I386) |
| 2244 | |
| 2245 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
| 2246 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 2247 | qemu_put_be32(f, dt->selector); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2248 | qemu_put_betl(f, dt->base); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2249 | qemu_put_be32(f, dt->limit); |
| 2250 | qemu_put_be32(f, dt->flags); |
| 2251 | } |
| 2252 | |
| 2253 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
| 2254 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 2255 | dt->selector = qemu_get_be32(f); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2256 | dt->base = qemu_get_betl(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2257 | dt->limit = qemu_get_be32(f); |
| 2258 | dt->flags = qemu_get_be32(f); |
| 2259 | } |
| 2260 | |
| 2261 | void cpu_save(QEMUFile *f, void *opaque) |
| 2262 | { |
| 2263 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2264 | uint16_t fptag, fpus, fpuc, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2265 | uint32_t hflags; |
| 2266 | int i; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2267 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2268 | for(i = 0; i < CPU_NB_REGS; i++) |
| 2269 | qemu_put_betls(f, &env->regs[i]); |
| 2270 | qemu_put_betls(f, &env->eip); |
| 2271 | qemu_put_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2272 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ |
| 2273 | qemu_put_be32s(f, &hflags); |
| 2274 | |
| 2275 | /* FPU */ |
| 2276 | fpuc = env->fpuc; |
| 2277 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
| 2278 | fptag = 0; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2279 | for(i = 0; i < 8; i++) { |
| 2280 | fptag |= ((!env->fptags[i]) << i); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | qemu_put_be16s(f, &fpuc); |
| 2284 | qemu_put_be16s(f, &fpus); |
| 2285 | qemu_put_be16s(f, &fptag); |
| 2286 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2287 | #ifdef USE_X86LDOUBLE |
| 2288 | fpregs_format = 0; |
| 2289 | #else |
| 2290 | fpregs_format = 1; |
| 2291 | #endif |
| 2292 | qemu_put_be16s(f, &fpregs_format); |
| 2293 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2294 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2295 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2296 | { |
| 2297 | uint64_t mant; |
| 2298 | uint16_t exp; |
| 2299 | /* we save the real CPU data (in case of MMX usage only 'mant' |
| 2300 | contains the MMX register */ |
| 2301 | cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
| 2302 | qemu_put_be64(f, mant); |
| 2303 | qemu_put_be16(f, exp); |
| 2304 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2305 | #else |
| 2306 | /* if we use doubles for float emulation, we save the doubles to |
| 2307 | avoid losing information in case of MMX usage. It can give |
| 2308 | problems if the image is restored on a CPU where long |
| 2309 | doubles are used instead. */ |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2310 | qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0)); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2311 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | for(i = 0; i < 6; i++) |
| 2315 | cpu_put_seg(f, &env->segs[i]); |
| 2316 | cpu_put_seg(f, &env->ldt); |
| 2317 | cpu_put_seg(f, &env->tr); |
| 2318 | cpu_put_seg(f, &env->gdt); |
| 2319 | cpu_put_seg(f, &env->idt); |
| 2320 | |
| 2321 | qemu_put_be32s(f, &env->sysenter_cs); |
| 2322 | qemu_put_be32s(f, &env->sysenter_esp); |
| 2323 | qemu_put_be32s(f, &env->sysenter_eip); |
| 2324 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2325 | qemu_put_betls(f, &env->cr[0]); |
| 2326 | qemu_put_betls(f, &env->cr[2]); |
| 2327 | qemu_put_betls(f, &env->cr[3]); |
| 2328 | qemu_put_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2329 | |
| 2330 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2331 | qemu_put_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2332 | |
| 2333 | /* MMU */ |
| 2334 | qemu_put_be32s(f, &env->a20_mask); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 2335 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2336 | /* XMM */ |
| 2337 | qemu_put_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 2338 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 2339 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 2340 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 2341 | } |
| 2342 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2343 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 2344 | qemu_put_be64s(f, &env->efer); |
| 2345 | qemu_put_be64s(f, &env->star); |
| 2346 | qemu_put_be64s(f, &env->lstar); |
| 2347 | qemu_put_be64s(f, &env->cstar); |
| 2348 | qemu_put_be64s(f, &env->fmask); |
| 2349 | qemu_put_be64s(f, &env->kernelgsbase); |
| 2350 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2353 | #ifdef USE_X86LDOUBLE |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2354 | /* XXX: add that in a FPU generic layer */ |
| 2355 | union x86_longdouble { |
| 2356 | uint64_t mant; |
| 2357 | uint16_t exp; |
| 2358 | }; |
| 2359 | |
| 2360 | #define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
| 2361 | #define EXPBIAS1 1023 |
| 2362 | #define EXPD1(fp) ((fp >> 52) & 0x7FF) |
| 2363 | #define SIGND1(fp) ((fp >> 32) & 0x80000000) |
| 2364 | |
| 2365 | static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
| 2366 | { |
| 2367 | int e; |
| 2368 | /* mantissa */ |
| 2369 | p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
| 2370 | /* exponent + sign */ |
| 2371 | e = EXPD1(temp) - EXPBIAS1 + 16383; |
| 2372 | e |= SIGND1(temp) >> 16; |
| 2373 | p->exp = e; |
| 2374 | } |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2375 | #endif |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2376 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2377 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 2378 | { |
| 2379 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2380 | int i, guess_mmx; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2381 | uint32_t hflags; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2382 | uint16_t fpus, fpuc, fptag, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2383 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2384 | if (version_id != 3) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2385 | return -EINVAL; |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2386 | for(i = 0; i < CPU_NB_REGS; i++) |
| 2387 | qemu_get_betls(f, &env->regs[i]); |
| 2388 | qemu_get_betls(f, &env->eip); |
| 2389 | qemu_get_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2390 | qemu_get_be32s(f, &hflags); |
| 2391 | |
| 2392 | qemu_get_be16s(f, &fpuc); |
| 2393 | qemu_get_be16s(f, &fpus); |
| 2394 | qemu_get_be16s(f, &fptag); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2395 | qemu_get_be16s(f, &fpregs_format); |
| 2396 | |
| 2397 | /* NOTE: we cannot always restore the FPU state if the image come |
| 2398 | from a host with a different 'USE_X86LDOUBLE' define. We guess |
| 2399 | if we are in an MMX state to restore correctly in that case. */ |
| 2400 | guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2401 | for(i = 0; i < 8; i++) { |
| 2402 | uint64_t mant; |
| 2403 | uint16_t exp; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2404 | |
| 2405 | switch(fpregs_format) { |
| 2406 | case 0: |
| 2407 | mant = qemu_get_be64(f); |
| 2408 | exp = qemu_get_be16(f); |
| 2409 | #ifdef USE_X86LDOUBLE |
| 2410 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 2411 | #else |
| 2412 | /* difficult case */ |
| 2413 | if (guess_mmx) |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2414 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2415 | else |
| 2416 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 2417 | #endif |
| 2418 | break; |
| 2419 | case 1: |
| 2420 | mant = qemu_get_be64(f); |
| 2421 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2422 | { |
| 2423 | union x86_longdouble *p; |
| 2424 | /* difficult case */ |
| 2425 | p = (void *)&env->fpregs[i]; |
| 2426 | if (guess_mmx) { |
| 2427 | p->mant = mant; |
| 2428 | p->exp = 0xffff; |
| 2429 | } else { |
| 2430 | fp64_to_fp80(p, mant); |
| 2431 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2432 | } |
| 2433 | #else |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 2434 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2435 | #endif |
| 2436 | break; |
| 2437 | default: |
| 2438 | return -EINVAL; |
| 2439 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | env->fpuc = fpuc; |
bellard | 7a0e1f4 | 2005-03-13 17:01:47 +0000 | [diff] [blame] | 2443 | /* XXX: restore FPU round state */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2444 | env->fpstt = (fpus >> 11) & 7; |
| 2445 | env->fpus = fpus & ~0x3800; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2446 | fptag ^= 0xff; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2447 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2448 | env->fptags[i] = (fptag >> i) & 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | for(i = 0; i < 6; i++) |
| 2452 | cpu_get_seg(f, &env->segs[i]); |
| 2453 | cpu_get_seg(f, &env->ldt); |
| 2454 | cpu_get_seg(f, &env->tr); |
| 2455 | cpu_get_seg(f, &env->gdt); |
| 2456 | cpu_get_seg(f, &env->idt); |
| 2457 | |
| 2458 | qemu_get_be32s(f, &env->sysenter_cs); |
| 2459 | qemu_get_be32s(f, &env->sysenter_esp); |
| 2460 | qemu_get_be32s(f, &env->sysenter_eip); |
| 2461 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2462 | qemu_get_betls(f, &env->cr[0]); |
| 2463 | qemu_get_betls(f, &env->cr[2]); |
| 2464 | qemu_get_betls(f, &env->cr[3]); |
| 2465 | qemu_get_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2466 | |
| 2467 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 2468 | qemu_get_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2469 | |
| 2470 | /* MMU */ |
| 2471 | qemu_get_be32s(f, &env->a20_mask); |
| 2472 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2473 | qemu_get_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 2474 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 2475 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 2476 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 2477 | } |
| 2478 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 2479 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 2480 | qemu_get_be64s(f, &env->efer); |
| 2481 | qemu_get_be64s(f, &env->star); |
| 2482 | qemu_get_be64s(f, &env->lstar); |
| 2483 | qemu_get_be64s(f, &env->cstar); |
| 2484 | qemu_get_be64s(f, &env->fmask); |
| 2485 | qemu_get_be64s(f, &env->kernelgsbase); |
| 2486 | #endif |
| 2487 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2488 | /* XXX: compute hflags from scratch, except for CPL and IIF */ |
| 2489 | env->hflags = hflags; |
| 2490 | tlb_flush(env, 1); |
| 2491 | return 0; |
| 2492 | } |
| 2493 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 2494 | #elif defined(TARGET_PPC) |
| 2495 | void cpu_save(QEMUFile *f, void *opaque) |
| 2496 | { |
| 2497 | } |
| 2498 | |
| 2499 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 2500 | { |
| 2501 | return 0; |
| 2502 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 2503 | |
| 2504 | #elif defined(TARGET_MIPS) |
| 2505 | void cpu_save(QEMUFile *f, void *opaque) |
| 2506 | { |
| 2507 | } |
| 2508 | |
| 2509 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 2510 | { |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 2514 | #elif defined(TARGET_SPARC) |
| 2515 | void cpu_save(QEMUFile *f, void *opaque) |
| 2516 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2517 | CPUState *env = opaque; |
| 2518 | int i; |
| 2519 | uint32_t tmp; |
| 2520 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2521 | for(i = 0; i < 8; i++) |
| 2522 | qemu_put_betls(f, &env->gregs[i]); |
| 2523 | for(i = 0; i < NWINDOWS * 16; i++) |
| 2524 | qemu_put_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2527 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 2528 | union { |
| 2529 | TARGET_FPREG_T f; |
| 2530 | target_ulong i; |
| 2531 | } u; |
| 2532 | u.f = env->fpr[i]; |
| 2533 | qemu_put_betl(f, u.i); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2534 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2535 | |
| 2536 | qemu_put_betls(f, &env->pc); |
| 2537 | qemu_put_betls(f, &env->npc); |
| 2538 | qemu_put_betls(f, &env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2539 | tmp = GET_PSR(env); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2540 | qemu_put_be32(f, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2541 | qemu_put_betls(f, &env->fsr); |
| 2542 | qemu_put_betls(f, &env->tbr); |
| 2543 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2544 | qemu_put_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2545 | /* MMU */ |
| 2546 | for(i = 0; i < 16; i++) |
| 2547 | qemu_put_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2548 | #endif |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 2552 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2553 | CPUState *env = opaque; |
| 2554 | int i; |
| 2555 | uint32_t tmp; |
| 2556 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2557 | for(i = 0; i < 8; i++) |
| 2558 | qemu_get_betls(f, &env->gregs[i]); |
| 2559 | for(i = 0; i < NWINDOWS * 16; i++) |
| 2560 | qemu_get_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2561 | |
| 2562 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2563 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 2564 | union { |
| 2565 | TARGET_FPREG_T f; |
| 2566 | target_ulong i; |
| 2567 | } u; |
| 2568 | u.i = qemu_get_betl(f); |
| 2569 | env->fpr[i] = u.f; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2570 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 2571 | |
| 2572 | qemu_get_betls(f, &env->pc); |
| 2573 | qemu_get_betls(f, &env->npc); |
| 2574 | qemu_get_betls(f, &env->y); |
| 2575 | tmp = qemu_get_be32(f); |
| 2576 | env->cwp = 0; /* needed to ensure that the wrapping registers are |
| 2577 | correctly updated */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2578 | PUT_PSR(env, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2579 | qemu_get_betls(f, &env->fsr); |
| 2580 | qemu_get_betls(f, &env->tbr); |
| 2581 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2582 | qemu_get_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2583 | /* MMU */ |
| 2584 | for(i = 0; i < 16; i++) |
| 2585 | qemu_get_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2586 | #endif |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2587 | tlb_flush(env, 1); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 2588 | return 0; |
| 2589 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2590 | #else |
| 2591 | |
| 2592 | #warning No CPU save/restore functions |
| 2593 | |
| 2594 | #endif |
| 2595 | |
| 2596 | /***********************************************************/ |
| 2597 | /* ram save/restore */ |
| 2598 | |
| 2599 | /* we just avoid storing empty pages */ |
| 2600 | static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len) |
| 2601 | { |
| 2602 | int i, v; |
| 2603 | |
| 2604 | v = buf[0]; |
| 2605 | for(i = 1; i < len; i++) { |
| 2606 | if (buf[i] != v) |
| 2607 | goto normal_save; |
| 2608 | } |
| 2609 | qemu_put_byte(f, 1); |
| 2610 | qemu_put_byte(f, v); |
| 2611 | return; |
| 2612 | normal_save: |
| 2613 | qemu_put_byte(f, 0); |
| 2614 | qemu_put_buffer(f, buf, len); |
| 2615 | } |
| 2616 | |
| 2617 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 2618 | { |
| 2619 | int v; |
| 2620 | |
| 2621 | v = qemu_get_byte(f); |
| 2622 | switch(v) { |
| 2623 | case 0: |
| 2624 | if (qemu_get_buffer(f, buf, len) != len) |
| 2625 | return -EIO; |
| 2626 | break; |
| 2627 | case 1: |
| 2628 | v = qemu_get_byte(f); |
| 2629 | memset(buf, v, len); |
| 2630 | break; |
| 2631 | default: |
| 2632 | return -EINVAL; |
| 2633 | } |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
| 2637 | static void ram_save(QEMUFile *f, void *opaque) |
| 2638 | { |
| 2639 | int i; |
| 2640 | qemu_put_be32(f, phys_ram_size); |
| 2641 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 2642 | ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 2647 | { |
| 2648 | int i, ret; |
| 2649 | |
| 2650 | if (version_id != 1) |
| 2651 | return -EINVAL; |
| 2652 | if (qemu_get_be32(f) != phys_ram_size) |
| 2653 | return -EINVAL; |
| 2654 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 2655 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 2656 | if (ret) |
| 2657 | return ret; |
| 2658 | } |
| 2659 | return 0; |
| 2660 | } |
| 2661 | |
| 2662 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2663 | /* machine registration */ |
| 2664 | |
| 2665 | QEMUMachine *first_machine = NULL; |
| 2666 | |
| 2667 | int qemu_register_machine(QEMUMachine *m) |
| 2668 | { |
| 2669 | QEMUMachine **pm; |
| 2670 | pm = &first_machine; |
| 2671 | while (*pm != NULL) |
| 2672 | pm = &(*pm)->next; |
| 2673 | m->next = NULL; |
| 2674 | *pm = m; |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | QEMUMachine *find_machine(const char *name) |
| 2679 | { |
| 2680 | QEMUMachine *m; |
| 2681 | |
| 2682 | for(m = first_machine; m != NULL; m = m->next) { |
| 2683 | if (!strcmp(m->name, name)) |
| 2684 | return m; |
| 2685 | } |
| 2686 | return NULL; |
| 2687 | } |
| 2688 | |
| 2689 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2690 | /* main execution loop */ |
| 2691 | |
| 2692 | void gui_update(void *opaque) |
| 2693 | { |
| 2694 | display_state.dpy_refresh(&display_state); |
| 2695 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
| 2696 | } |
| 2697 | |
| 2698 | /* XXX: support several handlers */ |
| 2699 | VMStopHandler *vm_stop_cb; |
| 2700 | VMStopHandler *vm_stop_opaque; |
| 2701 | |
| 2702 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 2703 | { |
| 2704 | vm_stop_cb = cb; |
| 2705 | vm_stop_opaque = opaque; |
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
| 2709 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 2710 | { |
| 2711 | vm_stop_cb = NULL; |
| 2712 | } |
| 2713 | |
| 2714 | void vm_start(void) |
| 2715 | { |
| 2716 | if (!vm_running) { |
| 2717 | cpu_enable_ticks(); |
| 2718 | vm_running = 1; |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | void vm_stop(int reason) |
| 2723 | { |
| 2724 | if (vm_running) { |
| 2725 | cpu_disable_ticks(); |
| 2726 | vm_running = 0; |
| 2727 | if (reason != 0) { |
| 2728 | if (vm_stop_cb) { |
| 2729 | vm_stop_cb(vm_stop_opaque, reason); |
| 2730 | } |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 2735 | /* reset/shutdown handler */ |
| 2736 | |
| 2737 | typedef struct QEMUResetEntry { |
| 2738 | QEMUResetHandler *func; |
| 2739 | void *opaque; |
| 2740 | struct QEMUResetEntry *next; |
| 2741 | } QEMUResetEntry; |
| 2742 | |
| 2743 | static QEMUResetEntry *first_reset_entry; |
| 2744 | static int reset_requested; |
| 2745 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2746 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 2747 | |
| 2748 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 2749 | { |
| 2750 | QEMUResetEntry **pre, *re; |
| 2751 | |
| 2752 | pre = &first_reset_entry; |
| 2753 | while (*pre != NULL) |
| 2754 | pre = &(*pre)->next; |
| 2755 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 2756 | re->func = func; |
| 2757 | re->opaque = opaque; |
| 2758 | re->next = NULL; |
| 2759 | *pre = re; |
| 2760 | } |
| 2761 | |
| 2762 | void qemu_system_reset(void) |
| 2763 | { |
| 2764 | QEMUResetEntry *re; |
| 2765 | |
| 2766 | /* reset all devices */ |
| 2767 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 2768 | re->func(re->opaque); |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | void qemu_system_reset_request(void) |
| 2773 | { |
| 2774 | reset_requested = 1; |
| 2775 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
| 2776 | } |
| 2777 | |
| 2778 | void qemu_system_shutdown_request(void) |
| 2779 | { |
| 2780 | shutdown_requested = 1; |
| 2781 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
| 2782 | } |
| 2783 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2784 | void qemu_system_powerdown_request(void) |
| 2785 | { |
| 2786 | powerdown_requested = 1; |
| 2787 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
| 2788 | } |
| 2789 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 2790 | static void main_cpu_reset(void *opaque) |
| 2791 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 2792 | #if defined(TARGET_I386) || defined(TARGET_SPARC) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 2793 | CPUState *env = opaque; |
| 2794 | cpu_reset(env); |
| 2795 | #endif |
| 2796 | } |
| 2797 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2798 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2799 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2800 | #ifndef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2801 | struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2802 | IOHandlerRecord *ioh, *ioh_next; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2803 | uint8_t buf[4096]; |
| 2804 | int n, max_size; |
| 2805 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2806 | int ret; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2807 | |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 2808 | #ifdef _WIN32 |
| 2809 | if (timeout > 0) |
| 2810 | Sleep(timeout); |
| 2811 | #else |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2812 | /* poll any events */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2813 | /* XXX: separate device handlers from system ones */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2814 | pf = ufds; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2815 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2816 | if (!ioh->fd_can_read) { |
| 2817 | max_size = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2818 | pf->fd = ioh->fd; |
| 2819 | pf->events = POLLIN; |
| 2820 | ioh->ufd = pf; |
| 2821 | pf++; |
| 2822 | } else { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2823 | max_size = ioh->fd_can_read(ioh->opaque); |
| 2824 | if (max_size > 0) { |
| 2825 | if (max_size > sizeof(buf)) |
| 2826 | max_size = sizeof(buf); |
| 2827 | pf->fd = ioh->fd; |
| 2828 | pf->events = POLLIN; |
| 2829 | ioh->ufd = pf; |
| 2830 | pf++; |
| 2831 | } else { |
| 2832 | ioh->ufd = NULL; |
| 2833 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2834 | } |
| 2835 | ioh->max_size = max_size; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2836 | } |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 2837 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2838 | ret = poll(ufds, pf - ufds, timeout); |
| 2839 | if (ret > 0) { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2840 | /* XXX: better handling of removal */ |
| 2841 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
| 2842 | ioh_next = ioh->next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2843 | pf = ioh->ufd; |
| 2844 | if (pf) { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2845 | if (pf->revents & POLLIN) { |
| 2846 | if (ioh->max_size == 0) { |
| 2847 | /* just a read event */ |
| 2848 | ioh->fd_read(ioh->opaque, NULL, 0); |
| 2849 | } else { |
| 2850 | n = read(ioh->fd, buf, ioh->max_size); |
| 2851 | if (n >= 0) { |
| 2852 | ioh->fd_read(ioh->opaque, buf, n); |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 2853 | } else if (errno != EAGAIN) { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2854 | ioh->fd_read(ioh->opaque, NULL, -errno); |
| 2855 | } |
| 2856 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2857 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2858 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2861 | #endif /* !defined(_WIN32) */ |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2862 | #if defined(CONFIG_SLIRP) |
| 2863 | /* XXX: merge with poll() */ |
| 2864 | if (slirp_inited) { |
| 2865 | fd_set rfds, wfds, xfds; |
| 2866 | int nfds; |
| 2867 | struct timeval tv; |
| 2868 | |
| 2869 | nfds = -1; |
| 2870 | FD_ZERO(&rfds); |
| 2871 | FD_ZERO(&wfds); |
| 2872 | FD_ZERO(&xfds); |
| 2873 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 2874 | tv.tv_sec = 0; |
| 2875 | tv.tv_usec = 0; |
| 2876 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
| 2877 | if (ret >= 0) { |
| 2878 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 2879 | } |
| 2880 | } |
| 2881 | #endif |
| 2882 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2883 | if (vm_running) { |
| 2884 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 2885 | qemu_get_clock(vm_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2886 | /* run dma transfers, if any */ |
| 2887 | DMA_run(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2888 | } |
bellard | 8dc75d7 | 2004-02-25 23:30:56 +0000 | [diff] [blame] | 2889 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2890 | /* real time timers */ |
| 2891 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 2892 | qemu_get_clock(rt_clock)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | int main_loop(void) |
| 2896 | { |
| 2897 | int ret, timeout; |
| 2898 | CPUState *env = global_env; |
| 2899 | |
| 2900 | for(;;) { |
| 2901 | if (vm_running) { |
| 2902 | ret = cpu_exec(env); |
| 2903 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2904 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | if (reset_requested) { |
| 2908 | reset_requested = 0; |
| 2909 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2910 | ret = EXCP_INTERRUPT; |
| 2911 | } |
| 2912 | if (powerdown_requested) { |
| 2913 | powerdown_requested = 0; |
| 2914 | qemu_system_powerdown(); |
| 2915 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2916 | } |
| 2917 | if (ret == EXCP_DEBUG) { |
| 2918 | vm_stop(EXCP_DEBUG); |
| 2919 | } |
| 2920 | /* if hlt instruction, we wait until the next IRQ */ |
| 2921 | /* XXX: use timeout computed from timers */ |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 2922 | if (ret == EXCP_HLT) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 2923 | timeout = 10; |
| 2924 | else |
| 2925 | timeout = 0; |
| 2926 | } else { |
| 2927 | timeout = 10; |
| 2928 | } |
| 2929 | main_loop_wait(timeout); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2930 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 2931 | cpu_disable_ticks(); |
| 2932 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2935 | void help(void) |
| 2936 | { |
bellard | f5a8510 | 2005-07-24 18:44:56 +0000 | [diff] [blame] | 2937 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2005 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 2938 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2939 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 2940 | "'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] | 2941 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 2942 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 2943 | "-M machine select emulated machine (-M ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 2944 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 2945 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 2946 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2947 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 2948 | "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 2949 | "-snapshot write to temporary files instead of disk image files\n" |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 2950 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2951 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 2952 | #ifndef _WIN32 |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 2953 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 2954 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 2955 | #ifdef HAS_AUDIO |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 2956 | "-enable-audio enable audio support, and all the sound cars\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 2957 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 2958 | "-soundhw c1,... enable audio support\n" |
| 2959 | " and only specified sound cards (comma separated list)\n" |
| 2960 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 2961 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 2962 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 2963 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 2964 | #ifdef TARGET_I386 |
| 2965 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 2966 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 2967 | "-usb enable the USB driver (will be the default soon)\n" |
| 2968 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 2969 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 2970 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 2971 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 2972 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2973 | "Network options:\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2974 | "-nics n simulate 'n' network cards [default=1]\n" |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 2975 | "-macaddr addr set the mac address of the first interface\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2976 | "-n script set tap/tun network init script [default=%s]\n" |
| 2977 | "-tun-fd fd use this fd as already opened tap/tun interface\n" |
| 2978 | #ifdef CONFIG_SLIRP |
| 2979 | "-user-net use user mode network stack [default if no tap/tun script]\n" |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 2980 | "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 2981 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 2982 | "-smb dir allow SMB access to files in 'dir' [-user-net]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 2983 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 2984 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
| 2985 | " redirect TCP or UDP connections from host to guest [-user-net]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2986 | #endif |
| 2987 | "-dummy-net use dummy network stack\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2988 | "\n" |
| 2989 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 2990 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 2991 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 2992 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 2993 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2994 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2995 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 2996 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 2997 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2998 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 2999 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3000 | "-s wait gdb connection to port %d\n" |
| 3001 | "-p port change gdb connection port\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 3002 | "-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] | 3003 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 3004 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3005 | "-L path set the directory for the BIOS and VGA BIOS\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 3006 | #ifdef USE_KQEMU |
| 3007 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 3008 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 3009 | #ifdef USE_CODE_COPY |
| 3010 | "-no-code-copy disable code copy acceleration\n" |
| 3011 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3012 | #ifdef TARGET_I386 |
| 3013 | "-isa simulate an ISA-only system (default is PCI system)\n" |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 3014 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 3015 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3016 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3017 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3018 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3019 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 3020 | "ctrl-alt-f toggle full screen\n" |
| 3021 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 3022 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3023 | "\n" |
| 3024 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 3025 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 3026 | #ifdef CONFIG_SOFTMMU |
| 3027 | "qemu", |
| 3028 | #else |
| 3029 | "qemu-fast", |
| 3030 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 3031 | DEFAULT_RAM_SIZE, |
| 3032 | DEFAULT_NETWORK_SCRIPT, |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 3033 | DEFAULT_GDBSTUB_PORT, |
| 3034 | "/tmp/qemu.log"); |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 3035 | #ifndef CONFIG_SOFTMMU |
| 3036 | printf("\n" |
| 3037 | "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n" |
| 3038 | "work. Please use the 'qemu' executable to have a more accurate (but slower)\n" |
| 3039 | "PC emulation.\n"); |
| 3040 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3041 | exit(1); |
| 3042 | } |
| 3043 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3044 | #define HAS_ARG 0x0001 |
| 3045 | |
| 3046 | enum { |
| 3047 | QEMU_OPTION_h, |
| 3048 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3049 | QEMU_OPTION_M, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3050 | QEMU_OPTION_fda, |
| 3051 | QEMU_OPTION_fdb, |
| 3052 | QEMU_OPTION_hda, |
| 3053 | QEMU_OPTION_hdb, |
| 3054 | QEMU_OPTION_hdc, |
| 3055 | QEMU_OPTION_hdd, |
| 3056 | QEMU_OPTION_cdrom, |
| 3057 | QEMU_OPTION_boot, |
| 3058 | QEMU_OPTION_snapshot, |
| 3059 | QEMU_OPTION_m, |
| 3060 | QEMU_OPTION_nographic, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3061 | #ifdef HAS_AUDIO |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3062 | QEMU_OPTION_enable_audio, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3063 | QEMU_OPTION_audio_help, |
| 3064 | QEMU_OPTION_soundhw, |
| 3065 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3066 | |
| 3067 | QEMU_OPTION_nics, |
| 3068 | QEMU_OPTION_macaddr, |
| 3069 | QEMU_OPTION_n, |
| 3070 | QEMU_OPTION_tun_fd, |
| 3071 | QEMU_OPTION_user_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 3072 | QEMU_OPTION_tftp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3073 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3074 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3075 | QEMU_OPTION_dummy_net, |
| 3076 | |
| 3077 | QEMU_OPTION_kernel, |
| 3078 | QEMU_OPTION_append, |
| 3079 | QEMU_OPTION_initrd, |
| 3080 | |
| 3081 | QEMU_OPTION_S, |
| 3082 | QEMU_OPTION_s, |
| 3083 | QEMU_OPTION_p, |
| 3084 | QEMU_OPTION_d, |
| 3085 | QEMU_OPTION_hdachs, |
| 3086 | QEMU_OPTION_L, |
| 3087 | QEMU_OPTION_no_code_copy, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 3088 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 3089 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 3090 | QEMU_OPTION_cirrusvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 3091 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 3092 | QEMU_OPTION_std_vga, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3093 | QEMU_OPTION_monitor, |
| 3094 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3095 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3096 | QEMU_OPTION_loadvm, |
| 3097 | QEMU_OPTION_full_screen, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 3098 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 3099 | QEMU_OPTION_no_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 3100 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 3101 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3102 | QEMU_OPTION_usbdevice, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3103 | }; |
| 3104 | |
| 3105 | typedef struct QEMUOption { |
| 3106 | const char *name; |
| 3107 | int flags; |
| 3108 | int index; |
| 3109 | } QEMUOption; |
| 3110 | |
| 3111 | const QEMUOption qemu_options[] = { |
| 3112 | { "h", 0, QEMU_OPTION_h }, |
| 3113 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3114 | { "M", HAS_ARG, QEMU_OPTION_M }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3115 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 3116 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 3117 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 3118 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 3119 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 3120 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
| 3121 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
| 3122 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 3123 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
| 3124 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 3125 | { "nographic", 0, QEMU_OPTION_nographic }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 3126 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3127 | #ifdef HAS_AUDIO |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3128 | { "enable-audio", 0, QEMU_OPTION_enable_audio }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3129 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 3130 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 3131 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3132 | |
| 3133 | { "nics", HAS_ARG, QEMU_OPTION_nics}, |
| 3134 | { "macaddr", HAS_ARG, QEMU_OPTION_macaddr}, |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 3135 | { "n", HAS_ARG, QEMU_OPTION_n }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3136 | { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 3137 | #ifdef CONFIG_SLIRP |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3138 | { "user-net", 0, QEMU_OPTION_user_net }, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 3139 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3140 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3141 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3142 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3143 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 3144 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3145 | { "dummy-net", 0, QEMU_OPTION_dummy_net }, |
| 3146 | |
| 3147 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 3148 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 3149 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 3150 | |
| 3151 | { "S", 0, QEMU_OPTION_S }, |
| 3152 | { "s", 0, QEMU_OPTION_s }, |
| 3153 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 3154 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 3155 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 3156 | { "L", HAS_ARG, QEMU_OPTION_L }, |
| 3157 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 3158 | #ifdef USE_KQEMU |
| 3159 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
| 3160 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 3161 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 3162 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 3163 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 3164 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 3165 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3166 | { "monitor", 1, QEMU_OPTION_monitor }, |
| 3167 | { "serial", 1, QEMU_OPTION_serial }, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3168 | { "parallel", 1, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3169 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 3170 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 3171 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 3172 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3173 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 3174 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 3175 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3176 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 3177 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3178 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3179 | }; |
| 3180 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 3181 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 3182 | |
| 3183 | /* this stack is only used during signal handling */ |
| 3184 | #define SIGNAL_STACK_SIZE 32768 |
| 3185 | |
| 3186 | static uint8_t *signal_stack; |
| 3187 | |
| 3188 | #endif |
| 3189 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3190 | /* password input */ |
| 3191 | |
| 3192 | static BlockDriverState *get_bdrv(int index) |
| 3193 | { |
| 3194 | BlockDriverState *bs; |
| 3195 | |
| 3196 | if (index < 4) { |
| 3197 | bs = bs_table[index]; |
| 3198 | } else if (index < 6) { |
| 3199 | bs = fd_table[index - 4]; |
| 3200 | } else { |
| 3201 | bs = NULL; |
| 3202 | } |
| 3203 | return bs; |
| 3204 | } |
| 3205 | |
| 3206 | static void read_passwords(void) |
| 3207 | { |
| 3208 | BlockDriverState *bs; |
| 3209 | int i, j; |
| 3210 | char password[256]; |
| 3211 | |
| 3212 | for(i = 0; i < 6; i++) { |
| 3213 | bs = get_bdrv(i); |
| 3214 | if (bs && bdrv_is_encrypted(bs)) { |
| 3215 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); |
| 3216 | for(j = 0; j < 3; j++) { |
| 3217 | monitor_readline("Password: ", |
| 3218 | 1, password, sizeof(password)); |
| 3219 | if (bdrv_set_key(bs, password) == 0) |
| 3220 | break; |
| 3221 | term_printf("invalid password\n"); |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3227 | /* XXX: currently we cannot use simultaneously different CPUs */ |
| 3228 | void register_machines(void) |
| 3229 | { |
| 3230 | #if defined(TARGET_I386) |
| 3231 | qemu_register_machine(&pc_machine); |
bellard | 3dbbdc2 | 2005-11-06 18:20:37 +0000 | [diff] [blame^] | 3232 | qemu_register_machine(&isapc_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3233 | #elif defined(TARGET_PPC) |
| 3234 | qemu_register_machine(&heathrow_machine); |
| 3235 | qemu_register_machine(&core99_machine); |
| 3236 | qemu_register_machine(&prep_machine); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 3237 | #elif defined(TARGET_MIPS) |
| 3238 | qemu_register_machine(&mips_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3239 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3240 | #ifdef TARGET_SPARC64 |
| 3241 | qemu_register_machine(&sun4u_machine); |
| 3242 | #else |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3243 | qemu_register_machine(&sun4m_machine); |
| 3244 | #endif |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3245 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3248 | #ifdef HAS_AUDIO |
| 3249 | static void select_soundhw (const char *optarg) |
| 3250 | { |
| 3251 | if (*optarg == '?') { |
| 3252 | show_valid_cards: |
| 3253 | printf ("Valid sound card names (comma separated):\n"); |
| 3254 | printf ("sb16 Creative Sound Blaster 16\n"); |
| 3255 | #ifdef CONFIG_ADLIB |
| 3256 | #ifdef HAS_YMF262 |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 3257 | printf ("adlib Yamaha YMF262 (OPL3)\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3258 | #else |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 3259 | printf ("adlib Yamaha YM3812 (OPL2)\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3260 | #endif |
| 3261 | #endif |
| 3262 | #ifdef CONFIG_GUS |
| 3263 | printf ("gus Gravis Ultrasound GF1\n"); |
| 3264 | #endif |
| 3265 | printf ("es1370 ENSONIQ AudioPCI ES1370\n"); |
| 3266 | exit (*optarg != '?'); |
| 3267 | } |
| 3268 | else { |
| 3269 | struct { |
| 3270 | char *name; |
| 3271 | int *enabledp; |
| 3272 | } soundhw_tab[] = { |
| 3273 | { "sb16", &sb16_enabled }, |
| 3274 | #ifdef CONFIG_ADLIB |
| 3275 | { "adlib", &adlib_enabled }, |
| 3276 | #endif |
| 3277 | #ifdef CONFIG_GUS |
| 3278 | { "gus", &gus_enabled }, |
| 3279 | #endif |
| 3280 | { "es1370", &es1370_enabled }, |
| 3281 | }; |
| 3282 | size_t tablen, l, i; |
| 3283 | const char *p; |
| 3284 | char *e; |
| 3285 | int bad_card = 0; |
| 3286 | |
| 3287 | p = optarg; |
| 3288 | tablen = sizeof (soundhw_tab) / sizeof (soundhw_tab[0]); |
| 3289 | |
| 3290 | while (*p) { |
| 3291 | e = strchr (p, ','); |
| 3292 | l = !e ? strlen (p) : (size_t) (e - p); |
| 3293 | for (i = 0; i < tablen; ++i) { |
| 3294 | if (!strncmp (soundhw_tab[i].name, p, l)) { |
| 3295 | audio_enabled = 1; |
| 3296 | *soundhw_tab[i].enabledp = 1; |
| 3297 | break; |
| 3298 | } |
| 3299 | } |
| 3300 | if (i == tablen) { |
| 3301 | if (l > 80) { |
| 3302 | fprintf (stderr, |
| 3303 | "Unknown sound card name (too big to show)\n"); |
| 3304 | } |
| 3305 | else { |
| 3306 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 3307 | (int) l, p); |
| 3308 | } |
| 3309 | bad_card = 1; |
| 3310 | } |
| 3311 | p += l + (e != NULL); |
| 3312 | } |
| 3313 | |
| 3314 | if (bad_card) |
| 3315 | goto show_valid_cards; |
| 3316 | } |
| 3317 | } |
| 3318 | #endif |
| 3319 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3320 | #define NET_IF_TUN 0 |
| 3321 | #define NET_IF_USER 1 |
| 3322 | #define NET_IF_DUMMY 2 |
| 3323 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3324 | int main(int argc, char **argv) |
| 3325 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3326 | #ifdef CONFIG_GDBSTUB |
| 3327 | int use_gdbstub, gdbstub_port; |
| 3328 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3329 | int i, cdrom_index; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3330 | int snapshot, linux_boot; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3331 | CPUState *env; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3332 | const char *initrd_filename; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3333 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3334 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3335 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3336 | int cyls, heads, secs, translation; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 3337 | int start_emulation = 1; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3338 | uint8_t macaddr[6]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3339 | int net_if_type, nb_tun_fds, tun_fds[MAX_NICS]; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3340 | int optind; |
| 3341 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3342 | CharDriverState *monitor_hd; |
| 3343 | char monitor_device[128]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 3344 | char serial_devices[MAX_SERIAL_PORTS][128]; |
| 3345 | int serial_device_index; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3346 | char parallel_devices[MAX_PARALLEL_PORTS][128]; |
| 3347 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3348 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3349 | QEMUMachine *machine; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3350 | char usb_devices[MAX_VM_USB_PORTS][128]; |
| 3351 | int usb_devices_index; |
| 3352 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3353 | #if !defined(CONFIG_SOFTMMU) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3354 | /* we never want that malloc() uses mmap() */ |
| 3355 | mallopt(M_MMAP_THRESHOLD, 4096 * 1024); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3356 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3357 | register_machines(); |
| 3358 | machine = first_machine; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3359 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3360 | for(i = 0; i < MAX_FD; i++) |
| 3361 | fd_filename[i] = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3362 | for(i = 0; i < MAX_DISKS; i++) |
| 3363 | hd_filename[i] = NULL; |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 3364 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3365 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 3366 | bios_size = BIOS_SIZE; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3367 | pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3368 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3369 | use_gdbstub = 0; |
| 3370 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3371 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3372 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3373 | nographic = 0; |
| 3374 | kernel_filename = NULL; |
| 3375 | kernel_cmdline = ""; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3376 | #ifdef TARGET_PPC |
| 3377 | cdrom_index = 1; |
| 3378 | #else |
| 3379 | cdrom_index = 2; |
| 3380 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3381 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3382 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3383 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3384 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 3385 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
| 3386 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
| 3387 | serial_devices[i][0] = '\0'; |
| 3388 | serial_device_index = 0; |
| 3389 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3390 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
| 3391 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
| 3392 | parallel_devices[i][0] = '\0'; |
| 3393 | parallel_device_index = 0; |
| 3394 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3395 | usb_devices_index = 0; |
| 3396 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3397 | nb_tun_fds = 0; |
| 3398 | net_if_type = -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3399 | nb_nics = 1; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3400 | /* default mac address of the first network interface */ |
| 3401 | macaddr[0] = 0x52; |
| 3402 | macaddr[1] = 0x54; |
| 3403 | macaddr[2] = 0x00; |
| 3404 | macaddr[3] = 0x12; |
| 3405 | macaddr[4] = 0x34; |
| 3406 | macaddr[5] = 0x56; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3407 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3408 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3409 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3410 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3411 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3412 | r = argv[optind]; |
| 3413 | if (r[0] != '-') { |
| 3414 | hd_filename[0] = argv[optind++]; |
| 3415 | } else { |
| 3416 | const QEMUOption *popt; |
| 3417 | |
| 3418 | optind++; |
| 3419 | popt = qemu_options; |
| 3420 | for(;;) { |
| 3421 | if (!popt->name) { |
| 3422 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 3423 | argv[0], r); |
| 3424 | exit(1); |
| 3425 | } |
| 3426 | if (!strcmp(popt->name, r + 1)) |
| 3427 | break; |
| 3428 | popt++; |
| 3429 | } |
| 3430 | if (popt->flags & HAS_ARG) { |
| 3431 | if (optind >= argc) { |
| 3432 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 3433 | argv[0], r); |
| 3434 | exit(1); |
| 3435 | } |
| 3436 | optarg = argv[optind++]; |
| 3437 | } else { |
| 3438 | optarg = NULL; |
| 3439 | } |
| 3440 | |
| 3441 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3442 | case QEMU_OPTION_M: |
| 3443 | machine = find_machine(optarg); |
| 3444 | if (!machine) { |
| 3445 | QEMUMachine *m; |
| 3446 | printf("Supported machines are:\n"); |
| 3447 | for(m = first_machine; m != NULL; m = m->next) { |
| 3448 | printf("%-10s %s%s\n", |
| 3449 | m->name, m->desc, |
| 3450 | m == first_machine ? " (default)" : ""); |
| 3451 | } |
| 3452 | exit(1); |
| 3453 | } |
| 3454 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3455 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3456 | initrd_filename = optarg; |
| 3457 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3458 | case QEMU_OPTION_hda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3459 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3460 | case QEMU_OPTION_hdc: |
| 3461 | case QEMU_OPTION_hdd: |
| 3462 | { |
| 3463 | int hd_index; |
| 3464 | hd_index = popt->index - QEMU_OPTION_hda; |
| 3465 | hd_filename[hd_index] = optarg; |
| 3466 | if (hd_index == cdrom_index) |
| 3467 | cdrom_index = -1; |
| 3468 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 3469 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3470 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3471 | snapshot = 1; |
| 3472 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3473 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3474 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3475 | const char *p; |
| 3476 | p = optarg; |
| 3477 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3478 | if (cyls < 1 || cyls > 16383) |
| 3479 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3480 | if (*p != ',') |
| 3481 | goto chs_fail; |
| 3482 | p++; |
| 3483 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3484 | if (heads < 1 || heads > 16) |
| 3485 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3486 | if (*p != ',') |
| 3487 | goto chs_fail; |
| 3488 | p++; |
| 3489 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3490 | if (secs < 1 || secs > 63) |
| 3491 | goto chs_fail; |
| 3492 | if (*p == ',') { |
| 3493 | p++; |
| 3494 | if (!strcmp(p, "none")) |
| 3495 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 3496 | else if (!strcmp(p, "lba")) |
| 3497 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 3498 | else if (!strcmp(p, "auto")) |
| 3499 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 3500 | else |
| 3501 | goto chs_fail; |
| 3502 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3503 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3504 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 3505 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3506 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3507 | } |
| 3508 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3509 | case QEMU_OPTION_nographic: |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3510 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 3511 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3512 | nographic = 1; |
| 3513 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3514 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3515 | kernel_filename = optarg; |
| 3516 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3517 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3518 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3519 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3520 | case QEMU_OPTION_tun_fd: |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3521 | { |
| 3522 | const char *p; |
| 3523 | int fd; |
bellard | d6b86f4 | 2004-05-12 19:59:20 +0000 | [diff] [blame] | 3524 | net_if_type = NET_IF_TUN; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3525 | if (nb_tun_fds < MAX_NICS) { |
| 3526 | fd = strtol(optarg, (char **)&p, 0); |
| 3527 | if (*p != '\0') { |
| 3528 | fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3529 | exit(1); |
| 3530 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3531 | tun_fds[nb_tun_fds++] = fd; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3532 | } |
| 3533 | } |
bellard | 42f1e0e | 2003-09-30 22:11:17 +0000 | [diff] [blame] | 3534 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3535 | case QEMU_OPTION_cdrom: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3536 | if (cdrom_index >= 0) { |
| 3537 | hd_filename[cdrom_index] = optarg; |
| 3538 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 3539 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3540 | case QEMU_OPTION_boot: |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 3541 | boot_device = optarg[0]; |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 3542 | if (boot_device != 'a' && |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 3543 | #ifdef TARGET_SPARC |
| 3544 | // Network boot |
| 3545 | boot_device != 'n' && |
| 3546 | #endif |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3547 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 3548 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 3549 | exit(1); |
| 3550 | } |
| 3551 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3552 | case QEMU_OPTION_fda: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3553 | fd_filename[0] = optarg; |
| 3554 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3555 | case QEMU_OPTION_fdb: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3556 | fd_filename[1] = optarg; |
| 3557 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3558 | case QEMU_OPTION_no_code_copy: |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 3559 | code_copy_enabled = 0; |
| 3560 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3561 | case QEMU_OPTION_nics: |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3562 | nb_nics = atoi(optarg); |
bellard | 3a1bc17 | 2004-05-12 19:54:43 +0000 | [diff] [blame] | 3563 | if (nb_nics < 0 || nb_nics > MAX_NICS) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3564 | fprintf(stderr, "qemu: invalid number of network interfaces\n"); |
| 3565 | exit(1); |
| 3566 | } |
| 3567 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3568 | case QEMU_OPTION_macaddr: |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3569 | { |
| 3570 | const char *p; |
| 3571 | int i; |
| 3572 | p = optarg; |
| 3573 | for(i = 0; i < 6; i++) { |
| 3574 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 3575 | if (i == 5) { |
| 3576 | if (*p != '\0') |
| 3577 | goto macaddr_error; |
| 3578 | } else { |
| 3579 | if (*p != ':') { |
| 3580 | macaddr_error: |
| 3581 | fprintf(stderr, "qemu: invalid syntax for ethernet address\n"); |
| 3582 | exit(1); |
| 3583 | } |
| 3584 | p++; |
| 3585 | } |
| 3586 | } |
| 3587 | } |
| 3588 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 3589 | #ifdef CONFIG_SLIRP |
| 3590 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 3591 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3592 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3593 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3594 | case QEMU_OPTION_smb: |
| 3595 | net_slirp_smb(optarg); |
| 3596 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3597 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3598 | case QEMU_OPTION_user_net: |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3599 | net_if_type = NET_IF_USER; |
| 3600 | break; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3601 | case QEMU_OPTION_redir: |
| 3602 | net_slirp_redir(optarg); |
| 3603 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 3604 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3605 | case QEMU_OPTION_dummy_net: |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3606 | net_if_type = NET_IF_DUMMY; |
| 3607 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3608 | #ifdef HAS_AUDIO |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3609 | case QEMU_OPTION_enable_audio: |
bellard | aaaa7df | 2004-04-26 20:56:53 +0000 | [diff] [blame] | 3610 | audio_enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3611 | sb16_enabled = 1; |
| 3612 | adlib_enabled = 1; |
| 3613 | gus_enabled = 1; |
| 3614 | es1370_enabled = 1; |
bellard | aaaa7df | 2004-04-26 20:56:53 +0000 | [diff] [blame] | 3615 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 3616 | case QEMU_OPTION_audio_help: |
| 3617 | AUD_help (); |
| 3618 | exit (0); |
| 3619 | break; |
| 3620 | case QEMU_OPTION_soundhw: |
| 3621 | select_soundhw (optarg); |
| 3622 | break; |
| 3623 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3624 | case QEMU_OPTION_h: |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3625 | help(); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3626 | break; |
| 3627 | case QEMU_OPTION_m: |
| 3628 | ram_size = atoi(optarg) * 1024 * 1024; |
| 3629 | if (ram_size <= 0) |
| 3630 | help(); |
| 3631 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
| 3632 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
| 3633 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 3634 | exit(1); |
| 3635 | } |
| 3636 | break; |
| 3637 | case QEMU_OPTION_d: |
| 3638 | { |
| 3639 | int mask; |
| 3640 | CPULogItem *item; |
| 3641 | |
| 3642 | mask = cpu_str_to_log_mask(optarg); |
| 3643 | if (!mask) { |
| 3644 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 3645 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 3646 | printf("%-10s %s\n", item->name, item->help); |
| 3647 | } |
| 3648 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3649 | } |
| 3650 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 3651 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3652 | break; |
| 3653 | case QEMU_OPTION_n: |
| 3654 | pstrcpy(network_script, sizeof(network_script), optarg); |
| 3655 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3656 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3657 | case QEMU_OPTION_s: |
| 3658 | use_gdbstub = 1; |
| 3659 | break; |
| 3660 | case QEMU_OPTION_p: |
| 3661 | gdbstub_port = atoi(optarg); |
| 3662 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3663 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3664 | case QEMU_OPTION_L: |
| 3665 | bios_dir = optarg; |
| 3666 | break; |
| 3667 | case QEMU_OPTION_S: |
| 3668 | start_emulation = 0; |
| 3669 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 3670 | case QEMU_OPTION_k: |
| 3671 | keyboard_layout = optarg; |
| 3672 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 3673 | case QEMU_OPTION_localtime: |
| 3674 | rtc_utc = 0; |
| 3675 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 3676 | case QEMU_OPTION_cirrusvga: |
| 3677 | cirrus_vga_enabled = 1; |
| 3678 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 3679 | case QEMU_OPTION_std_vga: |
| 3680 | cirrus_vga_enabled = 0; |
| 3681 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 3682 | case QEMU_OPTION_g: |
| 3683 | { |
| 3684 | const char *p; |
| 3685 | int w, h, depth; |
| 3686 | p = optarg; |
| 3687 | w = strtol(p, (char **)&p, 10); |
| 3688 | if (w <= 0) { |
| 3689 | graphic_error: |
| 3690 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 3691 | exit(1); |
| 3692 | } |
| 3693 | if (*p != 'x') |
| 3694 | goto graphic_error; |
| 3695 | p++; |
| 3696 | h = strtol(p, (char **)&p, 10); |
| 3697 | if (h <= 0) |
| 3698 | goto graphic_error; |
| 3699 | if (*p == 'x') { |
| 3700 | p++; |
| 3701 | depth = strtol(p, (char **)&p, 10); |
| 3702 | if (depth != 8 && depth != 15 && depth != 16 && |
| 3703 | depth != 24 && depth != 32) |
| 3704 | goto graphic_error; |
| 3705 | } else if (*p == '\0') { |
| 3706 | depth = graphic_depth; |
| 3707 | } else { |
| 3708 | goto graphic_error; |
| 3709 | } |
| 3710 | |
| 3711 | graphic_width = w; |
| 3712 | graphic_height = h; |
| 3713 | graphic_depth = depth; |
| 3714 | } |
| 3715 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3716 | case QEMU_OPTION_monitor: |
| 3717 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); |
| 3718 | break; |
| 3719 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 3720 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 3721 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 3722 | exit(1); |
| 3723 | } |
| 3724 | pstrcpy(serial_devices[serial_device_index], |
| 3725 | sizeof(serial_devices[0]), optarg); |
| 3726 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3727 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3728 | case QEMU_OPTION_parallel: |
| 3729 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 3730 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 3731 | exit(1); |
| 3732 | } |
| 3733 | pstrcpy(parallel_devices[parallel_device_index], |
| 3734 | sizeof(parallel_devices[0]), optarg); |
| 3735 | parallel_device_index++; |
| 3736 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3737 | case QEMU_OPTION_loadvm: |
| 3738 | loadvm = optarg; |
| 3739 | break; |
| 3740 | case QEMU_OPTION_full_screen: |
| 3741 | full_screen = 1; |
| 3742 | break; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 3743 | case QEMU_OPTION_pidfile: |
| 3744 | create_pidfile(optarg); |
| 3745 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 3746 | #ifdef TARGET_I386 |
| 3747 | case QEMU_OPTION_win2k_hack: |
| 3748 | win2k_install_hack = 1; |
| 3749 | break; |
| 3750 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 3751 | #ifdef USE_KQEMU |
| 3752 | case QEMU_OPTION_no_kqemu: |
| 3753 | kqemu_allowed = 0; |
| 3754 | break; |
| 3755 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 3756 | case QEMU_OPTION_usb: |
| 3757 | usb_enabled = 1; |
| 3758 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3759 | case QEMU_OPTION_usbdevice: |
| 3760 | usb_enabled = 1; |
| 3761 | if (usb_devices_index >= MAX_VM_USB_PORTS) { |
| 3762 | fprintf(stderr, "Too many USB devices\n"); |
| 3763 | exit(1); |
| 3764 | } |
| 3765 | pstrcpy(usb_devices[usb_devices_index], |
| 3766 | sizeof(usb_devices[usb_devices_index]), |
| 3767 | optarg); |
| 3768 | usb_devices_index++; |
| 3769 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 3770 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3771 | } |
| 3772 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3773 | |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3774 | linux_boot = (kernel_filename != NULL); |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3775 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3776 | if (!linux_boot && |
| 3777 | hd_filename[0] == '\0' && |
| 3778 | (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 3779 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3780 | help(); |
bellard | 8f2b1fb | 2003-11-16 19:46:01 +0000 | [diff] [blame] | 3781 | |
| 3782 | /* boot to cd by default if no hard disk */ |
bellard | d030931 | 2004-01-18 22:35:25 +0000 | [diff] [blame] | 3783 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 3784 | if (fd_filename[0] != '\0') |
| 3785 | boot_device = 'a'; |
| 3786 | else |
| 3787 | boot_device = 'd'; |
| 3788 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3789 | |
bellard | dc887a4 | 2004-01-04 18:18:57 +0000 | [diff] [blame] | 3790 | #if !defined(CONFIG_SOFTMMU) |
| 3791 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
| 3792 | { |
| 3793 | static uint8_t stdout_buf[4096]; |
| 3794 | setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf)); |
| 3795 | } |
| 3796 | #else |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 3797 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | dc887a4 | 2004-01-04 18:18:57 +0000 | [diff] [blame] | 3798 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3799 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3800 | /* init host network redirectors */ |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3801 | if (net_if_type == -1) { |
| 3802 | net_if_type = NET_IF_TUN; |
| 3803 | #if defined(CONFIG_SLIRP) |
| 3804 | if (access(network_script, R_OK) < 0) { |
| 3805 | net_if_type = NET_IF_USER; |
| 3806 | } |
| 3807 | #endif |
| 3808 | } |
| 3809 | |
| 3810 | for(i = 0; i < nb_nics; i++) { |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3811 | NetDriverState *nd = &nd_table[i]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3812 | nd->index = i; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3813 | /* init virtual mac address */ |
| 3814 | nd->macaddr[0] = macaddr[0]; |
| 3815 | nd->macaddr[1] = macaddr[1]; |
| 3816 | nd->macaddr[2] = macaddr[2]; |
| 3817 | nd->macaddr[3] = macaddr[3]; |
| 3818 | nd->macaddr[4] = macaddr[4]; |
| 3819 | nd->macaddr[5] = macaddr[5] + i; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3820 | switch(net_if_type) { |
| 3821 | #if defined(CONFIG_SLIRP) |
| 3822 | case NET_IF_USER: |
| 3823 | net_slirp_init(nd); |
| 3824 | break; |
| 3825 | #endif |
| 3826 | #if !defined(_WIN32) |
| 3827 | case NET_IF_TUN: |
| 3828 | if (i < nb_tun_fds) { |
| 3829 | net_fd_init(nd, tun_fds[i]); |
| 3830 | } else { |
bellard | d927637 | 2004-04-26 19:44:57 +0000 | [diff] [blame] | 3831 | if (net_tun_init(nd) < 0) |
| 3832 | net_dummy_init(nd); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3833 | } |
| 3834 | break; |
| 3835 | #endif |
| 3836 | case NET_IF_DUMMY: |
| 3837 | default: |
| 3838 | net_dummy_init(nd); |
| 3839 | break; |
| 3840 | } |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 3841 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3842 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3843 | /* init the memory */ |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 3844 | phys_ram_size = ram_size + vga_ram_size + bios_size; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3845 | |
| 3846 | #ifdef CONFIG_SOFTMMU |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 3847 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3848 | if (!phys_ram_base) { |
| 3849 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3850 | exit(1); |
| 3851 | } |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3852 | #else |
| 3853 | /* as we must map the same page at several addresses, we must use |
| 3854 | a fd */ |
| 3855 | { |
| 3856 | const char *tmpdir; |
| 3857 | |
| 3858 | tmpdir = getenv("QEMU_TMPDIR"); |
| 3859 | if (!tmpdir) |
| 3860 | tmpdir = "/tmp"; |
| 3861 | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); |
| 3862 | if (mkstemp(phys_ram_file) < 0) { |
| 3863 | fprintf(stderr, "Could not create temporary memory file '%s'\n", |
| 3864 | phys_ram_file); |
| 3865 | exit(1); |
| 3866 | } |
| 3867 | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600); |
| 3868 | if (phys_ram_fd < 0) { |
| 3869 | fprintf(stderr, "Could not open temporary memory file '%s'\n", |
| 3870 | phys_ram_file); |
| 3871 | exit(1); |
| 3872 | } |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3873 | ftruncate(phys_ram_fd, phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3874 | unlink(phys_ram_file); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 3875 | phys_ram_base = mmap(get_mmap_addr(phys_ram_size), |
| 3876 | phys_ram_size, |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 3877 | PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
| 3878 | phys_ram_fd, 0); |
| 3879 | if (phys_ram_base == MAP_FAILED) { |
| 3880 | fprintf(stderr, "Could not map physical memory\n"); |
| 3881 | exit(1); |
| 3882 | } |
| 3883 | } |
| 3884 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3885 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3886 | /* we always create the cdrom drive, even if no disk is there */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3887 | bdrv_init(); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3888 | if (cdrom_index >= 0) { |
| 3889 | bs_table[cdrom_index] = bdrv_new("cdrom"); |
| 3890 | bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3893 | /* open the virtual block devices */ |
| 3894 | for(i = 0; i < MAX_DISKS; i++) { |
| 3895 | if (hd_filename[i]) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3896 | if (!bs_table[i]) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3897 | char buf[64]; |
| 3898 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 3899 | bs_table[i] = bdrv_new(buf); |
| 3900 | } |
| 3901 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3902 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3903 | hd_filename[i]); |
| 3904 | exit(1); |
| 3905 | } |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3906 | if (i == 0 && cyls != 0) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3907 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 3908 | bdrv_set_translation_hint(bs_table[i], translation); |
| 3909 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* we always create at least one floppy disk */ |
| 3914 | fd_table[0] = bdrv_new("fda"); |
| 3915 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 3916 | |
| 3917 | for(i = 0; i < MAX_FD; i++) { |
| 3918 | if (fd_filename[i]) { |
| 3919 | if (!fd_table[i]) { |
| 3920 | char buf[64]; |
| 3921 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 3922 | fd_table[i] = bdrv_new(buf); |
| 3923 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 3924 | } |
| 3925 | if (fd_filename[i] != '\0') { |
| 3926 | if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3927 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3928 | fd_filename[i]); |
| 3929 | exit(1); |
| 3930 | } |
| 3931 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3935 | /* init USB devices */ |
| 3936 | if (usb_enabled) { |
| 3937 | vm_usb_hub = usb_hub_init(vm_usb_ports, MAX_VM_USB_PORTS); |
| 3938 | for(i = 0; i < usb_devices_index; i++) { |
| 3939 | if (usb_device_add(usb_devices[i]) < 0) { |
| 3940 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 3941 | usb_devices[i]); |
| 3942 | } |
| 3943 | } |
| 3944 | } |
| 3945 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3946 | /* init CPU state */ |
| 3947 | env = cpu_init(); |
| 3948 | global_env = env; |
| 3949 | cpu_single_env = env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3950 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3951 | register_savevm("timer", 0, 1, timer_save, timer_load, env); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 3952 | register_savevm("cpu", 0, 3, cpu_save, cpu_load, env); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3953 | register_savevm("ram", 0, 1, ram_save, ram_load, NULL); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3954 | qemu_register_reset(main_cpu_reset, global_env); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3955 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3956 | init_ioports(); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 3957 | cpu_calibrate_ticks(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3958 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3959 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 3960 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3961 | dumb_display_init(ds); |
| 3962 | } else { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 3963 | #if defined(CONFIG_SDL) |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 3964 | sdl_display_init(ds, full_screen); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 3965 | #elif defined(CONFIG_COCOA) |
| 3966 | cocoa_display_init(ds, full_screen); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 3967 | #else |
| 3968 | dumb_display_init(ds); |
| 3969 | #endif |
| 3970 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3971 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3972 | vga_console = graphic_console_init(ds); |
| 3973 | |
| 3974 | monitor_hd = qemu_chr_open(monitor_device); |
| 3975 | if (!monitor_hd) { |
| 3976 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 3977 | exit(1); |
| 3978 | } |
| 3979 | monitor_init(monitor_hd, !nographic); |
| 3980 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 3981 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 3982 | if (serial_devices[i][0] != '\0') { |
| 3983 | serial_hds[i] = qemu_chr_open(serial_devices[i]); |
| 3984 | if (!serial_hds[i]) { |
| 3985 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
| 3986 | serial_devices[i]); |
| 3987 | exit(1); |
| 3988 | } |
| 3989 | if (!strcmp(serial_devices[i], "vc")) |
| 3990 | qemu_chr_printf(serial_hds[i], "serial%d console\n", i); |
| 3991 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3992 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3993 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 3994 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
| 3995 | if (parallel_devices[i][0] != '\0') { |
| 3996 | parallel_hds[i] = qemu_chr_open(parallel_devices[i]); |
| 3997 | if (!parallel_hds[i]) { |
| 3998 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
| 3999 | parallel_devices[i]); |
| 4000 | exit(1); |
| 4001 | } |
| 4002 | if (!strcmp(parallel_devices[i], "vc")) |
| 4003 | qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i); |
| 4004 | } |
| 4005 | } |
| 4006 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4007 | /* setup cpu signal handlers for MMU / self modifying code handling */ |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4008 | #if !defined(CONFIG_SOFTMMU) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4009 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4010 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 4011 | { |
| 4012 | stack_t stk; |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 4013 | signal_stack = memalign(16, SIGNAL_STACK_SIZE); |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4014 | stk.ss_sp = signal_stack; |
| 4015 | stk.ss_size = SIGNAL_STACK_SIZE; |
| 4016 | stk.ss_flags = 0; |
| 4017 | |
| 4018 | if (sigaltstack(&stk, NULL) < 0) { |
| 4019 | perror("sigaltstack"); |
| 4020 | exit(1); |
| 4021 | } |
| 4022 | } |
| 4023 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4024 | { |
| 4025 | struct sigaction act; |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4026 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4027 | sigfillset(&act.sa_mask); |
| 4028 | act.sa_flags = SA_SIGINFO; |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4029 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4030 | act.sa_flags |= SA_ONSTACK; |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4031 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4032 | act.sa_sigaction = host_segv_handler; |
| 4033 | sigaction(SIGSEGV, &act, NULL); |
| 4034 | sigaction(SIGBUS, &act, NULL); |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4035 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4036 | sigaction(SIGFPE, &act, NULL); |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 4037 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4038 | } |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 4039 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4040 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4041 | #ifndef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4042 | { |
| 4043 | struct sigaction act; |
| 4044 | sigfillset(&act.sa_mask); |
| 4045 | act.sa_flags = 0; |
| 4046 | act.sa_handler = SIG_IGN; |
| 4047 | sigaction(SIGPIPE, &act, NULL); |
| 4048 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4049 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 4050 | init_timers(); |
| 4051 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4052 | machine->init(ram_size, vga_ram_size, boot_device, |
| 4053 | ds, fd_filename, snapshot, |
| 4054 | kernel_filename, kernel_cmdline, initrd_filename); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 4055 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4056 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
| 4057 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4058 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4059 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4060 | if (use_gdbstub) { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4061 | if (gdbserver_start(gdbstub_port) < 0) { |
| 4062 | fprintf(stderr, "Could not open gdbserver socket on port %d\n", |
| 4063 | gdbstub_port); |
| 4064 | exit(1); |
| 4065 | } else { |
| 4066 | printf("Waiting gdb connection on port %d\n", gdbstub_port); |
| 4067 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4068 | } else |
| 4069 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4070 | if (loadvm) |
| 4071 | qemu_loadvm(loadvm); |
| 4072 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4073 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4074 | /* XXX: simplify init */ |
| 4075 | read_passwords(); |
| 4076 | if (start_emulation) { |
| 4077 | vm_start(); |
| 4078 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4079 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4080 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 4081 | quit_timers(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4082 | return 0; |
| 4083 | } |