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