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 | */ |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
bellard | 1df912c | 2003-06-25 16:20:35 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 27 | #include <string.h> |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <signal.h> |
| 35 | #include <time.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <malloc.h> |
| 38 | #include <termios.h> |
| 39 | #include <sys/poll.h> |
| 40 | #include <errno.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 41 | #include <sys/wait.h> |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 42 | #include <pty.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 43 | |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <linux/if.h> |
| 47 | #include <linux/if_tun.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 48 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 49 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 50 | #include "thunk.h" |
| 51 | |
| 52 | #include "vl.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 53 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 54 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 55 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 56 | //#define DEBUG_UNUSED_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 57 | |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 58 | #if !defined(CONFIG_SOFTMMU) |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 59 | #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 60 | #else |
| 61 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
| 62 | #endif |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 63 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 64 | #if defined (TARGET_I386) |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 65 | #elif defined (TARGET_PPC) |
| 66 | //#define USE_OPEN_FIRMWARE |
bellard | 3f5dcc3 | 2004-01-18 22:44:01 +0000 | [diff] [blame] | 67 | #if !defined (USE_OPEN_FIRMWARE) |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 68 | #define KERNEL_LOAD_ADDR 0x01000000 |
| 69 | #define KERNEL_STACK_ADDR 0x01200000 |
| 70 | #else |
| 71 | #define KERNEL_LOAD_ADDR 0x00000000 |
| 72 | #define KERNEL_STACK_ADDR 0x00400000 |
| 73 | #endif |
| 74 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 75 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 76 | #define GUI_REFRESH_INTERVAL 30 |
| 77 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 78 | /* XXX: use a two level table to limit memory usage */ |
| 79 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 80 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 81 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 82 | char phys_ram_file[1024]; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 83 | CPUState *global_env; |
| 84 | CPUState *cpu_single_env; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 85 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 86 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 87 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 88 | BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 89 | int vga_ram_size; |
| 90 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 91 | int nographic; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 92 | int term_inited; |
| 93 | int64_t ticks_per_sec; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 94 | int boot_device = 'c'; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 95 | static int ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 96 | static char network_script[1024]; |
| 97 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 98 | int nb_nics; |
| 99 | NetDriverState nd_table[MAX_NICS]; |
| 100 | SerialState *serial_console; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 101 | |
| 102 | /***********************************************************/ |
| 103 | /* x86 io ports */ |
| 104 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 105 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 106 | { |
| 107 | #ifdef DEBUG_UNUSED_IOPORT |
| 108 | fprintf(stderr, "inb: port=0x%04x\n", address); |
| 109 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 110 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 113 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 114 | { |
| 115 | #ifdef DEBUG_UNUSED_IOPORT |
| 116 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 121 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 122 | { |
| 123 | uint32_t data; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 124 | data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address); |
| 125 | data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 126 | return data; |
| 127 | } |
| 128 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 129 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 130 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 131 | ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff); |
| 132 | ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 135 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 136 | { |
| 137 | #ifdef DEBUG_UNUSED_IOPORT |
| 138 | fprintf(stderr, "inl: port=0x%04x\n", address); |
| 139 | #endif |
| 140 | return 0xffffffff; |
| 141 | } |
| 142 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 143 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 144 | { |
| 145 | #ifdef DEBUG_UNUSED_IOPORT |
| 146 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); |
| 147 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void init_ioports(void) |
| 151 | { |
| 152 | int i; |
| 153 | |
| 154 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 155 | ioport_read_table[0][i] = default_ioport_readb; |
| 156 | ioport_write_table[0][i] = default_ioport_writeb; |
| 157 | ioport_read_table[1][i] = default_ioport_readw; |
| 158 | ioport_write_table[1][i] = default_ioport_writew; |
| 159 | ioport_read_table[2][i] = default_ioport_readl; |
| 160 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 164 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 165 | int register_ioport_read(int start, int length, int size, |
| 166 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 167 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 168 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 169 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 170 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 171 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 172 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 173 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 174 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 175 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 176 | } else { |
| 177 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 178 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 179 | } |
| 180 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 181 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 182 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 183 | hw_error("register_ioport_read: invalid opaque"); |
| 184 | ioport_opaque[i] = opaque; |
| 185 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 189 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 190 | int register_ioport_write(int start, int length, int size, |
| 191 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 192 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 193 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 194 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 195 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 196 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 197 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 198 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 199 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 200 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 201 | } else { |
| 202 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 203 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 204 | } |
| 205 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 206 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 207 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 208 | hw_error("register_ioport_read: invalid opaque"); |
| 209 | ioport_opaque[i] = opaque; |
| 210 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 214 | void pstrcpy(char *buf, int buf_size, const char *str) |
| 215 | { |
| 216 | int c; |
| 217 | char *q = buf; |
| 218 | |
| 219 | if (buf_size <= 0) |
| 220 | return; |
| 221 | |
| 222 | for(;;) { |
| 223 | c = *str++; |
| 224 | if (c == 0 || q >= buf + buf_size - 1) |
| 225 | break; |
| 226 | *q++ = c; |
| 227 | } |
| 228 | *q = '\0'; |
| 229 | } |
| 230 | |
| 231 | /* strcat and truncate. */ |
| 232 | char *pstrcat(char *buf, int buf_size, const char *s) |
| 233 | { |
| 234 | int len; |
| 235 | len = strlen(buf); |
| 236 | if (len < buf_size) |
| 237 | pstrcpy(buf + len, buf_size - len, s); |
| 238 | return buf; |
| 239 | } |
| 240 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 241 | /* return the size or -1 if error */ |
| 242 | int load_image(const char *filename, uint8_t *addr) |
| 243 | { |
| 244 | int fd, size; |
| 245 | fd = open(filename, O_RDONLY); |
| 246 | if (fd < 0) |
| 247 | return -1; |
| 248 | size = lseek(fd, 0, SEEK_END); |
| 249 | lseek(fd, 0, SEEK_SET); |
| 250 | if (read(fd, addr, size) != size) { |
| 251 | close(fd); |
| 252 | return -1; |
| 253 | } |
| 254 | close(fd); |
| 255 | return size; |
| 256 | } |
| 257 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 258 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 259 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 260 | addr &= (MAX_IOPORTS - 1); |
| 261 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 264 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 265 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 266 | addr &= (MAX_IOPORTS - 1); |
| 267 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 268 | } |
| 269 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 270 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 271 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 272 | addr &= (MAX_IOPORTS - 1); |
| 273 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 274 | } |
| 275 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 276 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 277 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 278 | addr &= (MAX_IOPORTS - 1); |
| 279 | return ioport_read_table[0][addr](ioport_opaque[addr], addr); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 282 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 283 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 284 | addr &= (MAX_IOPORTS - 1); |
| 285 | return ioport_read_table[1][addr](ioport_opaque[addr], addr); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 286 | } |
| 287 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 288 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 289 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 290 | addr &= (MAX_IOPORTS - 1); |
| 291 | return ioport_read_table[2][addr](ioport_opaque[addr], addr); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 295 | void hw_error(const char *fmt, ...) |
| 296 | { |
| 297 | va_list ap; |
| 298 | |
| 299 | va_start(ap, fmt); |
| 300 | fprintf(stderr, "qemu: hardware error: "); |
| 301 | vfprintf(stderr, fmt, ap); |
| 302 | fprintf(stderr, "\n"); |
| 303 | #ifdef TARGET_I386 |
| 304 | cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 305 | #else |
| 306 | cpu_dump_state(global_env, stderr, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 307 | #endif |
| 308 | va_end(ap); |
| 309 | abort(); |
| 310 | } |
| 311 | |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 312 | #if defined(__powerpc__) |
| 313 | |
| 314 | static inline uint32_t get_tbl(void) |
| 315 | { |
| 316 | uint32_t tbl; |
| 317 | asm volatile("mftb %0" : "=r" (tbl)); |
| 318 | return tbl; |
| 319 | } |
| 320 | |
| 321 | static inline uint32_t get_tbu(void) |
| 322 | { |
| 323 | uint32_t tbl; |
| 324 | asm volatile("mftbu %0" : "=r" (tbl)); |
| 325 | return tbl; |
| 326 | } |
| 327 | |
| 328 | int64_t cpu_get_real_ticks(void) |
| 329 | { |
| 330 | uint32_t l, h, h1; |
| 331 | /* NOTE: we test if wrapping has occurred */ |
| 332 | do { |
| 333 | h = get_tbu(); |
| 334 | l = get_tbl(); |
| 335 | h1 = get_tbu(); |
| 336 | } while (h != h1); |
| 337 | return ((int64_t)h << 32) | l; |
| 338 | } |
| 339 | |
| 340 | #elif defined(__i386__) |
| 341 | |
| 342 | int64_t cpu_get_real_ticks(void) |
| 343 | { |
| 344 | int64_t val; |
| 345 | asm("rdtsc" : "=A" (val)); |
| 346 | return val; |
| 347 | } |
| 348 | |
| 349 | #else |
| 350 | #error unsupported CPU |
| 351 | #endif |
| 352 | |
| 353 | static int64_t cpu_ticks_offset; |
| 354 | static int64_t cpu_ticks_last; |
| 355 | |
| 356 | int64_t cpu_get_ticks(void) |
| 357 | { |
| 358 | return cpu_get_real_ticks() + cpu_ticks_offset; |
| 359 | } |
| 360 | |
| 361 | /* enable cpu_get_ticks() */ |
| 362 | void cpu_enable_ticks(void) |
| 363 | { |
| 364 | cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks(); |
| 365 | } |
| 366 | |
| 367 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 368 | cpu_get_ticks() after that. */ |
| 369 | void cpu_disable_ticks(void) |
| 370 | { |
| 371 | cpu_ticks_last = cpu_get_ticks(); |
| 372 | } |
| 373 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 374 | int64_t get_clock(void) |
| 375 | { |
| 376 | struct timeval tv; |
| 377 | gettimeofday(&tv, NULL); |
| 378 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
| 379 | } |
| 380 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 381 | void cpu_calibrate_ticks(void) |
| 382 | { |
| 383 | int64_t usec, ticks; |
| 384 | |
| 385 | usec = get_clock(); |
| 386 | ticks = cpu_get_ticks(); |
| 387 | usleep(50 * 1000); |
| 388 | usec = get_clock() - usec; |
| 389 | ticks = cpu_get_ticks() - ticks; |
| 390 | ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
| 391 | } |
| 392 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 393 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 394 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 395 | { |
| 396 | union { |
| 397 | uint64_t ll; |
| 398 | struct { |
| 399 | #ifdef WORDS_BIGENDIAN |
| 400 | uint32_t high, low; |
| 401 | #else |
| 402 | uint32_t low, high; |
| 403 | #endif |
| 404 | } l; |
| 405 | } u, res; |
| 406 | uint64_t rl, rh; |
| 407 | |
| 408 | u.ll = a; |
| 409 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 410 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 411 | rh += (rl >> 32); |
| 412 | res.l.high = rh / c; |
| 413 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 414 | return res.ll; |
| 415 | } |
| 416 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 417 | /***********************************************************/ |
| 418 | /* serial device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 419 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 420 | int serial_open_device(void) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 421 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 422 | char slave_name[1024]; |
| 423 | int master_fd, slave_fd; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 424 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 425 | if (serial_console == NULL && nographic) { |
| 426 | /* use console for serial port */ |
| 427 | return 0; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 428 | } else { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 429 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 430 | fprintf(stderr, "warning: could not create pseudo terminal for serial port\n"); |
| 431 | return -1; |
| 432 | } |
| 433 | fprintf(stderr, "Serial port redirected to %s\n", slave_name); |
| 434 | return master_fd; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 438 | /***********************************************************/ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 439 | /* Linux network device redirector */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 440 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 441 | static int tun_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 442 | { |
| 443 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 444 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 445 | |
| 446 | fd = open("/dev/net/tun", O_RDWR); |
| 447 | if (fd < 0) { |
| 448 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 449 | return -1; |
| 450 | } |
| 451 | memset(&ifr, 0, sizeof(ifr)); |
| 452 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 453 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d"); |
| 454 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 455 | if (ret != 0) { |
| 456 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 457 | close(fd); |
| 458 | return -1; |
| 459 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 460 | printf("Connected to host network interface: %s\n", ifr.ifr_name); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 461 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 462 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 463 | return fd; |
| 464 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 465 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 466 | static int net_init(void) |
| 467 | { |
| 468 | int pid, status, launch_script, i; |
| 469 | NetDriverState *nd; |
| 470 | char *args[MAX_NICS + 2]; |
| 471 | char **parg; |
| 472 | |
| 473 | launch_script = 0; |
| 474 | for(i = 0; i < nb_nics; i++) { |
| 475 | nd = &nd_table[i]; |
| 476 | if (nd->fd < 0) { |
| 477 | nd->fd = tun_open(nd->ifname, sizeof(nd->ifname)); |
| 478 | if (nd->fd >= 0) |
| 479 | launch_script = 1; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 480 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 481 | } |
| 482 | |
| 483 | if (launch_script) { |
| 484 | /* try to launch network init script */ |
| 485 | pid = fork(); |
| 486 | if (pid >= 0) { |
| 487 | if (pid == 0) { |
| 488 | parg = args; |
| 489 | *parg++ = network_script; |
| 490 | for(i = 0; i < nb_nics; i++) { |
| 491 | nd = &nd_table[i]; |
| 492 | if (nd->fd >= 0) { |
| 493 | *parg++ = nd->ifname; |
| 494 | } |
| 495 | } |
| 496 | *parg++ = NULL; |
| 497 | execv(network_script, args); |
| 498 | exit(1); |
| 499 | } |
| 500 | while (waitpid(pid, &status, 0) != pid); |
| 501 | if (!WIFEXITED(status) || |
| 502 | WEXITSTATUS(status) != 0) { |
| 503 | fprintf(stderr, "%s: could not launch network script\n", |
| 504 | network_script); |
| 505 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | return 0; |
| 509 | } |
| 510 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 511 | void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 512 | { |
| 513 | #ifdef DEBUG_NE2000 |
| 514 | printf("NE2000: sending packet size=%d\n", size); |
| 515 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 516 | write(nd->fd, buf, size); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 517 | } |
| 518 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 519 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 520 | /* dumb display */ |
| 521 | |
| 522 | /* init terminal so that we can grab keys */ |
| 523 | static struct termios oldtty; |
| 524 | |
| 525 | static void term_exit(void) |
| 526 | { |
| 527 | tcsetattr (0, TCSANOW, &oldtty); |
| 528 | } |
| 529 | |
| 530 | static void term_init(void) |
| 531 | { |
| 532 | struct termios tty; |
| 533 | |
| 534 | tcgetattr (0, &tty); |
| 535 | oldtty = tty; |
| 536 | |
| 537 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 538 | |INLCR|IGNCR|ICRNL|IXON); |
| 539 | tty.c_oflag |= OPOST; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 540 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 541 | /* if graphical mode, we allow Ctrl-C handling */ |
| 542 | if (nographic) |
| 543 | tty.c_lflag &= ~ISIG; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 544 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 545 | tty.c_cflag |= CS8; |
| 546 | tty.c_cc[VMIN] = 1; |
| 547 | tty.c_cc[VTIME] = 0; |
| 548 | |
| 549 | tcsetattr (0, TCSANOW, &tty); |
| 550 | |
| 551 | atexit(term_exit); |
| 552 | |
| 553 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 554 | } |
| 555 | |
| 556 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 557 | { |
| 558 | } |
| 559 | |
| 560 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 561 | { |
| 562 | } |
| 563 | |
| 564 | static void dumb_refresh(DisplayState *ds) |
| 565 | { |
| 566 | vga_update_display(); |
| 567 | } |
| 568 | |
| 569 | void dumb_display_init(DisplayState *ds) |
| 570 | { |
| 571 | ds->data = NULL; |
| 572 | ds->linesize = 0; |
| 573 | ds->depth = 0; |
| 574 | ds->dpy_update = dumb_update; |
| 575 | ds->dpy_resize = dumb_resize; |
| 576 | ds->dpy_refresh = dumb_refresh; |
| 577 | } |
| 578 | |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 579 | #if !defined(CONFIG_SOFTMMU) |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 580 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 581 | /* cpu signal handler */ |
| 582 | static void host_segv_handler(int host_signum, siginfo_t *info, |
| 583 | void *puc) |
| 584 | { |
| 585 | if (cpu_signal_handler(host_signum, info, puc)) |
| 586 | return; |
| 587 | term_exit(); |
| 588 | abort(); |
| 589 | } |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 590 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 591 | |
| 592 | static int timer_irq_pending; |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 593 | static int timer_irq_count; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 594 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 595 | static int timer_ms; |
| 596 | static int gui_refresh_pending, gui_refresh_count; |
| 597 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 598 | static void host_alarm_handler(int host_signum, siginfo_t *info, |
| 599 | void *puc) |
| 600 | { |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 601 | /* NOTE: since usually the OS asks a 100 Hz clock, there can be |
| 602 | some drift between cpu_get_ticks() and the interrupt time. So |
| 603 | we queue some interrupts to avoid missing some */ |
| 604 | timer_irq_count += pit_get_out_edges(&pit_channels[0]); |
| 605 | if (timer_irq_count) { |
| 606 | if (timer_irq_count > 2) |
| 607 | timer_irq_count = 2; |
| 608 | timer_irq_count--; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 609 | timer_irq_pending = 1; |
| 610 | } |
| 611 | gui_refresh_count += timer_ms; |
| 612 | if (gui_refresh_count >= GUI_REFRESH_INTERVAL) { |
| 613 | gui_refresh_count = 0; |
| 614 | gui_refresh_pending = 1; |
| 615 | } |
| 616 | |
| 617 | if (gui_refresh_pending || timer_irq_pending) { |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 618 | /* just exit from the cpu to have a chance to handle timers */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 619 | cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 620 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 621 | } |
| 622 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 623 | #define MAX_IO_HANDLERS 64 |
| 624 | |
| 625 | typedef struct IOHandlerRecord { |
| 626 | int fd; |
| 627 | IOCanRWHandler *fd_can_read; |
| 628 | IOReadHandler *fd_read; |
| 629 | void *opaque; |
| 630 | /* temporary data */ |
| 631 | struct pollfd *ufd; |
| 632 | int max_size; |
| 633 | } IOHandlerRecord; |
| 634 | |
| 635 | static IOHandlerRecord io_handlers[MAX_IO_HANDLERS]; |
| 636 | static int nb_io_handlers = 0; |
| 637 | |
| 638 | int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, |
| 639 | IOReadHandler *fd_read, void *opaque) |
| 640 | { |
| 641 | IOHandlerRecord *ioh; |
| 642 | |
| 643 | if (nb_io_handlers >= MAX_IO_HANDLERS) |
| 644 | return -1; |
| 645 | ioh = &io_handlers[nb_io_handlers]; |
| 646 | ioh->fd = fd; |
| 647 | ioh->fd_can_read = fd_can_read; |
| 648 | ioh->fd_read = fd_read; |
| 649 | ioh->opaque = opaque; |
| 650 | nb_io_handlers++; |
| 651 | return 0; |
| 652 | } |
| 653 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 654 | /* main execution loop */ |
| 655 | |
| 656 | CPUState *cpu_gdbstub_get_env(void *opaque) |
| 657 | { |
| 658 | return global_env; |
| 659 | } |
| 660 | |
bellard | 4c3a88a | 2003-07-26 12:06:08 +0000 | [diff] [blame] | 661 | int main_loop(void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 662 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 663 | struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd; |
| 664 | int ret, n, timeout, serial_ok, max_size, i; |
| 665 | uint8_t buf[4096]; |
| 666 | IOHandlerRecord *ioh; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 667 | CPUState *env = global_env; |
| 668 | |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 669 | if (!term_inited) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 670 | /* initialize terminal only there so that the user has a |
| 671 | chance to stop QEMU with Ctrl-C before the gdb connection |
| 672 | is launched */ |
| 673 | term_inited = 1; |
| 674 | term_init(); |
| 675 | } |
| 676 | |
bellard | 27c3f2c | 2003-09-30 21:40:47 +0000 | [diff] [blame] | 677 | serial_ok = 1; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 678 | cpu_enable_ticks(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 679 | for(;;) { |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 680 | #if defined (DO_TB_FLUSH) |
| 681 | tb_flush(); |
| 682 | #endif |
| 683 | ret = cpu_exec(env); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 684 | if (reset_requested) { |
| 685 | ret = EXCP_INTERRUPT; |
bellard | cd4c3e8 | 2003-07-04 14:38:25 +0000 | [diff] [blame] | 686 | break; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 687 | } |
| 688 | if (ret == EXCP_DEBUG) { |
| 689 | ret = EXCP_DEBUG; |
| 690 | break; |
| 691 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 692 | /* if hlt instruction, we wait until the next IRQ */ |
| 693 | if (ret == EXCP_HLT) |
| 694 | timeout = 10; |
| 695 | else |
| 696 | timeout = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 697 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 698 | /* poll any events */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 699 | pf = ufds; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 700 | ioh = io_handlers; |
| 701 | for(i = 0; i < nb_io_handlers; i++) { |
| 702 | max_size = ioh->fd_can_read(ioh->opaque); |
| 703 | if (max_size > 0) { |
| 704 | if (max_size > sizeof(buf)) |
| 705 | max_size = sizeof(buf); |
| 706 | pf->fd = ioh->fd; |
| 707 | pf->events = POLLIN; |
| 708 | ioh->ufd = pf; |
| 709 | pf++; |
| 710 | } else { |
| 711 | ioh->ufd = NULL; |
| 712 | } |
| 713 | ioh->max_size = max_size; |
| 714 | ioh++; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 715 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 716 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 717 | gdb_ufd = NULL; |
| 718 | if (gdbstub_fd > 0) { |
| 719 | gdb_ufd = pf; |
| 720 | pf->fd = gdbstub_fd; |
| 721 | pf->events = POLLIN; |
| 722 | pf++; |
| 723 | } |
| 724 | |
| 725 | ret = poll(ufds, pf - ufds, timeout); |
| 726 | if (ret > 0) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 727 | ioh = io_handlers; |
| 728 | for(i = 0; i < nb_io_handlers; i++) { |
| 729 | pf = ioh->ufd; |
| 730 | if (pf) { |
| 731 | n = read(ioh->fd, buf, ioh->max_size); |
| 732 | if (n > 0) { |
| 733 | ioh->fd_read(ioh->opaque, buf, n); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 734 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 735 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 736 | ioh++; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 737 | } |
| 738 | if (gdb_ufd && (gdb_ufd->revents & POLLIN)) { |
| 739 | uint8_t buf[1]; |
| 740 | /* stop emulation if requested by gdb */ |
| 741 | n = read(gdbstub_fd, buf, 1); |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 742 | if (n == 1) { |
| 743 | ret = EXCP_INTERRUPT; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 744 | break; |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 745 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | /* timer IRQ */ |
| 750 | if (timer_irq_pending) { |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 751 | #if defined (TARGET_I386) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 752 | pic_set_irq(0, 1); |
| 753 | pic_set_irq(0, 0); |
| 754 | timer_irq_pending = 0; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 755 | rtc_timer(); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 756 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 757 | } |
bellard | 8dc75d7 | 2004-02-25 23:30:56 +0000 | [diff] [blame] | 758 | /* XXX: add explicit timer */ |
| 759 | SB16_run(); |
| 760 | |
| 761 | /* run dma transfers, if any */ |
| 762 | DMA_run(); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 763 | |
| 764 | /* VGA */ |
| 765 | if (gui_refresh_pending) { |
| 766 | display_state.dpy_refresh(&display_state); |
| 767 | gui_refresh_pending = 0; |
| 768 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 769 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 770 | cpu_disable_ticks(); |
| 771 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 772 | } |
| 773 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 774 | void help(void) |
| 775 | { |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 776 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 777 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 778 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 779 | "'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] | 780 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 781 | "Standard options:\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 782 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 783 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 784 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 785 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 786 | "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 787 | "-snapshot write to temporary files instead of disk image files\n" |
| 788 | "-m megs set virtual RAM size to megs MB\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 789 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 790 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 791 | "Network options:\n" |
| 792 | "-n script set network init script [default=%s]\n" |
| 793 | "-nics n simulate 'n' network interfaces [default=1]\n" |
| 794 | "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n" |
| 795 | "\n" |
| 796 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 797 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 798 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 799 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 800 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 801 | "Debug/Expert options:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 802 | "-s wait gdb connection to port %d\n" |
| 803 | "-p port change gdb connection port\n" |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 804 | "-d output log to %s\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 805 | "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n" |
| 806 | "-L path set the directory for the BIOS and VGA BIOS\n" |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 807 | #ifdef USE_CODE_COPY |
| 808 | "-no-code-copy disable code copy acceleration\n" |
| 809 | #endif |
| 810 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 811 | "\n" |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 812 | "During emulation, use C-a h to get terminal commands:\n", |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 813 | #ifdef CONFIG_SOFTMMU |
| 814 | "qemu", |
| 815 | #else |
| 816 | "qemu-fast", |
| 817 | #endif |
| 818 | DEFAULT_NETWORK_SCRIPT, |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 819 | DEFAULT_GDBSTUB_PORT, |
| 820 | "/tmp/qemu.log"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 821 | term_print_help(); |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 822 | #ifndef CONFIG_SOFTMMU |
| 823 | printf("\n" |
| 824 | "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n" |
| 825 | "work. Please use the 'qemu' executable to have a more accurate (but slower)\n" |
| 826 | "PC emulation.\n"); |
| 827 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 828 | exit(1); |
| 829 | } |
| 830 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 831 | struct option long_options[] = { |
| 832 | { "initrd", 1, NULL, 0, }, |
| 833 | { "hda", 1, NULL, 0, }, |
| 834 | { "hdb", 1, NULL, 0, }, |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 835 | { "snapshot", 0, NULL, 0, }, |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 836 | { "hdachs", 1, NULL, 0, }, |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 837 | { "nographic", 0, NULL, 0, }, |
| 838 | { "kernel", 1, NULL, 0, }, |
| 839 | { "append", 1, NULL, 0, }, |
bellard | 42f1e0e | 2003-09-30 22:11:17 +0000 | [diff] [blame] | 840 | { "tun-fd", 1, NULL, 0, }, |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 841 | { "hdc", 1, NULL, 0, }, |
| 842 | { "hdd", 1, NULL, 0, }, |
| 843 | { "cdrom", 1, NULL, 0, }, |
| 844 | { "boot", 1, NULL, 0, }, |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 845 | { "fda", 1, NULL, 0, }, |
| 846 | { "fdb", 1, NULL, 0, }, |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 847 | { "no-code-copy", 0, NULL, 0 }, |
| 848 | { "nics", 1, NULL, 0 }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 849 | { NULL, 0, NULL, 0 }, |
| 850 | }; |
| 851 | |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 852 | #ifdef CONFIG_SDL |
| 853 | /* SDL use the pthreads and they modify sigaction. We don't |
| 854 | want that. */ |
bellard | dc887a4 | 2004-01-04 18:18:57 +0000 | [diff] [blame] | 855 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 856 | extern void __libc_sigaction(); |
| 857 | #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact) |
| 858 | #else |
| 859 | extern void __sigaction(); |
| 860 | #define sigaction(sig, act, oact) __sigaction(sig, act, oact) |
| 861 | #endif |
| 862 | #endif /* CONFIG_SDL */ |
| 863 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 864 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 865 | |
| 866 | /* this stack is only used during signal handling */ |
| 867 | #define SIGNAL_STACK_SIZE 32768 |
| 868 | |
| 869 | static uint8_t *signal_stack; |
| 870 | |
| 871 | #endif |
| 872 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 873 | int main(int argc, char **argv) |
| 874 | { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 875 | int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 876 | int snapshot, linux_boot; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 877 | struct sigaction act; |
| 878 | struct itimerval itv; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 879 | CPUState *env; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 880 | const char *initrd_filename; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 881 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 882 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 883 | DisplayState *ds = &display_state; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 884 | int cyls, heads, secs; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 885 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 886 | /* we never want that malloc() uses mmap() */ |
| 887 | mallopt(M_MMAP_THRESHOLD, 4096 * 1024); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 888 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 889 | for(i = 0; i < MAX_FD; i++) |
| 890 | fd_filename[i] = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 891 | for(i = 0; i < MAX_DISKS; i++) |
| 892 | hd_filename[i] = NULL; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 893 | ram_size = 32 * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 894 | vga_ram_size = VGA_RAM_SIZE; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 895 | pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 896 | use_gdbstub = 0; |
| 897 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 898 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 899 | nographic = 0; |
| 900 | kernel_filename = NULL; |
| 901 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 902 | has_cdrom = 1; |
| 903 | cyls = heads = secs = 0; |
| 904 | |
| 905 | nb_nics = 1; |
| 906 | for(i = 0; i < MAX_NICS; i++) { |
| 907 | NetDriverState *nd = &nd_table[i]; |
| 908 | nd->fd = -1; |
| 909 | /* init virtual mac address */ |
| 910 | nd->macaddr[0] = 0x52; |
| 911 | nd->macaddr[1] = 0x54; |
| 912 | nd->macaddr[2] = 0x00; |
| 913 | nd->macaddr[3] = 0x12; |
| 914 | nd->macaddr[4] = 0x34; |
| 915 | nd->macaddr[5] = 0x56 + i; |
| 916 | } |
| 917 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 918 | for(;;) { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 919 | c = getopt_long_only(argc, argv, "hm:dn:sp:L:", long_options, &long_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 920 | if (c == -1) |
| 921 | break; |
| 922 | switch(c) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 923 | case 0: |
| 924 | switch(long_index) { |
| 925 | case 0: |
| 926 | initrd_filename = optarg; |
| 927 | break; |
| 928 | case 1: |
| 929 | hd_filename[0] = optarg; |
| 930 | break; |
| 931 | case 2: |
| 932 | hd_filename[1] = optarg; |
| 933 | break; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 934 | case 3: |
| 935 | snapshot = 1; |
| 936 | break; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 937 | case 4: |
| 938 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 939 | const char *p; |
| 940 | p = optarg; |
| 941 | cyls = strtol(p, (char **)&p, 0); |
| 942 | if (*p != ',') |
| 943 | goto chs_fail; |
| 944 | p++; |
| 945 | heads = strtol(p, (char **)&p, 0); |
| 946 | if (*p != ',') |
| 947 | goto chs_fail; |
| 948 | p++; |
| 949 | secs = strtol(p, (char **)&p, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 950 | if (*p != '\0') { |
| 951 | chs_fail: |
| 952 | cyls = 0; |
| 953 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 954 | } |
| 955 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 956 | case 5: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 957 | nographic = 1; |
| 958 | break; |
| 959 | case 6: |
| 960 | kernel_filename = optarg; |
| 961 | break; |
| 962 | case 7: |
| 963 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 964 | break; |
bellard | 42f1e0e | 2003-09-30 22:11:17 +0000 | [diff] [blame] | 965 | case 8: |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 966 | { |
| 967 | const char *p; |
| 968 | int fd; |
| 969 | p = optarg; |
| 970 | nb_nics = 0; |
| 971 | for(;;) { |
| 972 | fd = strtol(p, (char **)&p, 0); |
| 973 | nd_table[nb_nics].fd = fd; |
| 974 | snprintf(nd_table[nb_nics].ifname, |
| 975 | sizeof(nd_table[nb_nics].ifname), |
| 976 | "fd%d", nb_nics); |
| 977 | nb_nics++; |
| 978 | if (*p == ',') { |
| 979 | p++; |
| 980 | } else if (*p != '\0') { |
| 981 | fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics); |
| 982 | exit(1); |
| 983 | } |
| 984 | } |
| 985 | } |
bellard | 42f1e0e | 2003-09-30 22:11:17 +0000 | [diff] [blame] | 986 | break; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 987 | case 9: |
| 988 | hd_filename[2] = optarg; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 989 | has_cdrom = 0; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 990 | break; |
| 991 | case 10: |
| 992 | hd_filename[3] = optarg; |
| 993 | break; |
| 994 | case 11: |
| 995 | hd_filename[2] = optarg; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 996 | has_cdrom = 1; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 997 | break; |
| 998 | case 12: |
| 999 | boot_device = optarg[0]; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1000 | if (boot_device != 'a' && boot_device != 'b' && |
| 1001 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 1002 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 1003 | exit(1); |
| 1004 | } |
| 1005 | break; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1006 | case 13: |
| 1007 | fd_filename[0] = optarg; |
| 1008 | break; |
| 1009 | case 14: |
| 1010 | fd_filename[1] = optarg; |
| 1011 | break; |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1012 | case 15: |
| 1013 | code_copy_enabled = 0; |
| 1014 | break; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1015 | case 16: |
| 1016 | nb_nics = atoi(optarg); |
| 1017 | if (nb_nics < 1 || nb_nics > MAX_NICS) { |
| 1018 | fprintf(stderr, "qemu: invalid number of network interfaces\n"); |
| 1019 | exit(1); |
| 1020 | } |
| 1021 | break; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 1022 | } |
| 1023 | break; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1024 | case 'h': |
| 1025 | help(); |
| 1026 | break; |
| 1027 | case 'm': |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1028 | ram_size = atoi(optarg) * 1024 * 1024; |
| 1029 | if (ram_size <= 0) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1030 | help(); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1031 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 1032 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 1033 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 1034 | exit(1); |
| 1035 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | case 'd': |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 1038 | cpu_set_log(CPU_LOG_ALL); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1039 | break; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1040 | case 'n': |
| 1041 | pstrcpy(network_script, sizeof(network_script), optarg); |
| 1042 | break; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1043 | case 's': |
| 1044 | use_gdbstub = 1; |
| 1045 | break; |
| 1046 | case 'p': |
| 1047 | gdbstub_port = atoi(optarg); |
| 1048 | break; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1049 | case 'L': |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 1050 | bios_dir = optarg; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1051 | break; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1054 | |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 1055 | if (optind < argc) { |
| 1056 | hd_filename[0] = argv[optind++]; |
| 1057 | } |
| 1058 | |
| 1059 | linux_boot = (kernel_filename != NULL); |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1060 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1061 | if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' && |
| 1062 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1063 | help(); |
bellard | 8f2b1fb | 2003-11-16 19:46:01 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* boot to cd by default if no hard disk */ |
bellard | d030931 | 2004-01-18 22:35:25 +0000 | [diff] [blame] | 1066 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 1067 | if (fd_filename[0] != '\0') |
| 1068 | boot_device = 'a'; |
| 1069 | else |
| 1070 | boot_device = 'd'; |
| 1071 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1072 | |
bellard | dc887a4 | 2004-01-04 18:18:57 +0000 | [diff] [blame] | 1073 | #if !defined(CONFIG_SOFTMMU) |
| 1074 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
| 1075 | { |
| 1076 | static uint8_t stdout_buf[4096]; |
| 1077 | setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf)); |
| 1078 | } |
| 1079 | #else |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 1080 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | dc887a4 | 2004-01-04 18:18:57 +0000 | [diff] [blame] | 1081 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1082 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1083 | /* init host network redirectors */ |
| 1084 | net_init(); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 1085 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1086 | /* init the memory */ |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1087 | phys_ram_size = ram_size + vga_ram_size; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1088 | |
| 1089 | #ifdef CONFIG_SOFTMMU |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1090 | phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1091 | if (!phys_ram_base) { |
| 1092 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1093 | exit(1); |
| 1094 | } |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | /* as we must map the same page at several addresses, we must use |
| 1097 | a fd */ |
| 1098 | { |
| 1099 | const char *tmpdir; |
| 1100 | |
| 1101 | tmpdir = getenv("QEMU_TMPDIR"); |
| 1102 | if (!tmpdir) |
| 1103 | tmpdir = "/tmp"; |
| 1104 | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); |
| 1105 | if (mkstemp(phys_ram_file) < 0) { |
| 1106 | fprintf(stderr, "Could not create temporary memory file '%s'\n", |
| 1107 | phys_ram_file); |
| 1108 | exit(1); |
| 1109 | } |
| 1110 | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600); |
| 1111 | if (phys_ram_fd < 0) { |
| 1112 | fprintf(stderr, "Could not open temporary memory file '%s'\n", |
| 1113 | phys_ram_file); |
| 1114 | exit(1); |
| 1115 | } |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1116 | ftruncate(phys_ram_fd, phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1117 | unlink(phys_ram_file); |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 1118 | phys_ram_base = mmap(get_mmap_addr(phys_ram_size), |
| 1119 | phys_ram_size, |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1120 | PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
| 1121 | phys_ram_fd, 0); |
| 1122 | if (phys_ram_base == MAP_FAILED) { |
| 1123 | fprintf(stderr, "Could not map physical memory\n"); |
| 1124 | exit(1); |
| 1125 | } |
| 1126 | } |
| 1127 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1128 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1129 | /* we always create the cdrom drive, even if no disk is there */ |
| 1130 | if (has_cdrom) { |
| 1131 | bs_table[2] = bdrv_new("cdrom"); |
| 1132 | bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM); |
| 1133 | } |
| 1134 | |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 1135 | /* open the virtual block devices */ |
| 1136 | for(i = 0; i < MAX_DISKS; i++) { |
| 1137 | if (hd_filename[i]) { |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 1138 | if (!bs_table[i]) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1139 | char buf[64]; |
| 1140 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 1141 | bs_table[i] = bdrv_new(buf); |
| 1142 | } |
| 1143 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 1144 | fprintf(stderr, "qemu: could not open hard disk image '%s\n", |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 1145 | hd_filename[i]); |
| 1146 | exit(1); |
| 1147 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1148 | if (i == 0 && cyls != 0) |
| 1149 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* we always create at least one floppy disk */ |
| 1154 | fd_table[0] = bdrv_new("fda"); |
| 1155 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 1156 | |
| 1157 | for(i = 0; i < MAX_FD; i++) { |
| 1158 | if (fd_filename[i]) { |
| 1159 | if (!fd_table[i]) { |
| 1160 | char buf[64]; |
| 1161 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 1162 | fd_table[i] = bdrv_new(buf); |
| 1163 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 1164 | } |
| 1165 | if (fd_filename[i] != '\0') { |
| 1166 | if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { |
| 1167 | fprintf(stderr, "qemu: could not open floppy disk image '%s\n", |
| 1168 | fd_filename[i]); |
| 1169 | exit(1); |
| 1170 | } |
| 1171 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1175 | /* init CPU state */ |
| 1176 | env = cpu_init(); |
| 1177 | global_env = env; |
| 1178 | cpu_single_env = env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1179 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 1180 | init_ioports(); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 1181 | cpu_calibrate_ticks(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1182 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1183 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 1184 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1185 | dumb_display_init(ds); |
| 1186 | } else { |
| 1187 | #ifdef CONFIG_SDL |
| 1188 | sdl_display_init(ds); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1189 | #else |
| 1190 | dumb_display_init(ds); |
| 1191 | #endif |
| 1192 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1193 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 1194 | #if defined(TARGET_I386) |
| 1195 | pc_init(ram_size, vga_ram_size, boot_device, |
| 1196 | ds, fd_filename, snapshot, |
| 1197 | kernel_filename, kernel_cmdline, initrd_filename); |
| 1198 | #elif defined(TARGET_PPC) |
| 1199 | ppc_init(); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1200 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1201 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame^] | 1202 | /* launched after the device init so that it can display or not a |
| 1203 | banner */ |
| 1204 | monitor_init(); |
| 1205 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1206 | /* setup cpu signal handlers for MMU / self modifying code handling */ |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1207 | #if !defined(CONFIG_SOFTMMU) |
| 1208 | |
| 1209 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 1210 | { |
| 1211 | stack_t stk; |
| 1212 | signal_stack = malloc(SIGNAL_STACK_SIZE); |
| 1213 | stk.ss_sp = signal_stack; |
| 1214 | stk.ss_size = SIGNAL_STACK_SIZE; |
| 1215 | stk.ss_flags = 0; |
| 1216 | |
| 1217 | if (sigaltstack(&stk, NULL) < 0) { |
| 1218 | perror("sigaltstack"); |
| 1219 | exit(1); |
| 1220 | } |
| 1221 | } |
| 1222 | #endif |
| 1223 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1224 | sigfillset(&act.sa_mask); |
| 1225 | act.sa_flags = SA_SIGINFO; |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1226 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 1227 | act.sa_flags |= SA_ONSTACK; |
| 1228 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1229 | act.sa_sigaction = host_segv_handler; |
| 1230 | sigaction(SIGSEGV, &act, NULL); |
| 1231 | sigaction(SIGBUS, &act, NULL); |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1232 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 1233 | sigaction(SIGFPE, &act, NULL); |
| 1234 | #endif |
bellard | 3a51dee | 2003-10-27 21:18:35 +0000 | [diff] [blame] | 1235 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1236 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 1237 | /* timer signal */ |
| 1238 | sigfillset(&act.sa_mask); |
| 1239 | act.sa_flags = SA_SIGINFO; |
| 1240 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 1241 | act.sa_flags |= SA_ONSTACK; |
| 1242 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1243 | act.sa_sigaction = host_alarm_handler; |
| 1244 | sigaction(SIGALRM, &act, NULL); |
| 1245 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1246 | itv.it_interval.tv_sec = 0; |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 1247 | itv.it_interval.tv_usec = 1000; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1248 | itv.it_value.tv_sec = 0; |
| 1249 | itv.it_value.tv_usec = 10 * 1000; |
| 1250 | setitimer(ITIMER_REAL, &itv, NULL); |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 1251 | /* we probe the tick duration of the kernel to inform the user if |
| 1252 | the emulated kernel requested a too high timer frequency */ |
| 1253 | getitimer(ITIMER_REAL, &itv); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 1254 | timer_ms = itv.it_interval.tv_usec / 1000; |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 1255 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) / |
| 1256 | 1000000; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 1257 | |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 1258 | if (use_gdbstub) { |
| 1259 | cpu_gdbstub(NULL, main_loop, gdbstub_port); |
| 1260 | } else { |
| 1261 | main_loop(NULL); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1262 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |