]>
Commit | Line | Data |
---|---|---|
0824d6fc | 1 | /* |
80cabfad | 2 | * QEMU System Emulator |
5fafdf24 | 3 | * |
68d0f70e | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
5fafdf24 | 5 | * |
1df912cf FB |
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. | |
0824d6fc | 23 | */ |
0824d6fc | 24 | #include <unistd.h> |
0824d6fc FB |
25 | #include <fcntl.h> |
26 | #include <signal.h> | |
27 | #include <time.h> | |
0824d6fc | 28 | #include <errno.h> |
67b915a5 | 29 | #include <sys/time.h> |
c88676f8 | 30 | #include <zlib.h> |
67b915a5 | 31 | |
179a2c19 | 32 | /* Needed early for HOST_BSD etc. */ |
d40cdb10 BS |
33 | #include "config-host.h" |
34 | ||
67b915a5 | 35 | #ifndef _WIN32 |
0858532e | 36 | #include <pwd.h> |
67b915a5 | 37 | #include <sys/times.h> |
f1510b2c | 38 | #include <sys/wait.h> |
67b915a5 | 39 | #include <termios.h> |
67b915a5 | 40 | #include <sys/mman.h> |
f1510b2c | 41 | #include <sys/ioctl.h> |
24646c7e | 42 | #include <sys/resource.h> |
f1510b2c | 43 | #include <sys/socket.h> |
c94c8d64 | 44 | #include <netinet/in.h> |
24646c7e BS |
45 | #include <net/if.h> |
46 | #if defined(__NetBSD__) | |
47 | #include <net/if_tap.h> | |
48 | #endif | |
49 | #ifdef __linux__ | |
50 | #include <linux/if_tun.h> | |
51 | #endif | |
52 | #include <arpa/inet.h> | |
9d728e8c | 53 | #include <dirent.h> |
7c9d8e07 | 54 | #include <netdb.h> |
cb4b976b | 55 | #include <sys/select.h> |
179a2c19 | 56 | #ifdef HOST_BSD |
7d3505c5 | 57 | #include <sys/stat.h> |
c5e97233 | 58 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
7d3505c5 | 59 | #include <libutil.h> |
24646c7e BS |
60 | #else |
61 | #include <util.h> | |
128ab2ff | 62 | #endif |
5c40d2bd TS |
63 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
64 | #include <freebsd/stdlib.h> | |
7d3505c5 | 65 | #else |
223f0d72 | 66 | #ifdef __linux__ |
7d3505c5 FB |
67 | #include <pty.h> |
68 | #include <malloc.h> | |
fd872598 | 69 | #include <linux/rtc.h> |
bd494f4c TS |
70 | |
71 | /* For the benefit of older linux systems which don't supply it, | |
72 | we use a local copy of hpet.h. */ | |
73 | /* #include <linux/hpet.h> */ | |
74 | #include "hpet.h" | |
75 | ||
e57a8c0e | 76 | #include <linux/ppdev.h> |
5867c88a | 77 | #include <linux/parport.h> |
223f0d72 BS |
78 | #endif |
79 | #ifdef __sun__ | |
d5d10bc3 TS |
80 | #include <sys/stat.h> |
81 | #include <sys/ethernet.h> | |
82 | #include <sys/sockio.h> | |
d5d10bc3 TS |
83 | #include <netinet/arp.h> |
84 | #include <netinet/in.h> | |
85 | #include <netinet/in_systm.h> | |
86 | #include <netinet/ip.h> | |
87 | #include <netinet/ip_icmp.h> // must come after ip.h | |
88 | #include <netinet/udp.h> | |
89 | #include <netinet/tcp.h> | |
90 | #include <net/if.h> | |
91 | #include <syslog.h> | |
92 | #include <stropts.h> | |
67b915a5 | 93 | #endif |
7d3505c5 | 94 | #endif |
ec530c81 | 95 | #endif |
67b915a5 | 96 | |
9892fbfb BS |
97 | #if defined(__OpenBSD__) |
98 | #include <util.h> | |
99 | #endif | |
100 | ||
8a16d273 TS |
101 | #if defined(CONFIG_VDE) |
102 | #include <libvdeplug.h> | |
103 | #endif | |
104 | ||
67b915a5 | 105 | #ifdef _WIN32 |
49dc768d | 106 | #include <windows.h> |
7d3505c5 | 107 | #include <malloc.h> |
67b915a5 | 108 | #include <sys/timeb.h> |
4fddf62a | 109 | #include <mmsystem.h> |
67b915a5 FB |
110 | #define getopt_long_only getopt_long |
111 | #define memalign(align, size) malloc(size) | |
112 | #endif | |
113 | ||
73332e5c | 114 | #ifdef CONFIG_SDL |
96bcd4f8 | 115 | #ifdef __APPLE__ |
83fb7adf | 116 | #include <SDL/SDL.h> |
880fec5d | 117 | int qemu_main(int argc, char **argv, char **envp); |
118 | int main(int argc, char **argv) | |
119 | { | |
120 | qemu_main(argc, argv, NULL); | |
121 | } | |
122 | #undef main | |
123 | #define main qemu_main | |
96bcd4f8 | 124 | #endif |
73332e5c | 125 | #endif /* CONFIG_SDL */ |
0824d6fc | 126 | |
5b0753e0 FB |
127 | #ifdef CONFIG_COCOA |
128 | #undef main | |
129 | #define main qemu_main | |
130 | #endif /* CONFIG_COCOA */ | |
131 | ||
511d2b14 BS |
132 | #include "hw/hw.h" |
133 | #include "hw/boards.h" | |
134 | #include "hw/usb.h" | |
135 | #include "hw/pcmcia.h" | |
136 | #include "hw/pc.h" | |
137 | #include "hw/audiodev.h" | |
138 | #include "hw/isa.h" | |
139 | #include "hw/baum.h" | |
140 | #include "hw/bt.h" | |
b6f6e3d3 | 141 | #include "hw/smbios.h" |
e37630ca | 142 | #include "hw/xen.h" |
5ef4efa4 | 143 | #include "bt-host.h" |
511d2b14 BS |
144 | #include "net.h" |
145 | #include "monitor.h" | |
146 | #include "console.h" | |
147 | #include "sysemu.h" | |
148 | #include "gdbstub.h" | |
149 | #include "qemu-timer.h" | |
150 | #include "qemu-char.h" | |
151 | #include "cache-utils.h" | |
152 | #include "block.h" | |
a718acec | 153 | #include "dma.h" |
511d2b14 BS |
154 | #include "audio/audio.h" |
155 | #include "migration.h" | |
156 | #include "kvm.h" | |
157 | #include "balloon.h" | |
158 | ||
0824d6fc | 159 | #include "disas.h" |
fc01f7e7 | 160 | |
8a7ddc38 | 161 | #include "exec-all.h" |
0824d6fc | 162 | |
511d2b14 BS |
163 | #include "qemu_socket.h" |
164 | ||
165 | #if defined(CONFIG_SLIRP) | |
166 | #include "libslirp.h" | |
167 | #endif | |
168 | ||
0824d6fc | 169 | //#define DEBUG_UNUSED_IOPORT |
fd872598 | 170 | //#define DEBUG_IOPORT |
9dc63a1e BS |
171 | //#define DEBUG_NET |
172 | //#define DEBUG_SLIRP | |
330d0414 | 173 | |
d12d51d5 AL |
174 | |
175 | #ifdef DEBUG_IOPORT | |
93fcfe39 | 176 | # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) |
d12d51d5 AL |
177 | #else |
178 | # define LOG_IOPORT(...) do { } while (0) | |
179 | #endif | |
180 | ||
1bfe856e | 181 | #define DEFAULT_RAM_SIZE 128 |
313aa567 | 182 | |
0d92ed30 PB |
183 | /* Max number of USB devices that can be specified on the commandline. */ |
184 | #define MAX_USB_CMDLINE 8 | |
185 | ||
dc72ac14 AZ |
186 | /* Max number of bluetooth switches on the commandline. */ |
187 | #define MAX_BT_CMDLINE 10 | |
188 | ||
7dea1da4 FB |
189 | /* XXX: use a two level table to limit memory usage */ |
190 | #define MAX_IOPORTS 65536 | |
0824d6fc | 191 | |
80cabfad | 192 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
1192dad8 | 193 | const char *bios_name = NULL; |
dbed7e40 BS |
194 | static void *ioport_opaque[MAX_IOPORTS]; |
195 | static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; | |
196 | static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; | |
e4bcb14c | 197 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
faea38e7 | 198 | to store the VM snapshots */ |
e4bcb14c TS |
199 | DriveInfo drives_table[MAX_DRIVES+1]; |
200 | int nb_drives; | |
dbed7e40 | 201 | static int vga_ram_size; |
cb5a7aa8 | 202 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
3023f332 | 203 | static DisplayState *display_state; |
a20dd508 | 204 | int nographic; |
dbed7e40 | 205 | static int curses; |
7d957bd8 | 206 | static int sdl; |
3d11d0eb | 207 | const char* keyboard_layout = NULL; |
313aa567 | 208 | int64_t ticks_per_sec; |
00f82b8a | 209 | ram_addr_t ram_size; |
c4b1fcc0 | 210 | int nb_nics; |
7c9d8e07 | 211 | NICInfo nd_table[MAX_NICS]; |
8a7ddc38 | 212 | int vm_running; |
c0f4ce77 | 213 | static int autostart; |
f6503059 AZ |
214 | static int rtc_utc = 1; |
215 | static int rtc_date_offset = -1; /* -1 means no change */ | |
1bfe856e | 216 | int cirrus_vga_enabled = 1; |
c2b3b41a | 217 | int std_vga_enabled = 0; |
d34cab9f | 218 | int vmsvga_enabled = 0; |
94909d9f | 219 | int xenfb_enabled = 0; |
d827220b FB |
220 | #ifdef TARGET_SPARC |
221 | int graphic_width = 1024; | |
222 | int graphic_height = 768; | |
eee0b836 | 223 | int graphic_depth = 8; |
d827220b | 224 | #else |
1bfe856e FB |
225 | int graphic_width = 800; |
226 | int graphic_height = 600; | |
e9b137c2 | 227 | int graphic_depth = 15; |
eee0b836 | 228 | #endif |
dbed7e40 | 229 | static int full_screen = 0; |
634a21f6 | 230 | #ifdef CONFIG_SDL |
dbed7e40 | 231 | static int no_frame = 0; |
634a21f6 | 232 | #endif |
667accab | 233 | int no_quit = 0; |
8d11df9e | 234 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
6508fe59 | 235 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
9ede2fde | 236 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
a09db21f FB |
237 | #ifdef TARGET_I386 |
238 | int win2k_install_hack = 0; | |
73822ec8 | 239 | int rtc_td_hack = 0; |
a09db21f | 240 | #endif |
bb36d470 | 241 | int usb_enabled = 0; |
1b530a6d | 242 | int singlestep = 0; |
6a00d601 | 243 | int smp_cpus = 1; |
73fc9742 | 244 | const char *vnc_display; |
6515b203 | 245 | int acpi_enabled = 1; |
16b29ae1 | 246 | int no_hpet = 0; |
52ca8d6a | 247 | int fd_bootchk = 1; |
d1beab82 | 248 | int no_reboot = 0; |
b2f76161 | 249 | int no_shutdown = 0; |
9467cd46 | 250 | int cursor_hide = 1; |
a171fe39 | 251 | int graphic_rotate = 0; |
b9e82a59 | 252 | #ifndef _WIN32 |
71e3ceb8 | 253 | int daemonize = 0; |
b9e82a59 | 254 | #endif |
9ae02555 TS |
255 | const char *option_rom[MAX_OPTION_ROMS]; |
256 | int nb_option_roms; | |
8e71621f | 257 | int semihosting_enabled = 0; |
2b8f2d41 AZ |
258 | #ifdef TARGET_ARM |
259 | int old_param = 0; | |
260 | #endif | |
c35734b2 | 261 | const char *qemu_name; |
3780e197 | 262 | int alt_grab = 0; |
95efd11c | 263 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
264 | unsigned int nb_prom_envs = 0; |
265 | const char *prom_envs[MAX_PROM_ENVS]; | |
266 | #endif | |
ec691c80 AL |
267 | int nb_drives_opt; |
268 | struct drive_opt drives_opt[MAX_DRIVES]; | |
0824d6fc | 269 | |
268a362c AL |
270 | int nb_numa_nodes; |
271 | uint64_t node_mem[MAX_NODES]; | |
272 | uint64_t node_cpumask[MAX_NODES]; | |
273 | ||
ee5605e5 AZ |
274 | static CPUState *cur_cpu; |
275 | static CPUState *next_cpu; | |
76ea08f9 | 276 | static int event_pending = 1; |
bf20dc07 | 277 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
2e70f6ef | 278 | static int icount_time_shift; |
bf20dc07 | 279 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
2e70f6ef PB |
280 | #define MAX_ICOUNT_SHIFT 10 |
281 | /* Compensate for varying guest execution speed. */ | |
282 | static int64_t qemu_icount_bias; | |
dbed7e40 BS |
283 | static QEMUTimer *icount_rt_timer; |
284 | static QEMUTimer *icount_vm_timer; | |
9043b62d | 285 | static QEMUTimer *nographic_timer; |
ee5605e5 | 286 | |
8fcb1b90 BS |
287 | uint8_t qemu_uuid[16]; |
288 | ||
0824d6fc | 289 | /***********************************************************/ |
26aa7d72 FB |
290 | /* x86 ISA bus support */ |
291 | ||
292 | target_phys_addr_t isa_mem_base = 0; | |
3de388f6 | 293 | PicState2 *isa_pic; |
0824d6fc | 294 | |
477e3edf AL |
295 | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl; |
296 | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel; | |
297 | ||
298 | static uint32_t ioport_read(int index, uint32_t address) | |
299 | { | |
300 | static IOPortReadFunc *default_func[3] = { | |
301 | default_ioport_readb, | |
302 | default_ioport_readw, | |
303 | default_ioport_readl | |
304 | }; | |
305 | IOPortReadFunc *func = ioport_read_table[index][address]; | |
306 | if (!func) | |
307 | func = default_func[index]; | |
308 | return func(ioport_opaque[address], address); | |
309 | } | |
310 | ||
311 | static void ioport_write(int index, uint32_t address, uint32_t data) | |
312 | { | |
313 | static IOPortWriteFunc *default_func[3] = { | |
314 | default_ioport_writeb, | |
315 | default_ioport_writew, | |
316 | default_ioport_writel | |
317 | }; | |
318 | IOPortWriteFunc *func = ioport_write_table[index][address]; | |
319 | if (!func) | |
320 | func = default_func[index]; | |
321 | func(ioport_opaque[address], address, data); | |
322 | } | |
323 | ||
9596ebb7 | 324 | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
0824d6fc FB |
325 | { |
326 | #ifdef DEBUG_UNUSED_IOPORT | |
1196be37 | 327 | fprintf(stderr, "unused inb: port=0x%04x\n", address); |
0824d6fc | 328 | #endif |
fc01f7e7 | 329 | return 0xff; |
0824d6fc FB |
330 | } |
331 | ||
9596ebb7 | 332 | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
0824d6fc FB |
333 | { |
334 | #ifdef DEBUG_UNUSED_IOPORT | |
1196be37 | 335 | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data); |
0824d6fc FB |
336 | #endif |
337 | } | |
338 | ||
339 | /* default is to make two byte accesses */ | |
9596ebb7 | 340 | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
0824d6fc FB |
341 | { |
342 | uint32_t data; | |
477e3edf | 343 | data = ioport_read(0, address); |
db45c29a | 344 | address = (address + 1) & (MAX_IOPORTS - 1); |
477e3edf | 345 | data |= ioport_read(0, address) << 8; |
0824d6fc FB |
346 | return data; |
347 | } | |
348 | ||
9596ebb7 | 349 | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
0824d6fc | 350 | { |
477e3edf | 351 | ioport_write(0, address, data & 0xff); |
db45c29a | 352 | address = (address + 1) & (MAX_IOPORTS - 1); |
477e3edf | 353 | ioport_write(0, address, (data >> 8) & 0xff); |
0824d6fc FB |
354 | } |
355 | ||
9596ebb7 | 356 | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
0824d6fc | 357 | { |
fc01f7e7 | 358 | #ifdef DEBUG_UNUSED_IOPORT |
1196be37 | 359 | fprintf(stderr, "unused inl: port=0x%04x\n", address); |
fc01f7e7 FB |
360 | #endif |
361 | return 0xffffffff; | |
0824d6fc FB |
362 | } |
363 | ||
9596ebb7 | 364 | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
0824d6fc | 365 | { |
fc01f7e7 | 366 | #ifdef DEBUG_UNUSED_IOPORT |
1196be37 | 367 | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data); |
fc01f7e7 | 368 | #endif |
0824d6fc FB |
369 | } |
370 | ||
fc01f7e7 | 371 | /* size is the word size in byte */ |
5fafdf24 | 372 | int register_ioport_read(int start, int length, int size, |
c4b1fcc0 | 373 | IOPortReadFunc *func, void *opaque) |
f1510b2c | 374 | { |
fc01f7e7 | 375 | int i, bsize; |
f1510b2c | 376 | |
c4b1fcc0 | 377 | if (size == 1) { |
fc01f7e7 | 378 | bsize = 0; |
c4b1fcc0 | 379 | } else if (size == 2) { |
fc01f7e7 | 380 | bsize = 1; |
c4b1fcc0 | 381 | } else if (size == 4) { |
fc01f7e7 | 382 | bsize = 2; |
c4b1fcc0 | 383 | } else { |
88fdf56f | 384 | hw_error("register_ioport_read: invalid size"); |
fc01f7e7 | 385 | return -1; |
c4b1fcc0 FB |
386 | } |
387 | for(i = start; i < start + length; i += size) { | |
fc01f7e7 | 388 | ioport_read_table[bsize][i] = func; |
c4b1fcc0 | 389 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
88fdf56f | 390 | hw_error("register_ioport_read: invalid opaque"); |
c4b1fcc0 FB |
391 | ioport_opaque[i] = opaque; |
392 | } | |
f1510b2c FB |
393 | return 0; |
394 | } | |
395 | ||
fc01f7e7 | 396 | /* size is the word size in byte */ |
5fafdf24 | 397 | int register_ioport_write(int start, int length, int size, |
c4b1fcc0 | 398 | IOPortWriteFunc *func, void *opaque) |
f1510b2c | 399 | { |
fc01f7e7 | 400 | int i, bsize; |
f1510b2c | 401 | |
c4b1fcc0 | 402 | if (size == 1) { |
fc01f7e7 | 403 | bsize = 0; |
c4b1fcc0 | 404 | } else if (size == 2) { |
fc01f7e7 | 405 | bsize = 1; |
c4b1fcc0 | 406 | } else if (size == 4) { |
fc01f7e7 | 407 | bsize = 2; |
c4b1fcc0 | 408 | } else { |
88fdf56f | 409 | hw_error("register_ioport_write: invalid size"); |
fc01f7e7 | 410 | return -1; |
c4b1fcc0 FB |
411 | } |
412 | for(i = start; i < start + length; i += size) { | |
fc01f7e7 | 413 | ioport_write_table[bsize][i] = func; |
88fdf56f AZ |
414 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
415 | hw_error("register_ioport_write: invalid opaque"); | |
c4b1fcc0 FB |
416 | ioport_opaque[i] = opaque; |
417 | } | |
f1510b2c FB |
418 | return 0; |
419 | } | |
420 | ||
69b91039 FB |
421 | void isa_unassign_ioport(int start, int length) |
422 | { | |
423 | int i; | |
424 | ||
425 | for(i = start; i < start + length; i++) { | |
426 | ioport_read_table[0][i] = default_ioport_readb; | |
427 | ioport_read_table[1][i] = default_ioport_readw; | |
428 | ioport_read_table[2][i] = default_ioport_readl; | |
429 | ||
430 | ioport_write_table[0][i] = default_ioport_writeb; | |
431 | ioport_write_table[1][i] = default_ioport_writew; | |
432 | ioport_write_table[2][i] = default_ioport_writel; | |
a7607f7e AL |
433 | |
434 | ioport_opaque[i] = NULL; | |
69b91039 FB |
435 | } |
436 | } | |
437 | ||
20f32282 FB |
438 | /***********************************************************/ |
439 | ||
c45886db | 440 | void cpu_outb(CPUState *env, int addr, int val) |
0824d6fc | 441 | { |
d12d51d5 | 442 | LOG_IOPORT("outb: %04x %02x\n", addr, val); |
477e3edf | 443 | ioport_write(0, addr, val); |
640f42e4 | 444 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
445 | if (env) |
446 | env->last_io_time = cpu_get_time_fast(); | |
447 | #endif | |
0824d6fc FB |
448 | } |
449 | ||
c45886db | 450 | void cpu_outw(CPUState *env, int addr, int val) |
0824d6fc | 451 | { |
d12d51d5 | 452 | LOG_IOPORT("outw: %04x %04x\n", addr, val); |
477e3edf | 453 | ioport_write(1, addr, val); |
640f42e4 | 454 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
455 | if (env) |
456 | env->last_io_time = cpu_get_time_fast(); | |
457 | #endif | |
0824d6fc FB |
458 | } |
459 | ||
c45886db | 460 | void cpu_outl(CPUState *env, int addr, int val) |
0824d6fc | 461 | { |
d12d51d5 | 462 | LOG_IOPORT("outl: %04x %08x\n", addr, val); |
477e3edf | 463 | ioport_write(2, addr, val); |
640f42e4 | 464 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
465 | if (env) |
466 | env->last_io_time = cpu_get_time_fast(); | |
467 | #endif | |
0824d6fc FB |
468 | } |
469 | ||
c45886db | 470 | int cpu_inb(CPUState *env, int addr) |
0824d6fc | 471 | { |
fd872598 | 472 | int val; |
477e3edf | 473 | val = ioport_read(0, addr); |
d12d51d5 | 474 | LOG_IOPORT("inb : %04x %02x\n", addr, val); |
640f42e4 | 475 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
476 | if (env) |
477 | env->last_io_time = cpu_get_time_fast(); | |
fd872598 FB |
478 | #endif |
479 | return val; | |
0824d6fc FB |
480 | } |
481 | ||
c45886db | 482 | int cpu_inw(CPUState *env, int addr) |
0824d6fc | 483 | { |
fd872598 | 484 | int val; |
477e3edf | 485 | val = ioport_read(1, addr); |
d12d51d5 | 486 | LOG_IOPORT("inw : %04x %04x\n", addr, val); |
640f42e4 | 487 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
488 | if (env) |
489 | env->last_io_time = cpu_get_time_fast(); | |
fd872598 FB |
490 | #endif |
491 | return val; | |
0824d6fc FB |
492 | } |
493 | ||
c45886db | 494 | int cpu_inl(CPUState *env, int addr) |
0824d6fc | 495 | { |
fd872598 | 496 | int val; |
477e3edf | 497 | val = ioport_read(2, addr); |
d12d51d5 | 498 | LOG_IOPORT("inl : %04x %08x\n", addr, val); |
640f42e4 | 499 | #ifdef CONFIG_KQEMU |
89bfc105 FB |
500 | if (env) |
501 | env->last_io_time = cpu_get_time_fast(); | |
fd872598 FB |
502 | #endif |
503 | return val; | |
0824d6fc FB |
504 | } |
505 | ||
506 | /***********************************************************/ | |
0824d6fc FB |
507 | void hw_error(const char *fmt, ...) |
508 | { | |
509 | va_list ap; | |
6a00d601 | 510 | CPUState *env; |
0824d6fc FB |
511 | |
512 | va_start(ap, fmt); | |
513 | fprintf(stderr, "qemu: hardware error: "); | |
514 | vfprintf(stderr, fmt, ap); | |
515 | fprintf(stderr, "\n"); | |
6a00d601 FB |
516 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
517 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); | |
0824d6fc | 518 | #ifdef TARGET_I386 |
6a00d601 | 519 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
c45886db | 520 | #else |
6a00d601 | 521 | cpu_dump_state(env, stderr, fprintf, 0); |
0824d6fc | 522 | #endif |
6a00d601 | 523 | } |
0824d6fc FB |
524 | va_end(ap); |
525 | abort(); | |
526 | } | |
df751fa8 AL |
527 | |
528 | /***************/ | |
529 | /* ballooning */ | |
530 | ||
531 | static QEMUBalloonEvent *qemu_balloon_event; | |
532 | void *qemu_balloon_event_opaque; | |
533 | ||
534 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) | |
535 | { | |
536 | qemu_balloon_event = func; | |
537 | qemu_balloon_event_opaque = opaque; | |
538 | } | |
539 | ||
540 | void qemu_balloon(ram_addr_t target) | |
541 | { | |
542 | if (qemu_balloon_event) | |
543 | qemu_balloon_event(qemu_balloon_event_opaque, target); | |
544 | } | |
545 | ||
546 | ram_addr_t qemu_balloon_status(void) | |
547 | { | |
548 | if (qemu_balloon_event) | |
549 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); | |
550 | return 0; | |
551 | } | |
0824d6fc | 552 | |
63066f4f FB |
553 | /***********************************************************/ |
554 | /* keyboard/mouse */ | |
555 | ||
556 | static QEMUPutKBDEvent *qemu_put_kbd_event; | |
557 | static void *qemu_put_kbd_event_opaque; | |
455204eb TS |
558 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
559 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; | |
63066f4f FB |
560 | |
561 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) | |
562 | { | |
563 | qemu_put_kbd_event_opaque = opaque; | |
564 | qemu_put_kbd_event = func; | |
565 | } | |
566 | ||
455204eb TS |
567 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
568 | void *opaque, int absolute, | |
569 | const char *name) | |
63066f4f | 570 | { |
455204eb TS |
571 | QEMUPutMouseEntry *s, *cursor; |
572 | ||
573 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); | |
455204eb TS |
574 | |
575 | s->qemu_put_mouse_event = func; | |
576 | s->qemu_put_mouse_event_opaque = opaque; | |
577 | s->qemu_put_mouse_event_absolute = absolute; | |
578 | s->qemu_put_mouse_event_name = qemu_strdup(name); | |
579 | s->next = NULL; | |
580 | ||
581 | if (!qemu_put_mouse_event_head) { | |
582 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; | |
583 | return s; | |
584 | } | |
585 | ||
586 | cursor = qemu_put_mouse_event_head; | |
587 | while (cursor->next != NULL) | |
588 | cursor = cursor->next; | |
589 | ||
590 | cursor->next = s; | |
591 | qemu_put_mouse_event_current = s; | |
592 | ||
593 | return s; | |
594 | } | |
595 | ||
596 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) | |
597 | { | |
598 | QEMUPutMouseEntry *prev = NULL, *cursor; | |
599 | ||
600 | if (!qemu_put_mouse_event_head || entry == NULL) | |
601 | return; | |
602 | ||
603 | cursor = qemu_put_mouse_event_head; | |
604 | while (cursor != NULL && cursor != entry) { | |
605 | prev = cursor; | |
606 | cursor = cursor->next; | |
607 | } | |
608 | ||
609 | if (cursor == NULL) // does not exist or list empty | |
610 | return; | |
611 | else if (prev == NULL) { // entry is head | |
612 | qemu_put_mouse_event_head = cursor->next; | |
613 | if (qemu_put_mouse_event_current == entry) | |
614 | qemu_put_mouse_event_current = cursor->next; | |
615 | qemu_free(entry->qemu_put_mouse_event_name); | |
616 | qemu_free(entry); | |
617 | return; | |
618 | } | |
619 | ||
620 | prev->next = entry->next; | |
621 | ||
622 | if (qemu_put_mouse_event_current == entry) | |
623 | qemu_put_mouse_event_current = prev; | |
624 | ||
625 | qemu_free(entry->qemu_put_mouse_event_name); | |
626 | qemu_free(entry); | |
63066f4f FB |
627 | } |
628 | ||
629 | void kbd_put_keycode(int keycode) | |
630 | { | |
631 | if (qemu_put_kbd_event) { | |
632 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); | |
633 | } | |
634 | } | |
635 | ||
636 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) | |
637 | { | |
455204eb TS |
638 | QEMUPutMouseEvent *mouse_event; |
639 | void *mouse_event_opaque; | |
a171fe39 | 640 | int width; |
455204eb TS |
641 | |
642 | if (!qemu_put_mouse_event_current) { | |
643 | return; | |
644 | } | |
645 | ||
646 | mouse_event = | |
647 | qemu_put_mouse_event_current->qemu_put_mouse_event; | |
648 | mouse_event_opaque = | |
649 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; | |
650 | ||
651 | if (mouse_event) { | |
a171fe39 AZ |
652 | if (graphic_rotate) { |
653 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) | |
654 | width = 0x7fff; | |
655 | else | |
b94ed577 | 656 | width = graphic_width - 1; |
a171fe39 AZ |
657 | mouse_event(mouse_event_opaque, |
658 | width - dy, dx, dz, buttons_state); | |
659 | } else | |
660 | mouse_event(mouse_event_opaque, | |
661 | dx, dy, dz, buttons_state); | |
63066f4f FB |
662 | } |
663 | } | |
664 | ||
09b26c5e FB |
665 | int kbd_mouse_is_absolute(void) |
666 | { | |
455204eb TS |
667 | if (!qemu_put_mouse_event_current) |
668 | return 0; | |
669 | ||
670 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; | |
671 | } | |
672 | ||
376253ec | 673 | void do_info_mice(Monitor *mon) |
455204eb TS |
674 | { |
675 | QEMUPutMouseEntry *cursor; | |
676 | int index = 0; | |
677 | ||
678 | if (!qemu_put_mouse_event_head) { | |
376253ec | 679 | monitor_printf(mon, "No mouse devices connected\n"); |
455204eb TS |
680 | return; |
681 | } | |
682 | ||
376253ec | 683 | monitor_printf(mon, "Mouse devices available:\n"); |
455204eb TS |
684 | cursor = qemu_put_mouse_event_head; |
685 | while (cursor != NULL) { | |
376253ec AL |
686 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
687 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), | |
688 | index, cursor->qemu_put_mouse_event_name); | |
455204eb TS |
689 | index++; |
690 | cursor = cursor->next; | |
691 | } | |
692 | } | |
693 | ||
376253ec | 694 | void do_mouse_set(Monitor *mon, int index) |
455204eb TS |
695 | { |
696 | QEMUPutMouseEntry *cursor; | |
697 | int i = 0; | |
698 | ||
699 | if (!qemu_put_mouse_event_head) { | |
376253ec | 700 | monitor_printf(mon, "No mouse devices connected\n"); |
455204eb TS |
701 | return; |
702 | } | |
703 | ||
704 | cursor = qemu_put_mouse_event_head; | |
705 | while (cursor != NULL && index != i) { | |
706 | i++; | |
707 | cursor = cursor->next; | |
708 | } | |
709 | ||
710 | if (cursor != NULL) | |
711 | qemu_put_mouse_event_current = cursor; | |
712 | else | |
376253ec | 713 | monitor_printf(mon, "Mouse at given index not found\n"); |
09b26c5e FB |
714 | } |
715 | ||
1dce7c3c FB |
716 | /* compute with 96 bit intermediate result: (a*b)/c */ |
717 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) | |
0824d6fc | 718 | { |
1dce7c3c FB |
719 | union { |
720 | uint64_t ll; | |
721 | struct { | |
722 | #ifdef WORDS_BIGENDIAN | |
723 | uint32_t high, low; | |
724 | #else | |
725 | uint32_t low, high; | |
3b46e624 | 726 | #endif |
1dce7c3c FB |
727 | } l; |
728 | } u, res; | |
729 | uint64_t rl, rh; | |
0824d6fc | 730 | |
1dce7c3c FB |
731 | u.ll = a; |
732 | rl = (uint64_t)u.l.low * (uint64_t)b; | |
733 | rh = (uint64_t)u.l.high * (uint64_t)b; | |
734 | rh += (rl >> 32); | |
735 | res.l.high = rh / c; | |
736 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; | |
737 | return res.ll; | |
34865134 FB |
738 | } |
739 | ||
1dce7c3c FB |
740 | /***********************************************************/ |
741 | /* real time host monotonic timer */ | |
34865134 | 742 | |
1dce7c3c | 743 | #define QEMU_TIMER_BASE 1000000000LL |
34865134 | 744 | |
1dce7c3c | 745 | #ifdef WIN32 |
0824d6fc | 746 | |
1dce7c3c | 747 | static int64_t clock_freq; |
1115dde7 | 748 | |
1dce7c3c | 749 | static void init_get_clock(void) |
1115dde7 | 750 | { |
a8e5ac33 FB |
751 | LARGE_INTEGER freq; |
752 | int ret; | |
1dce7c3c FB |
753 | ret = QueryPerformanceFrequency(&freq); |
754 | if (ret == 0) { | |
755 | fprintf(stderr, "Could not calibrate ticks\n"); | |
756 | exit(1); | |
757 | } | |
758 | clock_freq = freq.QuadPart; | |
1115dde7 FB |
759 | } |
760 | ||
1dce7c3c | 761 | static int64_t get_clock(void) |
b8076a74 | 762 | { |
1dce7c3c FB |
763 | LARGE_INTEGER ti; |
764 | QueryPerformanceCounter(&ti); | |
765 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); | |
b8076a74 FB |
766 | } |
767 | ||
1dce7c3c | 768 | #else |
90cb9493 | 769 | |
1dce7c3c FB |
770 | static int use_rt_clock; |
771 | ||
772 | static void init_get_clock(void) | |
90cb9493 | 773 | { |
1dce7c3c | 774 | use_rt_clock = 0; |
c5e97233 BS |
775 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
776 | || defined(__DragonFly__) | |
1dce7c3c FB |
777 | { |
778 | struct timespec ts; | |
779 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { | |
780 | use_rt_clock = 1; | |
781 | } | |
782 | } | |
783 | #endif | |
90cb9493 FB |
784 | } |
785 | ||
1dce7c3c | 786 | static int64_t get_clock(void) |
fdbb4691 | 787 | { |
c5e97233 BS |
788 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
789 | || defined(__DragonFly__) | |
1dce7c3c FB |
790 | if (use_rt_clock) { |
791 | struct timespec ts; | |
792 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
793 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; | |
5fafdf24 | 794 | } else |
fdbb4691 | 795 | #endif |
1dce7c3c FB |
796 | { |
797 | /* XXX: using gettimeofday leads to problems if the date | |
798 | changes, so it should be avoided. */ | |
799 | struct timeval tv; | |
800 | gettimeofday(&tv, NULL); | |
801 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); | |
802 | } | |
fdbb4691 | 803 | } |
34865134 FB |
804 | #endif |
805 | ||
2e70f6ef PB |
806 | /* Return the virtual CPU time, based on the instruction counter. */ |
807 | static int64_t cpu_get_icount(void) | |
808 | { | |
809 | int64_t icount; | |
810 | CPUState *env = cpu_single_env;; | |
811 | icount = qemu_icount; | |
812 | if (env) { | |
813 | if (!can_do_io(env)) | |
814 | fprintf(stderr, "Bad clock read\n"); | |
815 | icount -= (env->icount_decr.u16.low + env->icount_extra); | |
816 | } | |
817 | return qemu_icount_bias + (icount << icount_time_shift); | |
818 | } | |
819 | ||
1dce7c3c FB |
820 | /***********************************************************/ |
821 | /* guest cycle counter */ | |
822 | ||
eade0f19 | 823 | static int64_t cpu_ticks_prev; |
34865134 | 824 | static int64_t cpu_ticks_offset; |
1dce7c3c | 825 | static int64_t cpu_clock_offset; |
8a7ddc38 | 826 | static int cpu_ticks_enabled; |
34865134 | 827 | |
1dce7c3c FB |
828 | /* return the host CPU cycle counter and handle stop/restart */ |
829 | int64_t cpu_get_ticks(void) | |
34865134 | 830 | { |
2e70f6ef PB |
831 | if (use_icount) { |
832 | return cpu_get_icount(); | |
833 | } | |
8a7ddc38 FB |
834 | if (!cpu_ticks_enabled) { |
835 | return cpu_ticks_offset; | |
836 | } else { | |
eade0f19 FB |
837 | int64_t ticks; |
838 | ticks = cpu_get_real_ticks(); | |
839 | if (cpu_ticks_prev > ticks) { | |
840 | /* Note: non increasing ticks may happen if the host uses | |
841 | software suspend */ | |
842 | cpu_ticks_offset += cpu_ticks_prev - ticks; | |
843 | } | |
844 | cpu_ticks_prev = ticks; | |
845 | return ticks + cpu_ticks_offset; | |
8a7ddc38 | 846 | } |
34865134 FB |
847 | } |
848 | ||
1dce7c3c FB |
849 | /* return the host CPU monotonic timer and handle stop/restart */ |
850 | static int64_t cpu_get_clock(void) | |
851 | { | |
852 | int64_t ti; | |
853 | if (!cpu_ticks_enabled) { | |
854 | return cpu_clock_offset; | |
855 | } else { | |
856 | ti = get_clock(); | |
857 | return ti + cpu_clock_offset; | |
858 | } | |
859 | } | |
860 | ||
34865134 FB |
861 | /* enable cpu_get_ticks() */ |
862 | void cpu_enable_ticks(void) | |
863 | { | |
8a7ddc38 FB |
864 | if (!cpu_ticks_enabled) { |
865 | cpu_ticks_offset -= cpu_get_real_ticks(); | |
1dce7c3c | 866 | cpu_clock_offset -= get_clock(); |
8a7ddc38 FB |
867 | cpu_ticks_enabled = 1; |
868 | } | |
34865134 FB |
869 | } |
870 | ||
871 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
872 | cpu_get_ticks() after that. */ | |
873 | void cpu_disable_ticks(void) | |
874 | { | |
8a7ddc38 FB |
875 | if (cpu_ticks_enabled) { |
876 | cpu_ticks_offset = cpu_get_ticks(); | |
1dce7c3c | 877 | cpu_clock_offset = cpu_get_clock(); |
8a7ddc38 FB |
878 | cpu_ticks_enabled = 0; |
879 | } | |
34865134 FB |
880 | } |
881 | ||
1dce7c3c FB |
882 | /***********************************************************/ |
883 | /* timers */ | |
5fafdf24 | 884 | |
8a7ddc38 FB |
885 | #define QEMU_TIMER_REALTIME 0 |
886 | #define QEMU_TIMER_VIRTUAL 1 | |
887 | ||
888 | struct QEMUClock { | |
889 | int type; | |
890 | /* XXX: add frequency */ | |
891 | }; | |
892 | ||
893 | struct QEMUTimer { | |
894 | QEMUClock *clock; | |
895 | int64_t expire_time; | |
896 | QEMUTimerCB *cb; | |
897 | void *opaque; | |
898 | struct QEMUTimer *next; | |
899 | }; | |
900 | ||
c8994013 TS |
901 | struct qemu_alarm_timer { |
902 | char const *name; | |
efe75411 | 903 | unsigned int flags; |
c8994013 TS |
904 | |
905 | int (*start)(struct qemu_alarm_timer *t); | |
906 | void (*stop)(struct qemu_alarm_timer *t); | |
efe75411 | 907 | void (*rearm)(struct qemu_alarm_timer *t); |
c8994013 TS |
908 | void *priv; |
909 | }; | |
910 | ||
efe75411 | 911 | #define ALARM_FLAG_DYNTICKS 0x1 |
d5d08334 | 912 | #define ALARM_FLAG_EXPIRED 0x2 |
efe75411 TS |
913 | |
914 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) | |
915 | { | |
916 | return t->flags & ALARM_FLAG_DYNTICKS; | |
917 | } | |
918 | ||
919 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) | |
920 | { | |
921 | if (!alarm_has_dynticks(t)) | |
922 | return; | |
923 | ||
924 | t->rearm(t); | |
925 | } | |
926 | ||
927 | /* TODO: MIN_TIMER_REARM_US should be optimized */ | |
928 | #define MIN_TIMER_REARM_US 250 | |
929 | ||
c8994013 | 930 | static struct qemu_alarm_timer *alarm_timer; |
f49e58dc | 931 | #ifndef _WIN32 |
c96f1a48 | 932 | static int alarm_timer_rfd, alarm_timer_wfd; |
f49e58dc | 933 | #endif |
8a7ddc38 | 934 | |
40c3bac3 | 935 | #ifdef _WIN32 |
c8994013 TS |
936 | |
937 | struct qemu_alarm_win32 { | |
938 | MMRESULT timerId; | |
939 | HANDLE host_alarm; | |
940 | unsigned int period; | |
941 | } alarm_win32_data = {0, NULL, -1}; | |
942 | ||
943 | static int win32_start_timer(struct qemu_alarm_timer *t); | |
944 | static void win32_stop_timer(struct qemu_alarm_timer *t); | |
efe75411 | 945 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
c8994013 | 946 | |
40c3bac3 | 947 | #else |
c8994013 TS |
948 | |
949 | static int unix_start_timer(struct qemu_alarm_timer *t); | |
950 | static void unix_stop_timer(struct qemu_alarm_timer *t); | |
951 | ||
231c6586 TS |
952 | #ifdef __linux__ |
953 | ||
efe75411 TS |
954 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
955 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); | |
956 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); | |
957 | ||
c40ec5a9 TS |
958 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
959 | static void hpet_stop_timer(struct qemu_alarm_timer *t); | |
960 | ||
c8994013 TS |
961 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
962 | static void rtc_stop_timer(struct qemu_alarm_timer *t); | |
963 | ||
efe75411 | 964 | #endif /* __linux__ */ |
8a7ddc38 | 965 | |
c8994013 TS |
966 | #endif /* _WIN32 */ |
967 | ||
2e70f6ef | 968 | /* Correlation between real and virtual time is always going to be |
bf20dc07 | 969 | fairly approximate, so ignore small variation. |
2e70f6ef PB |
970 | When the guest is idle real and virtual time will be aligned in |
971 | the IO wait loop. */ | |
972 | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) | |
973 | ||
974 | static void icount_adjust(void) | |
975 | { | |
976 | int64_t cur_time; | |
977 | int64_t cur_icount; | |
978 | int64_t delta; | |
979 | static int64_t last_delta; | |
980 | /* If the VM is not running, then do nothing. */ | |
981 | if (!vm_running) | |
982 | return; | |
983 | ||
984 | cur_time = cpu_get_clock(); | |
985 | cur_icount = qemu_get_clock(vm_clock); | |
986 | delta = cur_icount - cur_time; | |
987 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ | |
988 | if (delta > 0 | |
989 | && last_delta + ICOUNT_WOBBLE < delta * 2 | |
990 | && icount_time_shift > 0) { | |
991 | /* The guest is getting too far ahead. Slow time down. */ | |
992 | icount_time_shift--; | |
993 | } | |
994 | if (delta < 0 | |
995 | && last_delta - ICOUNT_WOBBLE > delta * 2 | |
996 | && icount_time_shift < MAX_ICOUNT_SHIFT) { | |
997 | /* The guest is getting too far behind. Speed time up. */ | |
998 | icount_time_shift++; | |
999 | } | |
1000 | last_delta = delta; | |
1001 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); | |
1002 | } | |
1003 | ||
1004 | static void icount_adjust_rt(void * opaque) | |
1005 | { | |
1006 | qemu_mod_timer(icount_rt_timer, | |
1007 | qemu_get_clock(rt_clock) + 1000); | |
1008 | icount_adjust(); | |
1009 | } | |
1010 | ||
1011 | static void icount_adjust_vm(void * opaque) | |
1012 | { | |
1013 | qemu_mod_timer(icount_vm_timer, | |
1014 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); | |
1015 | icount_adjust(); | |
1016 | } | |
1017 | ||
1018 | static void init_icount_adjust(void) | |
1019 | { | |
1020 | /* Have both realtime and virtual time triggers for speed adjustment. | |
1021 | The realtime trigger catches emulated time passing too slowly, | |
1022 | the virtual time trigger catches emulated time passing too fast. | |
1023 | Realtime triggers occur even when idle, so use them less frequently | |
1024 | than VM triggers. */ | |
1025 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); | |
1026 | qemu_mod_timer(icount_rt_timer, | |
1027 | qemu_get_clock(rt_clock) + 1000); | |
1028 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); | |
1029 | qemu_mod_timer(icount_vm_timer, | |
1030 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); | |
1031 | } | |
1032 | ||
c8994013 | 1033 | static struct qemu_alarm_timer alarm_timers[] = { |
efe75411 | 1034 | #ifndef _WIN32 |
231c6586 | 1035 | #ifdef __linux__ |
efe75411 TS |
1036 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
1037 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, | |
c40ec5a9 | 1038 | /* HPET - if available - is preferred */ |
efe75411 | 1039 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
c40ec5a9 | 1040 | /* ...otherwise try RTC */ |
efe75411 | 1041 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
c8994013 | 1042 | #endif |
efe75411 | 1043 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
c8994013 | 1044 | #else |
efe75411 TS |
1045 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
1046 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, | |
1047 | {"win32", 0, win32_start_timer, | |
1048 | win32_stop_timer, NULL, &alarm_win32_data}, | |
c8994013 TS |
1049 | #endif |
1050 | {NULL, } | |
1051 | }; | |
1052 | ||
3f47aa8c | 1053 | static void show_available_alarms(void) |
f3dcfada TS |
1054 | { |
1055 | int i; | |
1056 | ||
1057 | printf("Available alarm timers, in order of precedence:\n"); | |
1058 | for (i = 0; alarm_timers[i].name; i++) | |
1059 | printf("%s\n", alarm_timers[i].name); | |
1060 | } | |
1061 | ||
1062 | static void configure_alarms(char const *opt) | |
1063 | { | |
1064 | int i; | |
1065 | int cur = 0; | |
b1503cda | 1066 | int count = ARRAY_SIZE(alarm_timers) - 1; |
f3dcfada TS |
1067 | char *arg; |
1068 | char *name; | |
2e70f6ef | 1069 | struct qemu_alarm_timer tmp; |
f3dcfada | 1070 | |
3adda04c | 1071 | if (!strcmp(opt, "?")) { |
f3dcfada TS |
1072 | show_available_alarms(); |
1073 | exit(0); | |
1074 | } | |
1075 | ||
1076 | arg = strdup(opt); | |
1077 | ||
1078 | /* Reorder the array */ | |
1079 | name = strtok(arg, ","); | |
1080 | while (name) { | |
e2b577e5 | 1081 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
f3dcfada TS |
1082 | if (!strcmp(alarm_timers[i].name, name)) |
1083 | break; | |
1084 | } | |
1085 | ||
1086 | if (i == count) { | |
1087 | fprintf(stderr, "Unknown clock %s\n", name); | |
1088 | goto next; | |
1089 | } | |
1090 | ||
1091 | if (i < cur) | |
1092 | /* Ignore */ | |
1093 | goto next; | |
1094 | ||
1095 | /* Swap */ | |
1096 | tmp = alarm_timers[i]; | |
1097 | alarm_timers[i] = alarm_timers[cur]; | |
1098 | alarm_timers[cur] = tmp; | |
1099 | ||
1100 | cur++; | |
1101 | next: | |
1102 | name = strtok(NULL, ","); | |
1103 | } | |
1104 | ||
1105 | free(arg); | |
1106 | ||
1107 | if (cur) { | |
2e70f6ef | 1108 | /* Disable remaining timers */ |
f3dcfada TS |
1109 | for (i = cur; i < count; i++) |
1110 | alarm_timers[i].name = NULL; | |
3adda04c AJ |
1111 | } else { |
1112 | show_available_alarms(); | |
1113 | exit(1); | |
f3dcfada | 1114 | } |
f3dcfada TS |
1115 | } |
1116 | ||
c8994013 TS |
1117 | QEMUClock *rt_clock; |
1118 | QEMUClock *vm_clock; | |
1119 | ||
1120 | static QEMUTimer *active_timers[2]; | |
1121 | ||
9596ebb7 | 1122 | static QEMUClock *qemu_new_clock(int type) |
8a7ddc38 FB |
1123 | { |
1124 | QEMUClock *clock; | |
1125 | clock = qemu_mallocz(sizeof(QEMUClock)); | |
8a7ddc38 FB |
1126 | clock->type = type; |
1127 | return clock; | |
1128 | } | |
1129 | ||
1130 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) | |
1131 | { | |
1132 | QEMUTimer *ts; | |
1133 | ||
1134 | ts = qemu_mallocz(sizeof(QEMUTimer)); | |
1135 | ts->clock = clock; | |
1136 | ts->cb = cb; | |
1137 | ts->opaque = opaque; | |
1138 | return ts; | |
1139 | } | |
1140 | ||
1141 | void qemu_free_timer(QEMUTimer *ts) | |
1142 | { | |
1143 | qemu_free(ts); | |
1144 | } | |
1145 | ||
1146 | /* stop a timer, but do not dealloc it */ | |
1147 | void qemu_del_timer(QEMUTimer *ts) | |
1148 | { | |
1149 | QEMUTimer **pt, *t; | |
1150 | ||
1151 | /* NOTE: this code must be signal safe because | |
1152 | qemu_timer_expired() can be called from a signal. */ | |
1153 | pt = &active_timers[ts->clock->type]; | |
1154 | for(;;) { | |
1155 | t = *pt; | |
1156 | if (!t) | |
1157 | break; | |
1158 | if (t == ts) { | |
1159 | *pt = t->next; | |
1160 | break; | |
1161 | } | |
1162 | pt = &t->next; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | /* modify the current timer so that it will be fired when current_time | |
1167 | >= expire_time. The corresponding callback will be called. */ | |
1168 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) | |
1169 | { | |
1170 | QEMUTimer **pt, *t; | |
1171 | ||
1172 | qemu_del_timer(ts); | |
1173 | ||
1174 | /* add the timer in the sorted list */ | |
1175 | /* NOTE: this code must be signal safe because | |
1176 | qemu_timer_expired() can be called from a signal. */ | |
1177 | pt = &active_timers[ts->clock->type]; | |
1178 | for(;;) { | |
1179 | t = *pt; | |
1180 | if (!t) | |
1181 | break; | |
5fafdf24 | 1182 | if (t->expire_time > expire_time) |
8a7ddc38 FB |
1183 | break; |
1184 | pt = &t->next; | |
1185 | } | |
1186 | ts->expire_time = expire_time; | |
1187 | ts->next = *pt; | |
1188 | *pt = ts; | |
d5d08334 AZ |
1189 | |
1190 | /* Rearm if necessary */ | |
2e70f6ef PB |
1191 | if (pt == &active_timers[ts->clock->type]) { |
1192 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { | |
1193 | qemu_rearm_alarm_timer(alarm_timer); | |
1194 | } | |
1195 | /* Interrupt execution to force deadline recalculation. */ | |
1196 | if (use_icount && cpu_single_env) { | |
3098dba0 | 1197 | cpu_exit(cpu_single_env); |
2e70f6ef PB |
1198 | } |
1199 | } | |
8a7ddc38 FB |
1200 | } |
1201 | ||
1202 | int qemu_timer_pending(QEMUTimer *ts) | |
1203 | { | |
1204 | QEMUTimer *t; | |
1205 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { | |
1206 | if (t == ts) | |
1207 | return 1; | |
1208 | } | |
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) | |
1213 | { | |
1214 | if (!timer_head) | |
1215 | return 0; | |
1216 | return (timer_head->expire_time <= current_time); | |
1217 | } | |
1218 | ||
1219 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) | |
1220 | { | |
1221 | QEMUTimer *ts; | |
3b46e624 | 1222 | |
8a7ddc38 FB |
1223 | for(;;) { |
1224 | ts = *ptimer_head; | |
e95c8d51 | 1225 | if (!ts || ts->expire_time > current_time) |
8a7ddc38 FB |
1226 | break; |
1227 | /* remove timer from the list before calling the callback */ | |
1228 | *ptimer_head = ts->next; | |
1229 | ts->next = NULL; | |
3b46e624 | 1230 | |
8a7ddc38 FB |
1231 | /* run the callback (the timer list can be modified) */ |
1232 | ts->cb(ts->opaque); | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | int64_t qemu_get_clock(QEMUClock *clock) | |
1237 | { | |
1238 | switch(clock->type) { | |
1239 | case QEMU_TIMER_REALTIME: | |
1dce7c3c | 1240 | return get_clock() / 1000000; |
8a7ddc38 FB |
1241 | default: |
1242 | case QEMU_TIMER_VIRTUAL: | |
2e70f6ef PB |
1243 | if (use_icount) { |
1244 | return cpu_get_icount(); | |
1245 | } else { | |
1246 | return cpu_get_clock(); | |
1247 | } | |
8a7ddc38 FB |
1248 | } |
1249 | } | |
1250 | ||
1dce7c3c FB |
1251 | static void init_timers(void) |
1252 | { | |
1253 | init_get_clock(); | |
1254 | ticks_per_sec = QEMU_TIMER_BASE; | |
1255 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); | |
1256 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); | |
1257 | } | |
1258 | ||
8a7ddc38 FB |
1259 | /* save a timer */ |
1260 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) | |
1261 | { | |
1262 | uint64_t expire_time; | |
1263 | ||
1264 | if (qemu_timer_pending(ts)) { | |
1265 | expire_time = ts->expire_time; | |
1266 | } else { | |
1267 | expire_time = -1; | |
1268 | } | |
1269 | qemu_put_be64(f, expire_time); | |
1270 | } | |
1271 | ||
1272 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) | |
1273 | { | |
1274 | uint64_t expire_time; | |
1275 | ||
1276 | expire_time = qemu_get_be64(f); | |
1277 | if (expire_time != -1) { | |
1278 | qemu_mod_timer(ts, expire_time); | |
1279 | } else { | |
1280 | qemu_del_timer(ts); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | static void timer_save(QEMUFile *f, void *opaque) | |
1285 | { | |
1286 | if (cpu_ticks_enabled) { | |
1287 | hw_error("cannot save state if virtual timers are running"); | |
1288 | } | |
bee8d684 TS |
1289 | qemu_put_be64(f, cpu_ticks_offset); |
1290 | qemu_put_be64(f, ticks_per_sec); | |
1291 | qemu_put_be64(f, cpu_clock_offset); | |
8a7ddc38 FB |
1292 | } |
1293 | ||
1294 | static int timer_load(QEMUFile *f, void *opaque, int version_id) | |
1295 | { | |
c88676f8 | 1296 | if (version_id != 1 && version_id != 2) |
8a7ddc38 FB |
1297 | return -EINVAL; |
1298 | if (cpu_ticks_enabled) { | |
1299 | return -EINVAL; | |
1300 | } | |
bee8d684 TS |
1301 | cpu_ticks_offset=qemu_get_be64(f); |
1302 | ticks_per_sec=qemu_get_be64(f); | |
c88676f8 | 1303 | if (version_id == 2) { |
bee8d684 | 1304 | cpu_clock_offset=qemu_get_be64(f); |
c88676f8 | 1305 | } |
8a7ddc38 FB |
1306 | return 0; |
1307 | } | |
1308 | ||
67b915a5 | 1309 | #ifdef _WIN32 |
b9e82a59 BS |
1310 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
1311 | DWORD_PTR dwUser, DWORD_PTR dw1, | |
1312 | DWORD_PTR dw2) | |
67b915a5 | 1313 | #else |
8a7ddc38 | 1314 | static void host_alarm_handler(int host_signum) |
67b915a5 | 1315 | #endif |
8a7ddc38 | 1316 | { |
02ba45c5 FB |
1317 | #if 0 |
1318 | #define DISP_FREQ 1000 | |
1319 | { | |
1320 | static int64_t delta_min = INT64_MAX; | |
1321 | static int64_t delta_max, delta_cum, last_clock, delta, ti; | |
1322 | static int count; | |
1323 | ti = qemu_get_clock(vm_clock); | |
1324 | if (last_clock != 0) { | |
1325 | delta = ti - last_clock; | |
1326 | if (delta < delta_min) | |
1327 | delta_min = delta; | |
1328 | if (delta > delta_max) | |
1329 | delta_max = delta; | |
1330 | delta_cum += delta; | |
1331 | if (++count == DISP_FREQ) { | |
26a76461 | 1332 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
02ba45c5 FB |
1333 | muldiv64(delta_min, 1000000, ticks_per_sec), |
1334 | muldiv64(delta_max, 1000000, ticks_per_sec), | |
1335 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), | |
1336 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); | |
1337 | count = 0; | |
1338 | delta_min = INT64_MAX; | |
1339 | delta_max = 0; | |
1340 | delta_cum = 0; | |
1341 | } | |
1342 | } | |
1343 | last_clock = ti; | |
1344 | } | |
1345 | #endif | |
efe75411 | 1346 | if (alarm_has_dynticks(alarm_timer) || |
2e70f6ef PB |
1347 | (!use_icount && |
1348 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], | |
1349 | qemu_get_clock(vm_clock))) || | |
8a7ddc38 FB |
1350 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1351 | qemu_get_clock(rt_clock))) { | |
c96f1a48 | 1352 | CPUState *env = next_cpu; |
c96f1a48 | 1353 | |
06d9f2f7 | 1354 | #ifdef _WIN32 |
c8994013 TS |
1355 | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1356 | SetEvent(data->host_alarm); | |
f49e58dc AL |
1357 | #else |
1358 | static const char byte = 0; | |
c96f1a48 | 1359 | write(alarm_timer_wfd, &byte, sizeof(byte)); |
f49e58dc | 1360 | #endif |
d5d08334 AZ |
1361 | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1362 | ||
4f8eb8da AZ |
1363 | if (env) { |
1364 | /* stop the currently executing cpu because a timer occured */ | |
3098dba0 | 1365 | cpu_exit(env); |
640f42e4 | 1366 | #ifdef CONFIG_KQEMU |
4f8eb8da AZ |
1367 | if (env->kqemu_enabled) { |
1368 | kqemu_cpu_interrupt(env); | |
1369 | } | |
ee5605e5 | 1370 | #endif |
4f8eb8da | 1371 | } |
ee5605e5 | 1372 | event_pending = 1; |
8a7ddc38 FB |
1373 | } |
1374 | } | |
1375 | ||
2e70f6ef | 1376 | static int64_t qemu_next_deadline(void) |
efe75411 | 1377 | { |
2e70f6ef | 1378 | int64_t delta; |
efe75411 TS |
1379 | |
1380 | if (active_timers[QEMU_TIMER_VIRTUAL]) { | |
2e70f6ef PB |
1381 | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1382 | qemu_get_clock(vm_clock); | |
1383 | } else { | |
1384 | /* To avoid problems with overflow limit this to 2^32. */ | |
1385 | delta = INT32_MAX; | |
efe75411 TS |
1386 | } |
1387 | ||
2e70f6ef PB |
1388 | if (delta < 0) |
1389 | delta = 0; | |
efe75411 | 1390 | |
2e70f6ef PB |
1391 | return delta; |
1392 | } | |
1393 | ||
8632fb9a | 1394 | #if defined(__linux__) || defined(_WIN32) |
2e70f6ef PB |
1395 | static uint64_t qemu_next_deadline_dyntick(void) |
1396 | { | |
1397 | int64_t delta; | |
1398 | int64_t rtdelta; | |
1399 | ||
1400 | if (use_icount) | |
1401 | delta = INT32_MAX; | |
1402 | else | |
1403 | delta = (qemu_next_deadline() + 999) / 1000; | |
1404 | ||
1405 | if (active_timers[QEMU_TIMER_REALTIME]) { | |
1406 | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - | |
1407 | qemu_get_clock(rt_clock))*1000; | |
1408 | if (rtdelta < delta) | |
1409 | delta = rtdelta; | |
1410 | } | |
1411 | ||
1412 | if (delta < MIN_TIMER_REARM_US) | |
1413 | delta = MIN_TIMER_REARM_US; | |
1414 | ||
1415 | return delta; | |
efe75411 | 1416 | } |
8632fb9a | 1417 | #endif |
efe75411 | 1418 | |
fd872598 FB |
1419 | #ifndef _WIN32 |
1420 | ||
7183b4b4 AL |
1421 | /* Sets a specific flag */ |
1422 | static int fcntl_setfl(int fd, int flag) | |
1423 | { | |
1424 | int flags; | |
1425 | ||
1426 | flags = fcntl(fd, F_GETFL); | |
1427 | if (flags == -1) | |
1428 | return -errno; | |
1429 | ||
1430 | if (fcntl(fd, F_SETFL, flags | flag) == -1) | |
1431 | return -errno; | |
1432 | ||
1433 | return 0; | |
1434 | } | |
1435 | ||
829309c7 FB |
1436 | #if defined(__linux__) |
1437 | ||
fd872598 FB |
1438 | #define RTC_FREQ 1024 |
1439 | ||
de9a95f0 | 1440 | static void enable_sigio_timer(int fd) |
c8994013 TS |
1441 | { |
1442 | struct sigaction act; | |
1443 | ||
1444 | /* timer signal */ | |
1445 | sigfillset(&act.sa_mask); | |
1446 | act.sa_flags = 0; | |
c8994013 TS |
1447 | act.sa_handler = host_alarm_handler; |
1448 | ||
1449 | sigaction(SIGIO, &act, NULL); | |
7183b4b4 | 1450 | fcntl_setfl(fd, O_ASYNC); |
c8994013 TS |
1451 | fcntl(fd, F_SETOWN, getpid()); |
1452 | } | |
829309c7 | 1453 | |
c40ec5a9 TS |
1454 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1455 | { | |
1456 | struct hpet_info info; | |
1457 | int r, fd; | |
1458 | ||
1459 | fd = open("/dev/hpet", O_RDONLY); | |
1460 | if (fd < 0) | |
1461 | return -1; | |
1462 | ||
1463 | /* Set frequency */ | |
1464 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); | |
1465 | if (r < 0) { | |
1466 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" | |
1467 | "error, but for better emulation accuracy type:\n" | |
1468 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); | |
1469 | goto fail; | |
1470 | } | |
1471 | ||
1472 | /* Check capabilities */ | |
1473 | r = ioctl(fd, HPET_INFO, &info); | |
1474 | if (r < 0) | |
1475 | goto fail; | |
1476 | ||
1477 | /* Enable periodic mode */ | |
1478 | r = ioctl(fd, HPET_EPI, 0); | |
1479 | if (info.hi_flags && (r < 0)) | |
1480 | goto fail; | |
1481 | ||
1482 | /* Enable interrupt */ | |
1483 | r = ioctl(fd, HPET_IE_ON, 0); | |
1484 | if (r < 0) | |
1485 | goto fail; | |
1486 | ||
1487 | enable_sigio_timer(fd); | |
fcdc2129 | 1488 | t->priv = (void *)(long)fd; |
c40ec5a9 TS |
1489 | |
1490 | return 0; | |
1491 | fail: | |
1492 | close(fd); | |
1493 | return -1; | |
1494 | } | |
1495 | ||
1496 | static void hpet_stop_timer(struct qemu_alarm_timer *t) | |
1497 | { | |
fcdc2129 | 1498 | int fd = (long)t->priv; |
c40ec5a9 TS |
1499 | |
1500 | close(fd); | |
1501 | } | |
1502 | ||
c8994013 | 1503 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
fd872598 | 1504 | { |
c8994013 | 1505 | int rtc_fd; |
b5a23ad4 | 1506 | unsigned long current_rtc_freq = 0; |
c8994013 | 1507 | |
aeb30be6 | 1508 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
fd872598 FB |
1509 | if (rtc_fd < 0) |
1510 | return -1; | |
b5a23ad4 AZ |
1511 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1512 | if (current_rtc_freq != RTC_FREQ && | |
1513 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { | |
fd872598 FB |
1514 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
1515 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" | |
1516 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); | |
1517 | goto fail; | |
1518 | } | |
1519 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { | |
1520 | fail: | |
1521 | close(rtc_fd); | |
1522 | return -1; | |
1523 | } | |
c8994013 TS |
1524 | |
1525 | enable_sigio_timer(rtc_fd); | |
1526 | ||
fcdc2129 | 1527 | t->priv = (void *)(long)rtc_fd; |
c8994013 | 1528 | |
fd872598 FB |
1529 | return 0; |
1530 | } | |
1531 | ||
c8994013 | 1532 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
829309c7 | 1533 | { |
fcdc2129 | 1534 | int rtc_fd = (long)t->priv; |
c8994013 TS |
1535 | |
1536 | close(rtc_fd); | |
829309c7 FB |
1537 | } |
1538 | ||
efe75411 TS |
1539 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1540 | { | |
1541 | struct sigevent ev; | |
1542 | timer_t host_timer; | |
1543 | struct sigaction act; | |
1544 | ||
1545 | sigfillset(&act.sa_mask); | |
1546 | act.sa_flags = 0; | |
efe75411 TS |
1547 | act.sa_handler = host_alarm_handler; |
1548 | ||
1549 | sigaction(SIGALRM, &act, NULL); | |
1550 | ||
1551 | ev.sigev_value.sival_int = 0; | |
1552 | ev.sigev_notify = SIGEV_SIGNAL; | |
1553 | ev.sigev_signo = SIGALRM; | |
1554 | ||
1555 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { | |
1556 | perror("timer_create"); | |
1557 | ||
1558 | /* disable dynticks */ | |
1559 | fprintf(stderr, "Dynamic Ticks disabled\n"); | |
1560 | ||
1561 | return -1; | |
1562 | } | |
1563 | ||
0399bfe0 | 1564 | t->priv = (void *)(long)host_timer; |
efe75411 TS |
1565 | |
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) | |
1570 | { | |
0399bfe0 | 1571 | timer_t host_timer = (timer_t)(long)t->priv; |
efe75411 TS |
1572 | |
1573 | timer_delete(host_timer); | |
1574 | } | |
1575 | ||
1576 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) | |
1577 | { | |
0399bfe0 | 1578 | timer_t host_timer = (timer_t)(long)t->priv; |
efe75411 TS |
1579 | struct itimerspec timeout; |
1580 | int64_t nearest_delta_us = INT64_MAX; | |
1581 | int64_t current_us; | |
1582 | ||
1583 | if (!active_timers[QEMU_TIMER_REALTIME] && | |
1584 | !active_timers[QEMU_TIMER_VIRTUAL]) | |
d5d08334 | 1585 | return; |
efe75411 | 1586 | |
2e70f6ef | 1587 | nearest_delta_us = qemu_next_deadline_dyntick(); |
efe75411 TS |
1588 | |
1589 | /* check whether a timer is already running */ | |
1590 | if (timer_gettime(host_timer, &timeout)) { | |
1591 | perror("gettime"); | |
1592 | fprintf(stderr, "Internal timer error: aborting\n"); | |
1593 | exit(1); | |
1594 | } | |
1595 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; | |
1596 | if (current_us && current_us <= nearest_delta_us) | |
1597 | return; | |
1598 | ||
1599 | timeout.it_interval.tv_sec = 0; | |
1600 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ | |
1601 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; | |
1602 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; | |
1603 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { | |
1604 | perror("settime"); | |
1605 | fprintf(stderr, "Internal timer error: aborting\n"); | |
1606 | exit(1); | |
1607 | } | |
1608 | } | |
1609 | ||
70744b3a | 1610 | #endif /* defined(__linux__) */ |
231c6586 | 1611 | |
c8994013 TS |
1612 | static int unix_start_timer(struct qemu_alarm_timer *t) |
1613 | { | |
1614 | struct sigaction act; | |
1615 | struct itimerval itv; | |
1616 | int err; | |
1617 | ||
1618 | /* timer signal */ | |
1619 | sigfillset(&act.sa_mask); | |
1620 | act.sa_flags = 0; | |
c8994013 TS |
1621 | act.sa_handler = host_alarm_handler; |
1622 | ||
1623 | sigaction(SIGALRM, &act, NULL); | |
1624 | ||
1625 | itv.it_interval.tv_sec = 0; | |
1626 | /* for i386 kernel 2.6 to get 1 ms */ | |
1627 | itv.it_interval.tv_usec = 999; | |
1628 | itv.it_value.tv_sec = 0; | |
1629 | itv.it_value.tv_usec = 10 * 1000; | |
1630 | ||
1631 | err = setitimer(ITIMER_REAL, &itv, NULL); | |
1632 | if (err) | |
1633 | return -1; | |
1634 | ||
1635 | return 0; | |
1636 | } | |
1637 | ||
1638 | static void unix_stop_timer(struct qemu_alarm_timer *t) | |
1639 | { | |
1640 | struct itimerval itv; | |
1641 | ||
1642 | memset(&itv, 0, sizeof(itv)); | |
1643 | setitimer(ITIMER_REAL, &itv, NULL); | |
1644 | } | |
1645 | ||
829309c7 | 1646 | #endif /* !defined(_WIN32) */ |
fd872598 | 1647 | |
f49e58dc AL |
1648 | static void try_to_rearm_timer(void *opaque) |
1649 | { | |
1650 | struct qemu_alarm_timer *t = opaque; | |
1651 | #ifndef _WIN32 | |
1652 | ssize_t len; | |
1653 | ||
1654 | /* Drain the notify pipe */ | |
1655 | do { | |
1656 | char buffer[512]; | |
1657 | len = read(alarm_timer_rfd, buffer, sizeof(buffer)); | |
1658 | } while ((len == -1 && errno == EINTR) || len > 0); | |
1659 | #endif | |
1660 | ||
f49e58dc AL |
1661 | if (t->flags & ALARM_FLAG_EXPIRED) { |
1662 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; | |
1663 | qemu_rearm_alarm_timer(alarm_timer); | |
1664 | } | |
1665 | } | |
1666 | ||
c8994013 TS |
1667 | #ifdef _WIN32 |
1668 | ||
1669 | static int win32_start_timer(struct qemu_alarm_timer *t) | |
1670 | { | |
1671 | TIMECAPS tc; | |
1672 | struct qemu_alarm_win32 *data = t->priv; | |
efe75411 | 1673 | UINT flags; |
c8994013 TS |
1674 | |
1675 | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); | |
1676 | if (!data->host_alarm) { | |
1677 | perror("Failed CreateEvent"); | |
c396a7f0 | 1678 | return -1; |
c8994013 TS |
1679 | } |
1680 | ||
1681 | memset(&tc, 0, sizeof(tc)); | |
1682 | timeGetDevCaps(&tc, sizeof(tc)); | |
1683 | ||
1684 | if (data->period < tc.wPeriodMin) | |
1685 | data->period = tc.wPeriodMin; | |
1686 | ||
1687 | timeBeginPeriod(data->period); | |
1688 | ||
efe75411 TS |
1689 | flags = TIME_CALLBACK_FUNCTION; |
1690 | if (alarm_has_dynticks(t)) | |
1691 | flags |= TIME_ONESHOT; | |
1692 | else | |
1693 | flags |= TIME_PERIODIC; | |
1694 | ||
c8994013 TS |
1695 | data->timerId = timeSetEvent(1, // interval (ms) |
1696 | data->period, // resolution | |
1697 | host_alarm_handler, // function | |
1698 | (DWORD)t, // parameter | |
efe75411 | 1699 | flags); |
c8994013 TS |
1700 | |
1701 | if (!data->timerId) { | |
1702 | perror("Failed to initialize win32 alarm timer"); | |
1703 | ||
1704 | timeEndPeriod(data->period); | |
1705 | CloseHandle(data->host_alarm); | |
1706 | return -1; | |
1707 | } | |
1708 | ||
f49e58dc | 1709 | qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t); |
c8994013 TS |
1710 | |
1711 | return 0; | |
1712 | } | |
1713 | ||
1714 | static void win32_stop_timer(struct qemu_alarm_timer *t) | |
1715 | { | |
1716 | struct qemu_alarm_win32 *data = t->priv; | |
1717 | ||
1718 | timeKillEvent(data->timerId); | |
1719 | timeEndPeriod(data->period); | |
1720 | ||
1721 | CloseHandle(data->host_alarm); | |
1722 | } | |
1723 | ||
efe75411 TS |
1724 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1725 | { | |
1726 | struct qemu_alarm_win32 *data = t->priv; | |
1727 | uint64_t nearest_delta_us; | |
1728 | ||
1729 | if (!active_timers[QEMU_TIMER_REALTIME] && | |
1730 | !active_timers[QEMU_TIMER_VIRTUAL]) | |
d5d08334 | 1731 | return; |
efe75411 | 1732 | |
2e70f6ef | 1733 | nearest_delta_us = qemu_next_deadline_dyntick(); |
efe75411 TS |
1734 | nearest_delta_us /= 1000; |
1735 | ||
1736 | timeKillEvent(data->timerId); | |
1737 | ||
1738 | data->timerId = timeSetEvent(1, | |
1739 | data->period, | |
1740 | host_alarm_handler, | |
1741 | (DWORD)t, | |
1742 | TIME_ONESHOT | TIME_PERIODIC); | |
1743 | ||
1744 | if (!data->timerId) { | |
1745 | perror("Failed to re-arm win32 alarm timer"); | |
1746 | ||
1747 | timeEndPeriod(data->period); | |
1748 | CloseHandle(data->host_alarm); | |
1749 | exit(1); | |
1750 | } | |
1751 | } | |
1752 | ||
c8994013 TS |
1753 | #endif /* _WIN32 */ |
1754 | ||
7183b4b4 | 1755 | static int init_timer_alarm(void) |
8a7ddc38 | 1756 | { |
223f0d72 | 1757 | struct qemu_alarm_timer *t = NULL; |
c8994013 | 1758 | int i, err = -1; |
f49e58dc AL |
1759 | |
1760 | #ifndef _WIN32 | |
c96f1a48 AL |
1761 | int fds[2]; |
1762 | ||
7183b4b4 AL |
1763 | err = pipe(fds); |
1764 | if (err == -1) | |
1765 | return -errno; | |
1766 | ||
1767 | err = fcntl_setfl(fds[0], O_NONBLOCK); | |
1768 | if (err < 0) | |
1769 | goto fail; | |
1770 | ||
1771 | err = fcntl_setfl(fds[1], O_NONBLOCK); | |
1772 | if (err < 0) | |
1773 | goto fail; | |
1774 | ||
c96f1a48 AL |
1775 | alarm_timer_rfd = fds[0]; |
1776 | alarm_timer_wfd = fds[1]; | |
f49e58dc | 1777 | #endif |
c8994013 TS |
1778 | |
1779 | for (i = 0; alarm_timers[i].name; i++) { | |
1780 | t = &alarm_timers[i]; | |
1781 | ||
c8994013 TS |
1782 | err = t->start(t); |
1783 | if (!err) | |
1784 | break; | |
67b915a5 | 1785 | } |
fd872598 | 1786 | |
c8994013 | 1787 | if (err) { |
7183b4b4 AL |
1788 | err = -ENOENT; |
1789 | goto fail; | |
67b915a5 | 1790 | } |
c8994013 | 1791 | |
f49e58dc | 1792 | #ifndef _WIN32 |
6abfbd79 AL |
1793 | qemu_set_fd_handler2(alarm_timer_rfd, NULL, |
1794 | try_to_rearm_timer, NULL, t); | |
f49e58dc | 1795 | #endif |
6abfbd79 | 1796 | |
c8994013 | 1797 | alarm_timer = t; |
7183b4b4 | 1798 | |
6abfbd79 | 1799 | return 0; |
7183b4b4 AL |
1800 | |
1801 | fail: | |
f49e58dc | 1802 | #ifndef _WIN32 |
7183b4b4 AL |
1803 | close(fds[0]); |
1804 | close(fds[1]); | |
f49e58dc | 1805 | #endif |
7183b4b4 | 1806 | return err; |
8a7ddc38 FB |
1807 | } |
1808 | ||
9596ebb7 | 1809 | static void quit_timers(void) |
40c3bac3 | 1810 | { |
c8994013 TS |
1811 | alarm_timer->stop(alarm_timer); |
1812 | alarm_timer = NULL; | |
40c3bac3 FB |
1813 | } |
1814 | ||
f6503059 AZ |
1815 | /***********************************************************/ |
1816 | /* host time/date access */ | |
1817 | void qemu_get_timedate(struct tm *tm, int offset) | |
1818 | { | |
1819 | time_t ti; | |
1820 | struct tm *ret; | |
1821 | ||
1822 | time(&ti); | |
1823 | ti += offset; | |
1824 | if (rtc_date_offset == -1) { | |
1825 | if (rtc_utc) | |
1826 | ret = gmtime(&ti); | |
1827 | else | |
1828 | ret = localtime(&ti); | |
1829 | } else { | |
1830 | ti -= rtc_date_offset; | |
1831 | ret = gmtime(&ti); | |
1832 | } | |
1833 | ||
1834 | memcpy(tm, ret, sizeof(struct tm)); | |
1835 | } | |
1836 | ||
1837 | int qemu_timedate_diff(struct tm *tm) | |
1838 | { | |
1839 | time_t seconds; | |
1840 | ||
1841 | if (rtc_date_offset == -1) | |
1842 | if (rtc_utc) | |
1843 | seconds = mktimegm(tm); | |
1844 | else | |
1845 | seconds = mktime(tm); | |
1846 | else | |
1847 | seconds = mktimegm(tm) + rtc_date_offset; | |
1848 | ||
1849 | return seconds - time(NULL); | |
1850 | } | |
1851 | ||
fd1dff4b | 1852 | #ifdef _WIN32 |
fd1dff4b FB |
1853 | static void socket_cleanup(void) |
1854 | { | |
1855 | WSACleanup(); | |
1856 | } | |
82c643ff | 1857 | |
fd1dff4b FB |
1858 | static int socket_init(void) |
1859 | { | |
1860 | WSADATA Data; | |
1861 | int ret, err; | |
1862 | ||
1863 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
1864 | if (ret != 0) { | |
1865 | err = WSAGetLastError(); | |
1866 | fprintf(stderr, "WSAStartup: %d\n", err); | |
1867 | return -1; | |
1868 | } | |
1869 | atexit(socket_cleanup); | |
1870 | return 0; | |
1871 | } | |
64b7b733 AJ |
1872 | #endif |
1873 | ||
268a362c | 1874 | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
609497ab AZ |
1875 | { |
1876 | char *q; | |
1877 | ||
1878 | q = buf; | |
268a362c | 1879 | while (*p != '\0' && *p != delim) { |
609497ab AZ |
1880 | if (q && (q - buf) < buf_size - 1) |
1881 | *q++ = *p; | |
1882 | p++; | |
1883 | } | |
1884 | if (q) | |
1885 | *q = '\0'; | |
1886 | ||
1887 | return p; | |
1888 | } | |
1889 | ||
63a01ef8 | 1890 | const char *get_opt_value(char *buf, int buf_size, const char *p) |
e4bcb14c TS |
1891 | { |
1892 | char *q; | |
e4bcb14c | 1893 | |
e4bcb14c TS |
1894 | q = buf; |
1895 | while (*p != '\0') { | |
609497ab AZ |
1896 | if (*p == ',') { |
1897 | if (*(p + 1) != ',') | |
e4bcb14c | 1898 | break; |
e4bcb14c | 1899 | p++; |
609497ab | 1900 | } |
e4bcb14c TS |
1901 | if (q && (q - buf) < buf_size - 1) |
1902 | *q++ = *p; | |
1903 | p++; | |
1904 | } | |
1905 | if (q) | |
1906 | *q = '\0'; | |
1907 | ||
1908 | return p; | |
1909 | } | |
1910 | ||
63a01ef8 AL |
1911 | int get_param_value(char *buf, int buf_size, |
1912 | const char *tag, const char *str) | |
7c9d8e07 FB |
1913 | { |
1914 | const char *p; | |
7c9d8e07 FB |
1915 | char option[128]; |
1916 | ||
1917 | p = str; | |
1918 | for(;;) { | |
268a362c | 1919 | p = get_opt_name(option, sizeof(option), p, '='); |
7c9d8e07 FB |
1920 | if (*p != '=') |
1921 | break; | |
1922 | p++; | |
1923 | if (!strcmp(tag, option)) { | |
609497ab | 1924 | (void)get_opt_value(buf, buf_size, p); |
e4bcb14c | 1925 | return strlen(buf); |
7c9d8e07 | 1926 | } else { |
609497ab | 1927 | p = get_opt_value(NULL, 0, p); |
7c9d8e07 FB |
1928 | } |
1929 | if (*p != ',') | |
1930 | break; | |
1931 | p++; | |
1932 | } | |
1933 | return 0; | |
1934 | } | |
1935 | ||
63a01ef8 AL |
1936 | int check_params(char *buf, int buf_size, |
1937 | const char * const *params, const char *str) | |
e4bcb14c TS |
1938 | { |
1939 | const char *p; | |
1940 | int i; | |
1941 | ||
1942 | p = str; | |
10300216 | 1943 | while (*p != '\0') { |
268a362c | 1944 | p = get_opt_name(buf, buf_size, p, '='); |
e4bcb14c TS |
1945 | if (*p != '=') |
1946 | return -1; | |
1947 | p++; | |
1948 | for(i = 0; params[i] != NULL; i++) | |
1949 | if (!strcmp(params[i], buf)) | |
1950 | break; | |
1951 | if (params[i] == NULL) | |
1952 | return -1; | |
609497ab | 1953 | p = get_opt_value(NULL, 0, p); |
e4bcb14c TS |
1954 | if (*p != ',') |
1955 | break; | |
1956 | p++; | |
1957 | } | |
1958 | return 0; | |
1959 | } | |
1960 | ||
1ae26a18 AZ |
1961 | /***********************************************************/ |
1962 | /* Bluetooth support */ | |
1963 | static int nb_hcis; | |
1964 | static int cur_hci; | |
1965 | static struct HCIInfo *hci_table[MAX_NICS]; | |
dc72ac14 | 1966 | |
1ae26a18 AZ |
1967 | static struct bt_vlan_s { |
1968 | struct bt_scatternet_s net; | |
1969 | int id; | |
1970 | struct bt_vlan_s *next; | |
1971 | } *first_bt_vlan; | |
1972 | ||
1973 | /* find or alloc a new bluetooth "VLAN" */ | |
674bb261 | 1974 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
1ae26a18 AZ |
1975 | { |
1976 | struct bt_vlan_s **pvlan, *vlan; | |
1977 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { | |
1978 | if (vlan->id == id) | |
1979 | return &vlan->net; | |
1980 | } | |
1981 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); | |
1982 | vlan->id = id; | |
1983 | pvlan = &first_bt_vlan; | |
1984 | while (*pvlan != NULL) | |
1985 | pvlan = &(*pvlan)->next; | |
1986 | *pvlan = vlan; | |
1987 | return &vlan->net; | |
1988 | } | |
1989 | ||
1990 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) | |
1991 | { | |
1992 | } | |
1993 | ||
1994 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) | |
1995 | { | |
1996 | return -ENOTSUP; | |
1997 | } | |
1998 | ||
1999 | static struct HCIInfo null_hci = { | |
2000 | .cmd_send = null_hci_send, | |
2001 | .sco_send = null_hci_send, | |
2002 | .acl_send = null_hci_send, | |
2003 | .bdaddr_set = null_hci_addr_set, | |
2004 | }; | |
2005 | ||
2006 | struct HCIInfo *qemu_next_hci(void) | |
2007 | { | |
2008 | if (cur_hci == nb_hcis) | |
2009 | return &null_hci; | |
2010 | ||
2011 | return hci_table[cur_hci++]; | |
2012 | } | |
2013 | ||
dc72ac14 AZ |
2014 | static struct HCIInfo *hci_init(const char *str) |
2015 | { | |
2016 | char *endp; | |
2017 | struct bt_scatternet_s *vlan = 0; | |
2018 | ||
2019 | if (!strcmp(str, "null")) | |
2020 | /* null */ | |
2021 | return &null_hci; | |
2022 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) | |
2023 | /* host[:hciN] */ | |
2024 | return bt_host_hci(str[4] ? str + 5 : "hci0"); | |
2025 | else if (!strncmp(str, "hci", 3)) { | |
2026 | /* hci[,vlan=n] */ | |
2027 | if (str[3]) { | |
2028 | if (!strncmp(str + 3, ",vlan=", 6)) { | |
2029 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); | |
2030 | if (*endp) | |
2031 | vlan = 0; | |
2032 | } | |
2033 | } else | |
2034 | vlan = qemu_find_bt_vlan(0); | |
2035 | if (vlan) | |
2036 | return bt_new_hci(vlan); | |
2037 | } | |
2038 | ||
2039 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); | |
2040 | ||
2041 | return 0; | |
2042 | } | |
2043 | ||
2044 | static int bt_hci_parse(const char *str) | |
2045 | { | |
2046 | struct HCIInfo *hci; | |
2047 | bdaddr_t bdaddr; | |
2048 | ||
2049 | if (nb_hcis >= MAX_NICS) { | |
2050 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); | |
2051 | return -1; | |
2052 | } | |
2053 | ||
2054 | hci = hci_init(str); | |
2055 | if (!hci) | |
2056 | return -1; | |
2057 | ||
2058 | bdaddr.b[0] = 0x52; | |
2059 | bdaddr.b[1] = 0x54; | |
2060 | bdaddr.b[2] = 0x00; | |
2061 | bdaddr.b[3] = 0x12; | |
2062 | bdaddr.b[4] = 0x34; | |
2063 | bdaddr.b[5] = 0x56 + nb_hcis; | |
2064 | hci->bdaddr_set(hci, bdaddr.b); | |
2065 | ||
2066 | hci_table[nb_hcis++] = hci; | |
2067 | ||
2068 | return 0; | |
2069 | } | |
2070 | ||
2071 | static void bt_vhci_add(int vlan_id) | |
2072 | { | |
2073 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); | |
2074 | ||
2075 | if (!vlan->slave) | |
2076 | fprintf(stderr, "qemu: warning: adding a VHCI to " | |
2077 | "an empty scatternet %i\n", vlan_id); | |
2078 | ||
2079 | bt_vhci_init(bt_new_hci(vlan)); | |
2080 | } | |
2081 | ||
2082 | static struct bt_device_s *bt_device_add(const char *opt) | |
2083 | { | |
2084 | struct bt_scatternet_s *vlan; | |
2085 | int vlan_id = 0; | |
2086 | char *endp = strstr(opt, ",vlan="); | |
2087 | int len = (endp ? endp - opt : strlen(opt)) + 1; | |
2088 | char devname[10]; | |
2089 | ||
2090 | pstrcpy(devname, MIN(sizeof(devname), len), opt); | |
2091 | ||
2092 | if (endp) { | |
2093 | vlan_id = strtol(endp + 6, &endp, 0); | |
2094 | if (*endp) { | |
2095 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); | |
2096 | return 0; | |
2097 | } | |
2098 | } | |
2099 | ||
2100 | vlan = qemu_find_bt_vlan(vlan_id); | |
2101 | ||
2102 | if (!vlan->slave) | |
2103 | fprintf(stderr, "qemu: warning: adding a slave device to " | |
2104 | "an empty scatternet %i\n", vlan_id); | |
2105 | ||
2106 | if (!strcmp(devname, "keyboard")) | |
2107 | return bt_keyboard_init(vlan); | |
2108 | ||
2109 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
2113 | static int bt_parse(const char *opt) | |
2114 | { | |
2115 | const char *endp, *p; | |
2116 | int vlan; | |
2117 | ||
2118 | if (strstart(opt, "hci", &endp)) { | |
2119 | if (!*endp || *endp == ',') { | |
2120 | if (*endp) | |
2121 | if (!strstart(endp, ",vlan=", 0)) | |
2122 | opt = endp + 1; | |
2123 | ||
2124 | return bt_hci_parse(opt); | |
2125 | } | |
2126 | } else if (strstart(opt, "vhci", &endp)) { | |
2127 | if (!*endp || *endp == ',') { | |
2128 | if (*endp) { | |
2129 | if (strstart(endp, ",vlan=", &p)) { | |
2130 | vlan = strtol(p, (char **) &endp, 0); | |
2131 | if (*endp) { | |
2132 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); | |
2133 | return 1; | |
2134 | } | |
2135 | } else { | |
2136 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); | |
2137 | return 1; | |
2138 | } | |
2139 | } else | |
2140 | vlan = 0; | |
2141 | ||
2142 | bt_vhci_add(vlan); | |
2143 | return 0; | |
2144 | } | |
2145 | } else if (strstart(opt, "device:", &endp)) | |
2146 | return !bt_device_add(endp); | |
2147 | ||
2148 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); | |
2149 | return 1; | |
2150 | } | |
2151 | ||
1ae26a18 AZ |
2152 | /***********************************************************/ |
2153 | /* QEMU Block devices */ | |
2154 | ||
609497ab | 2155 | #define HD_ALIAS "index=%d,media=disk" |
e4bcb14c | 2156 | #define CDROM_ALIAS "index=2,media=cdrom" |
e4bcb14c | 2157 | #define FD_ALIAS "index=%d,if=floppy" |
609497ab AZ |
2158 | #define PFLASH_ALIAS "if=pflash" |
2159 | #define MTD_ALIAS "if=mtd" | |
9d413d1d | 2160 | #define SD_ALIAS "index=0,if=sd" |
e4bcb14c | 2161 | |
7d5aca9e AL |
2162 | static int drive_opt_get_free_idx(void) |
2163 | { | |
2164 | int index; | |
2165 | ||
2166 | for (index = 0; index < MAX_DRIVES; index++) | |
2167 | if (!drives_opt[index].used) { | |
2168 | drives_opt[index].used = 1; | |
2169 | return index; | |
2170 | } | |
2171 | ||
2172 | return -1; | |
2173 | } | |
2174 | ||
2175 | static int drive_get_free_idx(void) | |
2176 | { | |
2177 | int index; | |
2178 | ||
2179 | for (index = 0; index < MAX_DRIVES; index++) | |
2180 | if (!drives_table[index].used) { | |
2181 | drives_table[index].used = 1; | |
2182 | return index; | |
2183 | } | |
2184 | ||
2185 | return -1; | |
2186 | } | |
2187 | ||
4d73cd3b | 2188 | int drive_add(const char *file, const char *fmt, ...) |
e4bcb14c TS |
2189 | { |
2190 | va_list ap; | |
7d5aca9e | 2191 | int index = drive_opt_get_free_idx(); |
e4bcb14c | 2192 | |
7d5aca9e | 2193 | if (nb_drives_opt >= MAX_DRIVES || index == -1) { |
e4bcb14c | 2194 | fprintf(stderr, "qemu: too many drives\n"); |
4d73cd3b | 2195 | return -1; |
e4bcb14c TS |
2196 | } |
2197 | ||
7d5aca9e | 2198 | drives_opt[index].file = file; |
e4bcb14c | 2199 | va_start(ap, fmt); |
7d5aca9e | 2200 | vsnprintf(drives_opt[index].opt, |
609497ab | 2201 | sizeof(drives_opt[0].opt), fmt, ap); |
e4bcb14c TS |
2202 | va_end(ap); |
2203 | ||
7d5aca9e AL |
2204 | nb_drives_opt++; |
2205 | return index; | |
e4bcb14c TS |
2206 | } |
2207 | ||
b01b1111 AL |
2208 | void drive_remove(int index) |
2209 | { | |
2210 | drives_opt[index].used = 0; | |
2211 | nb_drives_opt--; | |
2212 | } | |
2213 | ||
f60d39bc | 2214 | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
e4bcb14c TS |
2215 | { |
2216 | int index; | |
2217 | ||
2218 | /* seek interface, bus and unit */ | |
2219 | ||
7d5aca9e | 2220 | for (index = 0; index < MAX_DRIVES; index++) |
f60d39bc | 2221 | if (drives_table[index].type == type && |
e4bcb14c | 2222 | drives_table[index].bus == bus && |
7d5aca9e AL |
2223 | drives_table[index].unit == unit && |
2224 | drives_table[index].used) | |
e4bcb14c TS |
2225 | return index; |
2226 | ||
2227 | return -1; | |
2228 | } | |
2229 | ||
f60d39bc | 2230 | int drive_get_max_bus(BlockInterfaceType type) |
e4bcb14c TS |
2231 | { |
2232 | int max_bus; | |
2233 | int index; | |
2234 | ||
2235 | max_bus = -1; | |
2236 | for (index = 0; index < nb_drives; index++) { | |
f60d39bc | 2237 | if(drives_table[index].type == type && |
e4bcb14c TS |
2238 | drives_table[index].bus > max_bus) |
2239 | max_bus = drives_table[index].bus; | |
2240 | } | |
2241 | return max_bus; | |
2242 | } | |
2243 | ||
fa879c64 AL |
2244 | const char *drive_get_serial(BlockDriverState *bdrv) |
2245 | { | |
2246 | int index; | |
2247 | ||
2248 | for (index = 0; index < nb_drives; index++) | |
2249 | if (drives_table[index].bdrv == bdrv) | |
2250 | return drives_table[index].serial; | |
2251 | ||
2252 | return "\0"; | |
2253 | } | |
2254 | ||
428c5705 AL |
2255 | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
2256 | { | |
2257 | int index; | |
2258 | ||
2259 | for (index = 0; index < nb_drives; index++) | |
2260 | if (drives_table[index].bdrv == bdrv) | |
2261 | return drives_table[index].onerror; | |
2262 | ||
cdad4bd8 | 2263 | return BLOCK_ERR_STOP_ENOSPC; |
428c5705 AL |
2264 | } |
2265 | ||
a1620fac AJ |
2266 | static void bdrv_format_print(void *opaque, const char *name) |
2267 | { | |
2268 | fprintf(stderr, " %s", name); | |
2269 | } | |
2270 | ||
b01b1111 AL |
2271 | void drive_uninit(BlockDriverState *bdrv) |
2272 | { | |
2273 | int i; | |
2274 | ||
2275 | for (i = 0; i < MAX_DRIVES; i++) | |
2276 | if (drives_table[i].bdrv == bdrv) { | |
2277 | drives_table[i].bdrv = NULL; | |
2278 | drives_table[i].used = 0; | |
2279 | drive_remove(drives_table[i].drive_opt_idx); | |
2280 | nb_drives--; | |
2281 | break; | |
2282 | } | |
2283 | } | |
2284 | ||
4d73cd3b | 2285 | int drive_init(struct drive_opt *arg, int snapshot, void *opaque) |
e4bcb14c TS |
2286 | { |
2287 | char buf[128]; | |
2288 | char file[1024]; | |
c8522bdf | 2289 | char devname[128]; |
fa879c64 | 2290 | char serial[21]; |
c8522bdf | 2291 | const char *mediastr = ""; |
f60d39bc | 2292 | BlockInterfaceType type; |
e4bcb14c TS |
2293 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
2294 | int bus_id, unit_id; | |
2295 | int cyls, heads, secs, translation; | |
2296 | BlockDriverState *bdrv; | |
1e72d3b7 | 2297 | BlockDriver *drv = NULL; |
4d73cd3b | 2298 | QEMUMachine *machine = opaque; |
e4bcb14c TS |
2299 | int max_devs; |
2300 | int index; | |
33f00271 | 2301 | int cache; |
428c5705 | 2302 | int bdrv_flags, onerror; |
7d5aca9e | 2303 | int drives_table_idx; |
609497ab | 2304 | char *str = arg->opt; |
7ccfb2eb BS |
2305 | static const char * const params[] = { "bus", "unit", "if", "index", |
2306 | "cyls", "heads", "secs", "trans", | |
2307 | "media", "snapshot", "file", | |
428c5705 AL |
2308 | "cache", "format", "serial", "werror", |
2309 | NULL }; | |
e4bcb14c TS |
2310 | |
2311 | if (check_params(buf, sizeof(buf), params, str) < 0) { | |
ff993638 | 2312 | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n", |
e4bcb14c TS |
2313 | buf, str); |
2314 | return -1; | |
2315 | } | |
2316 | ||
2317 | file[0] = 0; | |
2318 | cyls = heads = secs = 0; | |
2319 | bus_id = 0; | |
2320 | unit_id = -1; | |
2321 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
2322 | index = -1; | |
4dc822d7 | 2323 | cache = 3; |
e4bcb14c | 2324 | |
c9b1ae2c | 2325 | if (machine->use_scsi) { |
f60d39bc | 2326 | type = IF_SCSI; |
e4bcb14c | 2327 | max_devs = MAX_SCSI_DEVS; |
363a37d5 | 2328 | pstrcpy(devname, sizeof(devname), "scsi"); |
e4bcb14c | 2329 | } else { |
f60d39bc | 2330 | type = IF_IDE; |
e4bcb14c | 2331 | max_devs = MAX_IDE_DEVS; |
363a37d5 | 2332 | pstrcpy(devname, sizeof(devname), "ide"); |
e4bcb14c TS |
2333 | } |
2334 | media = MEDIA_DISK; | |
2335 | ||
2336 | /* extract parameters */ | |
2337 | ||
2338 | if (get_param_value(buf, sizeof(buf), "bus", str)) { | |
2339 | bus_id = strtol(buf, NULL, 0); | |
2340 | if (bus_id < 0) { | |
2341 | fprintf(stderr, "qemu: '%s' invalid bus id\n", str); | |
2342 | return -1; | |
2343 | } | |
2344 | } | |
2345 | ||
2346 | if (get_param_value(buf, sizeof(buf), "unit", str)) { | |
2347 | unit_id = strtol(buf, NULL, 0); | |
2348 | if (unit_id < 0) { | |
2349 | fprintf(stderr, "qemu: '%s' invalid unit id\n", str); | |
2350 | return -1; | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | if (get_param_value(buf, sizeof(buf), "if", str)) { | |
ae45d369 | 2355 | pstrcpy(devname, sizeof(devname), buf); |
e4bcb14c | 2356 | if (!strcmp(buf, "ide")) { |
f60d39bc | 2357 | type = IF_IDE; |
e4bcb14c TS |
2358 | max_devs = MAX_IDE_DEVS; |
2359 | } else if (!strcmp(buf, "scsi")) { | |
f60d39bc | 2360 | type = IF_SCSI; |
e4bcb14c TS |
2361 | max_devs = MAX_SCSI_DEVS; |
2362 | } else if (!strcmp(buf, "floppy")) { | |
f60d39bc | 2363 | type = IF_FLOPPY; |
e4bcb14c TS |
2364 | max_devs = 0; |
2365 | } else if (!strcmp(buf, "pflash")) { | |
f60d39bc | 2366 | type = IF_PFLASH; |
e4bcb14c TS |
2367 | max_devs = 0; |
2368 | } else if (!strcmp(buf, "mtd")) { | |
f60d39bc | 2369 | type = IF_MTD; |
e4bcb14c TS |
2370 | max_devs = 0; |
2371 | } else if (!strcmp(buf, "sd")) { | |
f60d39bc | 2372 | type = IF_SD; |
e4bcb14c | 2373 | max_devs = 0; |
6e02c38d AL |
2374 | } else if (!strcmp(buf, "virtio")) { |
2375 | type = IF_VIRTIO; | |
2376 | max_devs = 0; | |
62d23efa AL |
2377 | } else if (!strcmp(buf, "xen")) { |
2378 | type = IF_XEN; | |
2379 | max_devs = 0; | |
2380 | } else { | |
e4bcb14c TS |
2381 | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf); |
2382 | return -1; | |
2383 | } | |
2384 | } | |
2385 | ||
2386 | if (get_param_value(buf, sizeof(buf), "index", str)) { | |
2387 | index = strtol(buf, NULL, 0); | |
2388 | if (index < 0) { | |
2389 | fprintf(stderr, "qemu: '%s' invalid index\n", str); | |
2390 | return -1; | |
2391 | } | |
2392 | } | |
2393 | ||
2394 | if (get_param_value(buf, sizeof(buf), "cyls", str)) { | |
2395 | cyls = strtol(buf, NULL, 0); | |
2396 | } | |
2397 | ||
2398 | if (get_param_value(buf, sizeof(buf), "heads", str)) { | |
2399 | heads = strtol(buf, NULL, 0); | |
2400 | } | |
2401 | ||
2402 | if (get_param_value(buf, sizeof(buf), "secs", str)) { | |
2403 | secs = strtol(buf, NULL, 0); | |
2404 | } | |
2405 | ||
2406 | if (cyls || heads || secs) { | |
2407 | if (cyls < 1 || cyls > 16383) { | |
2408 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str); | |
2409 | return -1; | |
2410 | } | |
2411 | if (heads < 1 || heads > 16) { | |
2412 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str); | |
2413 | return -1; | |
2414 | } | |
2415 | if (secs < 1 || secs > 63) { | |
2416 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str); | |
2417 | return -1; | |
2418 | } | |
2419 | } | |
2420 | ||
2421 | if (get_param_value(buf, sizeof(buf), "trans", str)) { | |
2422 | if (!cyls) { | |
2423 | fprintf(stderr, | |
2424 | "qemu: '%s' trans must be used with cyls,heads and secs\n", | |
2425 | str); | |
2426 | return -1; | |
2427 | } | |
2428 | if (!strcmp(buf, "none")) | |
2429 | translation = BIOS_ATA_TRANSLATION_NONE; | |
2430 | else if (!strcmp(buf, "lba")) | |
2431 | translation = BIOS_ATA_TRANSLATION_LBA; | |
2432 | else if (!strcmp(buf, "auto")) | |
2433 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
2434 | else { | |
2435 | fprintf(stderr, "qemu: '%s' invalid translation type\n", str); | |
2436 | return -1; | |
2437 | } | |
2438 | } | |
2439 | ||
2440 | if (get_param_value(buf, sizeof(buf), "media", str)) { | |
2441 | if (!strcmp(buf, "disk")) { | |
2442 | media = MEDIA_DISK; | |
2443 | } else if (!strcmp(buf, "cdrom")) { | |
2444 | if (cyls || secs || heads) { | |
2445 | fprintf(stderr, | |
2446 | "qemu: '%s' invalid physical CHS format\n", str); | |
2447 | return -1; | |
2448 | } | |
2449 | media = MEDIA_CDROM; | |
2450 | } else { | |
2451 | fprintf(stderr, "qemu: '%s' invalid media\n", str); | |
2452 | return -1; | |
2453 | } | |
2454 | } | |
2455 | ||
2456 | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { | |
2457 | if (!strcmp(buf, "on")) | |
2458 | snapshot = 1; | |
2459 | else if (!strcmp(buf, "off")) | |
2460 | snapshot = 0; | |
2461 | else { | |
2462 | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str); | |
2463 | return -1; | |
2464 | } | |
2465 | } | |
2466 | ||
33f00271 | 2467 | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
9f7965c7 | 2468 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
33f00271 | 2469 | cache = 0; |
9f7965c7 | 2470 | else if (!strcmp(buf, "writethrough")) |
33f00271 | 2471 | cache = 1; |
9f7965c7 AL |
2472 | else if (!strcmp(buf, "writeback")) |
2473 | cache = 2; | |
33f00271 AZ |
2474 | else { |
2475 | fprintf(stderr, "qemu: invalid cache option\n"); | |
2476 | return -1; | |
2477 | } | |
2478 | } | |
2479 | ||
1e72d3b7 | 2480 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
a1620fac AJ |
2481 | if (strcmp(buf, "?") == 0) { |
2482 | fprintf(stderr, "qemu: Supported formats:"); | |
2483 | bdrv_iterate_format(bdrv_format_print, NULL); | |
2484 | fprintf(stderr, "\n"); | |
2485 | return -1; | |
2486 | } | |
1e72d3b7 AJ |
2487 | drv = bdrv_find_format(buf); |
2488 | if (!drv) { | |
2489 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); | |
2490 | return -1; | |
2491 | } | |
2492 | } | |
2493 | ||
609497ab AZ |
2494 | if (arg->file == NULL) |
2495 | get_param_value(file, sizeof(file), "file", str); | |
2496 | else | |
2497 | pstrcpy(file, sizeof(file), arg->file); | |
e4bcb14c | 2498 | |
fa879c64 AL |
2499 | if (!get_param_value(serial, sizeof(serial), "serial", str)) |
2500 | memset(serial, 0, sizeof(serial)); | |
2501 | ||
cdad4bd8 | 2502 | onerror = BLOCK_ERR_STOP_ENOSPC; |
428c5705 | 2503 | if (get_param_value(buf, sizeof(serial), "werror", str)) { |
869a5c6d | 2504 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
ea8a5d7f | 2505 | fprintf(stderr, "werror is no supported by this format\n"); |
428c5705 AL |
2506 | return -1; |
2507 | } | |
2508 | if (!strcmp(buf, "ignore")) | |
2509 | onerror = BLOCK_ERR_IGNORE; | |
2510 | else if (!strcmp(buf, "enospc")) | |
2511 | onerror = BLOCK_ERR_STOP_ENOSPC; | |
2512 | else if (!strcmp(buf, "stop")) | |
2513 | onerror = BLOCK_ERR_STOP_ANY; | |
2514 | else if (!strcmp(buf, "report")) | |
2515 | onerror = BLOCK_ERR_REPORT; | |
2516 | else { | |
2517 | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); | |
2518 | return -1; | |
2519 | } | |
2520 | } | |
2521 | ||
e4bcb14c TS |
2522 | /* compute bus and unit according index */ |
2523 | ||
2524 | if (index != -1) { | |
2525 | if (bus_id != 0 || unit_id != -1) { | |
2526 | fprintf(stderr, | |
2527 | "qemu: '%s' index cannot be used with bus and unit\n", str); | |
2528 | return -1; | |
2529 | } | |
2530 | if (max_devs == 0) | |
2531 | { | |
2532 | unit_id = index; | |
2533 | bus_id = 0; | |
2534 | } else { | |
2535 | unit_id = index % max_devs; | |
2536 | bus_id = index / max_devs; | |
2537 | } | |
2538 | } | |
2539 | ||
2540 | /* if user doesn't specify a unit_id, | |
2541 | * try to find the first free | |
2542 | */ | |
2543 | ||
2544 | if (unit_id == -1) { | |
2545 | unit_id = 0; | |
f60d39bc | 2546 | while (drive_get_index(type, bus_id, unit_id) != -1) { |
e4bcb14c TS |
2547 | unit_id++; |
2548 | if (max_devs && unit_id >= max_devs) { | |
2549 | unit_id -= max_devs; | |
2550 | bus_id++; | |
2551 | } | |
2552 | } | |
2553 | } | |
2554 | ||
2555 | /* check unit id */ | |
2556 | ||
2557 | if (max_devs && unit_id >= max_devs) { | |
2558 | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n", | |
2559 | str, unit_id, max_devs - 1); | |
2560 | return -1; | |
2561 | } | |
2562 | ||
2563 | /* | |
2564 | * ignore multiple definitions | |
2565 | */ | |
2566 | ||
f60d39bc | 2567 | if (drive_get_index(type, bus_id, unit_id) != -1) |
4d73cd3b | 2568 | return -2; |
e4bcb14c TS |
2569 | |
2570 | /* init */ | |
2571 | ||
f60d39bc | 2572 | if (type == IF_IDE || type == IF_SCSI) |
c8522bdf | 2573 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
e6198a70 AZ |
2574 | if (max_devs) |
2575 | snprintf(buf, sizeof(buf), "%s%i%s%i", | |
2576 | devname, bus_id, mediastr, unit_id); | |
2577 | else | |
2578 | snprintf(buf, sizeof(buf), "%s%s%i", | |
2579 | devname, mediastr, unit_id); | |
e4bcb14c | 2580 | bdrv = bdrv_new(buf); |
7d5aca9e AL |
2581 | drives_table_idx = drive_get_free_idx(); |
2582 | drives_table[drives_table_idx].bdrv = bdrv; | |
2583 | drives_table[drives_table_idx].type = type; | |
2584 | drives_table[drives_table_idx].bus = bus_id; | |
2585 | drives_table[drives_table_idx].unit = unit_id; | |
2586 | drives_table[drives_table_idx].onerror = onerror; | |
b01b1111 | 2587 | drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt; |
fa879c64 | 2588 | strncpy(drives_table[nb_drives].serial, serial, sizeof(serial)); |
e4bcb14c TS |
2589 | nb_drives++; |
2590 | ||
f60d39bc | 2591 | switch(type) { |
e4bcb14c TS |
2592 | case IF_IDE: |
2593 | case IF_SCSI: | |
62d23efa | 2594 | case IF_XEN: |
e4bcb14c TS |
2595 | switch(media) { |
2596 | case MEDIA_DISK: | |
2597 | if (cyls != 0) { | |
2598 | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); | |
2599 | bdrv_set_translation_hint(bdrv, translation); | |
2600 | } | |
2601 | break; | |
2602 | case MEDIA_CDROM: | |
2603 | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); | |
2604 | break; | |
2605 | } | |
2606 | break; | |
2607 | case IF_SD: | |
2608 | /* FIXME: This isn't really a floppy, but it's a reasonable | |
2609 | approximation. */ | |
2610 | case IF_FLOPPY: | |
2611 | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); | |
2612 | break; | |
2613 | case IF_PFLASH: | |
2614 | case IF_MTD: | |
6e02c38d | 2615 | case IF_VIRTIO: |
e4bcb14c TS |
2616 | break; |
2617 | } | |
2618 | if (!file[0]) | |
4d73cd3b | 2619 | return -2; |
33f00271 | 2620 | bdrv_flags = 0; |
9f7965c7 | 2621 | if (snapshot) { |
33f00271 | 2622 | bdrv_flags |= BDRV_O_SNAPSHOT; |
9f7965c7 AL |
2623 | cache = 2; /* always use write-back with snapshot */ |
2624 | } | |
2625 | if (cache == 0) /* no caching */ | |
2626 | bdrv_flags |= BDRV_O_NOCACHE; | |
2627 | else if (cache == 2) /* write-back */ | |
2628 | bdrv_flags |= BDRV_O_CACHE_WB; | |
4dc822d7 AL |
2629 | else if (cache == 3) /* not specified */ |
2630 | bdrv_flags |= BDRV_O_CACHE_DEF; | |
c0f4ce77 | 2631 | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) { |
e4bcb14c TS |
2632 | fprintf(stderr, "qemu: could not open disk image %s\n", |
2633 | file); | |
2634 | return -1; | |
2635 | } | |
c0f4ce77 AL |
2636 | if (bdrv_key_required(bdrv)) |
2637 | autostart = 0; | |
4d73cd3b | 2638 | return drives_table_idx; |
e4bcb14c TS |
2639 | } |
2640 | ||
268a362c AL |
2641 | static void numa_add(const char *optarg) |
2642 | { | |
2643 | char option[128]; | |
2644 | char *endptr; | |
2645 | unsigned long long value, endvalue; | |
2646 | int nodenr; | |
2647 | ||
2648 | optarg = get_opt_name(option, 128, optarg, ',') + 1; | |
2649 | if (!strcmp(option, "node")) { | |
2650 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { | |
2651 | nodenr = nb_numa_nodes; | |
2652 | } else { | |
2653 | nodenr = strtoull(option, NULL, 10); | |
2654 | } | |
2655 | ||
2656 | if (get_param_value(option, 128, "mem", optarg) == 0) { | |
2657 | node_mem[nodenr] = 0; | |
2658 | } else { | |
2659 | value = strtoull(option, &endptr, 0); | |
2660 | switch (*endptr) { | |
2661 | case 0: case 'M': case 'm': | |
2662 | value <<= 20; | |
2663 | break; | |
2664 | case 'G': case 'g': | |
2665 | value <<= 30; | |
2666 | break; | |
2667 | } | |
2668 | node_mem[nodenr] = value; | |
2669 | } | |
2670 | if (get_param_value(option, 128, "cpus", optarg) == 0) { | |
2671 | node_cpumask[nodenr] = 0; | |
2672 | } else { | |
2673 | value = strtoull(option, &endptr, 10); | |
2674 | if (value >= 64) { | |
2675 | value = 63; | |
2676 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); | |
2677 | } else { | |
2678 | if (*endptr == '-') { | |
2679 | endvalue = strtoull(endptr+1, &endptr, 10); | |
2680 | if (endvalue >= 63) { | |
2681 | endvalue = 62; | |
2682 | fprintf(stderr, | |
2683 | "only 63 CPUs in NUMA mode supported.\n"); | |
2684 | } | |
2685 | value = (1 << (endvalue + 1)) - (1 << value); | |
2686 | } else { | |
2687 | value = 1 << value; | |
2688 | } | |
2689 | } | |
2690 | node_cpumask[nodenr] = value; | |
2691 | } | |
2692 | nb_numa_nodes++; | |
2693 | } | |
2694 | return; | |
2695 | } | |
2696 | ||
a594cfbf FB |
2697 | /***********************************************************/ |
2698 | /* USB devices */ | |
2699 | ||
0d92ed30 PB |
2700 | static USBPort *used_usb_ports; |
2701 | static USBPort *free_usb_ports; | |
2702 | ||
2703 | /* ??? Maybe change this to register a hub to keep track of the topology. */ | |
2704 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, | |
2705 | usb_attachfn attach) | |
2706 | { | |
2707 | port->opaque = opaque; | |
2708 | port->index = index; | |
2709 | port->attach = attach; | |
2710 | port->next = free_usb_ports; | |
2711 | free_usb_ports = port; | |
2712 | } | |
2713 | ||
4b096fc9 AL |
2714 | int usb_device_add_dev(USBDevice *dev) |
2715 | { | |
2716 | USBPort *port; | |
2717 | ||
2718 | /* Find a USB port to add the device to. */ | |
2719 | port = free_usb_ports; | |
2720 | if (!port->next) { | |
2721 | USBDevice *hub; | |
2722 | ||
2723 | /* Create a new hub and chain it on. */ | |
2724 | free_usb_ports = NULL; | |
2725 | port->next = used_usb_ports; | |
2726 | used_usb_ports = port; | |
2727 | ||
2728 | hub = usb_hub_init(VM_USB_HUB_SIZE); | |
2729 | usb_attach(port, hub); | |
2730 | port = free_usb_ports; | |
2731 | } | |
2732 | ||
2733 | free_usb_ports = port->next; | |
2734 | port->next = used_usb_ports; | |
2735 | used_usb_ports = port; | |
2736 | usb_attach(port, dev); | |
2737 | return 0; | |
2738 | } | |
2739 | ||
bb5fc20f AL |
2740 | static void usb_msd_password_cb(void *opaque, int err) |
2741 | { | |
2742 | USBDevice *dev = opaque; | |
2743 | ||
2744 | if (!err) | |
2745 | usb_device_add_dev(dev); | |
2746 | else | |
2747 | dev->handle_destroy(dev); | |
2748 | } | |
2749 | ||
c0f4ce77 | 2750 | static int usb_device_add(const char *devname, int is_hotplug) |
a594cfbf FB |
2751 | { |
2752 | const char *p; | |
2753 | USBDevice *dev; | |
a594cfbf | 2754 | |
0d92ed30 | 2755 | if (!free_usb_ports) |
a594cfbf FB |
2756 | return -1; |
2757 | ||
2758 | if (strstart(devname, "host:", &p)) { | |
2759 | dev = usb_host_device_open(p); | |
a594cfbf FB |
2760 | } else if (!strcmp(devname, "mouse")) { |
2761 | dev = usb_mouse_init(); | |
09b26c5e | 2762 | } else if (!strcmp(devname, "tablet")) { |
47b2d338 AZ |
2763 | dev = usb_tablet_init(); |
2764 | } else if (!strcmp(devname, "keyboard")) { | |
2765 | dev = usb_keyboard_init(); | |
2e5d83bb | 2766 | } else if (strstart(devname, "disk:", &p)) { |
c0f4ce77 AL |
2767 | BlockDriverState *bs; |
2768 | ||
bb5fc20f | 2769 | dev = usb_msd_init(p); |
c0f4ce77 AL |
2770 | if (!dev) |
2771 | return -1; | |
bb5fc20f | 2772 | bs = usb_msd_get_bdrv(dev); |
c0f4ce77 AL |
2773 | if (bdrv_key_required(bs)) { |
2774 | autostart = 0; | |
bb5fc20f | 2775 | if (is_hotplug) { |
376253ec AL |
2776 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
2777 | dev); | |
bb5fc20f | 2778 | return 0; |
c0f4ce77 AL |
2779 | } |
2780 | } | |
f6d2a316 AZ |
2781 | } else if (!strcmp(devname, "wacom-tablet")) { |
2782 | dev = usb_wacom_init(); | |
a7954218 AZ |
2783 | } else if (strstart(devname, "serial:", &p)) { |
2784 | dev = usb_serial_init(p); | |
2e4d9fb1 AJ |
2785 | #ifdef CONFIG_BRLAPI |
2786 | } else if (!strcmp(devname, "braille")) { | |
2787 | dev = usb_baum_init(); | |
2788 | #endif | |
6c9f886c | 2789 | } else if (strstart(devname, "net:", &p)) { |
9ad97e65 | 2790 | int nic = nb_nics; |
6c9f886c | 2791 | |
9ad97e65 | 2792 | if (net_client_init("nic", p) < 0) |
6c9f886c | 2793 | return -1; |
9ad97e65 AZ |
2794 | nd_table[nic].model = "usb"; |
2795 | dev = usb_net_init(&nd_table[nic]); | |
dc72ac14 AZ |
2796 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
2797 | dev = usb_bt_init(devname[2] ? hci_init(p) : | |
2798 | bt_new_hci(qemu_find_bt_vlan(0))); | |
a594cfbf FB |
2799 | } else { |
2800 | return -1; | |
2801 | } | |
0d92ed30 PB |
2802 | if (!dev) |
2803 | return -1; | |
2804 | ||
4b096fc9 | 2805 | return usb_device_add_dev(dev); |
a594cfbf FB |
2806 | } |
2807 | ||
1f3870ab | 2808 | int usb_device_del_addr(int bus_num, int addr) |
a594cfbf | 2809 | { |
0d92ed30 PB |
2810 | USBPort *port; |
2811 | USBPort **lastp; | |
059809e4 | 2812 | USBDevice *dev; |
a594cfbf | 2813 | |
0d92ed30 | 2814 | if (!used_usb_ports) |
a594cfbf FB |
2815 | return -1; |
2816 | ||
a594cfbf FB |
2817 | if (bus_num != 0) |
2818 | return -1; | |
0d92ed30 PB |
2819 | |
2820 | lastp = &used_usb_ports; | |
2821 | port = used_usb_ports; | |
2822 | while (port && port->dev->addr != addr) { | |
2823 | lastp = &port->next; | |
2824 | port = port->next; | |
a594cfbf | 2825 | } |
0d92ed30 PB |
2826 | |
2827 | if (!port) | |
a594cfbf | 2828 | return -1; |
0d92ed30 | 2829 | |
059809e4 | 2830 | dev = port->dev; |
0d92ed30 PB |
2831 | *lastp = port->next; |
2832 | usb_attach(port, NULL); | |
059809e4 | 2833 | dev->handle_destroy(dev); |
0d92ed30 PB |
2834 | port->next = free_usb_ports; |
2835 | free_usb_ports = port; | |
a594cfbf FB |
2836 | return 0; |
2837 | } | |
2838 | ||
1f3870ab AL |
2839 | static int usb_device_del(const char *devname) |
2840 | { | |
2841 | int bus_num, addr; | |
2842 | const char *p; | |
2843 | ||
5d0c5750 AL |
2844 | if (strstart(devname, "host:", &p)) |
2845 | return usb_host_device_close(p); | |
2846 | ||
1f3870ab AL |
2847 | if (!used_usb_ports) |
2848 | return -1; | |
2849 | ||
2850 | p = strchr(devname, '.'); | |
2851 | if (!p) | |
2852 | return -1; | |
2853 | bus_num = strtoul(devname, NULL, 0); | |
2854 | addr = strtoul(p + 1, NULL, 0); | |
2855 | ||
2856 | return usb_device_del_addr(bus_num, addr); | |
2857 | } | |
2858 | ||
376253ec | 2859 | void do_usb_add(Monitor *mon, const char *devname) |
a594cfbf | 2860 | { |
c0f4ce77 | 2861 | usb_device_add(devname, 1); |
a594cfbf FB |
2862 | } |
2863 | ||
376253ec | 2864 | void do_usb_del(Monitor *mon, const char *devname) |
a594cfbf | 2865 | { |
4b096fc9 | 2866 | usb_device_del(devname); |
a594cfbf FB |
2867 | } |
2868 | ||
376253ec | 2869 | void usb_info(Monitor *mon) |
a594cfbf FB |
2870 | { |
2871 | USBDevice *dev; | |
0d92ed30 | 2872 | USBPort *port; |
a594cfbf FB |
2873 | const char *speed_str; |
2874 | ||
0d92ed30 | 2875 | if (!usb_enabled) { |
376253ec | 2876 | monitor_printf(mon, "USB support not enabled\n"); |
a594cfbf FB |
2877 | return; |
2878 | } | |
2879 | ||
0d92ed30 PB |
2880 | for (port = used_usb_ports; port; port = port->next) { |
2881 | dev = port->dev; | |
2882 | if (!dev) | |
2883 | continue; | |
2884 | switch(dev->speed) { | |
5fafdf24 TS |
2885 | case USB_SPEED_LOW: |
2886 | speed_str = "1.5"; | |
0d92ed30 | 2887 | break; |
5fafdf24 TS |
2888 | case USB_SPEED_FULL: |
2889 | speed_str = "12"; | |
0d92ed30 | 2890 | break; |
5fafdf24 TS |
2891 | case USB_SPEED_HIGH: |
2892 | speed_str = "480"; | |
0d92ed30 PB |
2893 | break; |
2894 | default: | |
5fafdf24 | 2895 | speed_str = "?"; |
0d92ed30 | 2896 | break; |
a594cfbf | 2897 | } |
376253ec AL |
2898 | monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n", |
2899 | 0, dev->addr, speed_str, dev->devname); | |
a594cfbf FB |
2900 | } |
2901 | } | |
2902 | ||
201a51fc AZ |
2903 | /***********************************************************/ |
2904 | /* PCMCIA/Cardbus */ | |
2905 | ||
2906 | static struct pcmcia_socket_entry_s { | |
2907 | struct pcmcia_socket_s *socket; | |
2908 | struct pcmcia_socket_entry_s *next; | |
2909 | } *pcmcia_sockets = 0; | |
2910 | ||
2911 | void pcmcia_socket_register(struct pcmcia_socket_s *socket) | |
2912 | { | |
2913 | struct pcmcia_socket_entry_s *entry; | |
2914 | ||
2915 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); | |
2916 | entry->socket = socket; | |
2917 | entry->next = pcmcia_sockets; | |
2918 | pcmcia_sockets = entry; | |
2919 | } | |
2920 | ||
2921 | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) | |
2922 | { | |
2923 | struct pcmcia_socket_entry_s *entry, **ptr; | |
2924 | ||
2925 | ptr = &pcmcia_sockets; | |
2926 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) | |
2927 | if (entry->socket == socket) { | |
2928 | *ptr = entry->next; | |
2929 | qemu_free(entry); | |
2930 | } | |
2931 | } | |
2932 | ||
376253ec | 2933 | void pcmcia_info(Monitor *mon) |
201a51fc AZ |
2934 | { |
2935 | struct pcmcia_socket_entry_s *iter; | |
376253ec | 2936 | |
201a51fc | 2937 | if (!pcmcia_sockets) |
376253ec | 2938 | monitor_printf(mon, "No PCMCIA sockets\n"); |
201a51fc AZ |
2939 | |
2940 | for (iter = pcmcia_sockets; iter; iter = iter->next) | |
376253ec AL |
2941 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
2942 | iter->socket->attached ? iter->socket->card_string : | |
2943 | "Empty"); | |
201a51fc AZ |
2944 | } |
2945 | ||
2ff89790 | 2946 | /***********************************************************/ |
3023f332 AL |
2947 | /* register display */ |
2948 | ||
7b5d76da AL |
2949 | struct DisplayAllocator default_allocator = { |
2950 | defaultallocator_create_displaysurface, | |
2951 | defaultallocator_resize_displaysurface, | |
2952 | defaultallocator_free_displaysurface | |
2953 | }; | |
2954 | ||
3023f332 AL |
2955 | void register_displaystate(DisplayState *ds) |
2956 | { | |
2957 | DisplayState **s; | |
2958 | s = &display_state; | |
2959 | while (*s != NULL) | |
2960 | s = &(*s)->next; | |
2961 | ds->next = NULL; | |
2962 | *s = ds; | |
2963 | } | |
2964 | ||
2965 | DisplayState *get_displaystate(void) | |
2966 | { | |
2967 | return display_state; | |
2968 | } | |
2969 | ||
7b5d76da AL |
2970 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
2971 | { | |
2972 | if(ds->allocator == &default_allocator) ds->allocator = da; | |
2973 | return ds->allocator; | |
2974 | } | |
2975 | ||
2ff89790 TS |
2976 | /* dumb display */ |
2977 | ||
8f391ab4 | 2978 | static void dumb_display_init(void) |
2ff89790 | 2979 | { |
8f391ab4 | 2980 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
7b5d76da AL |
2981 | ds->allocator = &default_allocator; |
2982 | ds->surface = qemu_create_displaysurface(ds, 640, 480); | |
8f391ab4 | 2983 | register_displaystate(ds); |
2ff89790 TS |
2984 | } |
2985 | ||
8a7ddc38 FB |
2986 | /***********************************************************/ |
2987 | /* I/O handling */ | |
0824d6fc | 2988 | |
c4b1fcc0 FB |
2989 | typedef struct IOHandlerRecord { |
2990 | int fd; | |
7c9d8e07 FB |
2991 | IOCanRWHandler *fd_read_poll; |
2992 | IOHandler *fd_read; | |
2993 | IOHandler *fd_write; | |
cafffd40 | 2994 | int deleted; |
c4b1fcc0 FB |
2995 | void *opaque; |
2996 | /* temporary data */ | |
2997 | struct pollfd *ufd; | |
8a7ddc38 | 2998 | struct IOHandlerRecord *next; |
c4b1fcc0 FB |
2999 | } IOHandlerRecord; |
3000 | ||
8a7ddc38 | 3001 | static IOHandlerRecord *first_io_handler; |
c4b1fcc0 | 3002 | |
7c9d8e07 FB |
3003 | /* XXX: fd_read_poll should be suppressed, but an API change is |
3004 | necessary in the character devices to suppress fd_can_read(). */ | |
5fafdf24 TS |
3005 | int qemu_set_fd_handler2(int fd, |
3006 | IOCanRWHandler *fd_read_poll, | |
3007 | IOHandler *fd_read, | |
3008 | IOHandler *fd_write, | |
7c9d8e07 | 3009 | void *opaque) |
c4b1fcc0 | 3010 | { |
7c9d8e07 | 3011 | IOHandlerRecord **pioh, *ioh; |
c4b1fcc0 | 3012 | |
7c9d8e07 FB |
3013 | if (!fd_read && !fd_write) { |
3014 | pioh = &first_io_handler; | |
3015 | for(;;) { | |
3016 | ioh = *pioh; | |
3017 | if (ioh == NULL) | |
3018 | break; | |
3019 | if (ioh->fd == fd) { | |
cafffd40 | 3020 | ioh->deleted = 1; |
7c9d8e07 FB |
3021 | break; |
3022 | } | |
3023 | pioh = &ioh->next; | |
3024 | } | |
3025 | } else { | |
3026 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { | |
3027 | if (ioh->fd == fd) | |
3028 | goto found; | |
3029 | } | |
3030 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); | |
7c9d8e07 FB |
3031 | ioh->next = first_io_handler; |
3032 | first_io_handler = ioh; | |
3033 | found: | |
3034 | ioh->fd = fd; | |
3035 | ioh->fd_read_poll = fd_read_poll; | |
3036 | ioh->fd_read = fd_read; | |
3037 | ioh->fd_write = fd_write; | |
3038 | ioh->opaque = opaque; | |
cafffd40 | 3039 | ioh->deleted = 0; |
7c9d8e07 | 3040 | } |
c4b1fcc0 FB |
3041 | return 0; |
3042 | } | |
3043 | ||
5fafdf24 TS |
3044 | int qemu_set_fd_handler(int fd, |
3045 | IOHandler *fd_read, | |
3046 | IOHandler *fd_write, | |
7c9d8e07 | 3047 | void *opaque) |
8a7ddc38 | 3048 | { |
7c9d8e07 | 3049 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
8a7ddc38 FB |
3050 | } |
3051 | ||
56f3a5d0 | 3052 | #ifdef _WIN32 |
f331110f FB |
3053 | /***********************************************************/ |
3054 | /* Polling handling */ | |
3055 | ||
3056 | typedef struct PollingEntry { | |
3057 | PollingFunc *func; | |
3058 | void *opaque; | |
3059 | struct PollingEntry *next; | |
3060 | } PollingEntry; | |
3061 | ||
3062 | static PollingEntry *first_polling_entry; | |
3063 | ||
3064 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) | |
3065 | { | |
3066 | PollingEntry **ppe, *pe; | |
3067 | pe = qemu_mallocz(sizeof(PollingEntry)); | |
f331110f FB |
3068 | pe->func = func; |
3069 | pe->opaque = opaque; | |
3070 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); | |
3071 | *ppe = pe; | |
3072 | return 0; | |
3073 | } | |
3074 | ||
3075 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) | |
3076 | { | |
3077 | PollingEntry **ppe, *pe; | |
3078 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { | |
3079 | pe = *ppe; | |
3080 | if (pe->func == func && pe->opaque == opaque) { | |
3081 | *ppe = pe->next; | |
3082 | qemu_free(pe); | |
3083 | break; | |
3084 | } | |
3085 | } | |
3086 | } | |
3087 | ||
a18e524a FB |
3088 | /***********************************************************/ |
3089 | /* Wait objects support */ | |
3090 | typedef struct WaitObjects { | |
3091 | int num; | |
3092 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; | |
3093 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; | |
3094 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; | |
3095 | } WaitObjects; | |
3096 | ||
3097 | static WaitObjects wait_objects = {0}; | |
3b46e624 | 3098 | |
a18e524a FB |
3099 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
3100 | { | |
3101 | WaitObjects *w = &wait_objects; | |
3102 | ||
3103 | if (w->num >= MAXIMUM_WAIT_OBJECTS) | |
3104 | return -1; | |
3105 | w->events[w->num] = handle; | |
3106 | w->func[w->num] = func; | |
3107 | w->opaque[w->num] = opaque; | |
3108 | w->num++; | |
3109 | return 0; | |
3110 | } | |
3111 | ||
3112 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) | |
3113 | { | |
3114 | int i, found; | |
3115 | WaitObjects *w = &wait_objects; | |
3116 | ||
3117 | found = 0; | |
3118 | for (i = 0; i < w->num; i++) { | |
3119 | if (w->events[i] == handle) | |
3120 | found = 1; | |
3121 | if (found) { | |
3122 | w->events[i] = w->events[i + 1]; | |
3123 | w->func[i] = w->func[i + 1]; | |
3124 | w->opaque[i] = w->opaque[i + 1]; | |
3b46e624 | 3125 | } |
a18e524a FB |
3126 | } |
3127 | if (found) | |
3128 | w->num--; | |
3129 | } | |
3130 | #endif | |
3131 | ||
8a7ddc38 FB |
3132 | /***********************************************************/ |
3133 | /* ram save/restore */ | |
3134 | ||
8a7ddc38 FB |
3135 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
3136 | { | |
3137 | int v; | |
3138 | ||
3139 | v = qemu_get_byte(f); | |
3140 | switch(v) { | |
3141 | case 0: | |
3142 | if (qemu_get_buffer(f, buf, len) != len) | |
3143 | return -EIO; | |
3144 | break; | |
3145 | case 1: | |
3146 | v = qemu_get_byte(f); | |
3147 | memset(buf, v, len); | |
3148 | break; | |
3149 | default: | |
3150 | return -EINVAL; | |
3151 | } | |
871d2f07 AL |
3152 | |
3153 | if (qemu_file_has_error(f)) | |
3154 | return -EIO; | |
3155 | ||
8a7ddc38 FB |
3156 | return 0; |
3157 | } | |
3158 | ||
c88676f8 FB |
3159 | static int ram_load_v1(QEMUFile *f, void *opaque) |
3160 | { | |
00f82b8a AJ |
3161 | int ret; |
3162 | ram_addr_t i; | |
c88676f8 | 3163 | |
94a6b54f | 3164 | if (qemu_get_be32(f) != last_ram_offset) |
c88676f8 | 3165 | return -EINVAL; |
94a6b54f | 3166 | for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) { |
5579c7f3 | 3167 | ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE); |
c88676f8 FB |
3168 | if (ret) |
3169 | return ret; | |
3170 | } | |
3171 | return 0; | |
3172 | } | |
3173 | ||
3174 | #define BDRV_HASH_BLOCK_SIZE 1024 | |
3175 | #define IOBUF_SIZE 4096 | |
3176 | #define RAM_CBLOCK_MAGIC 0xfabe | |
3177 | ||
c88676f8 FB |
3178 | typedef struct RamDecompressState { |
3179 | z_stream zstream; | |
3180 | QEMUFile *f; | |
3181 | uint8_t buf[IOBUF_SIZE]; | |
3182 | } RamDecompressState; | |
3183 | ||
3184 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) | |
3185 | { | |
3186 | int ret; | |
3187 | memset(s, 0, sizeof(*s)); | |
3188 | s->f = f; | |
3189 | ret = inflateInit(&s->zstream); | |
3190 | if (ret != Z_OK) | |
3191 | return -1; | |
3192 | return 0; | |
3193 | } | |
3194 | ||
3195 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) | |
3196 | { | |
3197 | int ret, clen; | |
3198 | ||
3199 | s->zstream.avail_out = len; | |
3200 | s->zstream.next_out = buf; | |
3201 | while (s->zstream.avail_out > 0) { | |
3202 | if (s->zstream.avail_in == 0) { | |
3203 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) | |
3204 | return -1; | |
3205 | clen = qemu_get_be16(s->f); | |
3206 | if (clen > IOBUF_SIZE) | |
3207 | return -1; | |
3208 | qemu_get_buffer(s->f, s->buf, clen); | |
3209 | s->zstream.avail_in = clen; | |
3210 | s->zstream.next_in = s->buf; | |
3211 | } | |
3212 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); | |
3213 | if (ret != Z_OK && ret != Z_STREAM_END) { | |
3214 | return -1; | |
3215 | } | |
3216 | } | |
3217 | return 0; | |
3218 | } | |
3219 | ||
3220 | static void ram_decompress_close(RamDecompressState *s) | |
3221 | { | |
3222 | inflateEnd(&s->zstream); | |
3223 | } | |
3224 | ||
475e4277 AL |
3225 | #define RAM_SAVE_FLAG_FULL 0x01 |
3226 | #define RAM_SAVE_FLAG_COMPRESS 0x02 | |
3227 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
3228 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
3229 | #define RAM_SAVE_FLAG_EOS 0x10 | |
3230 | ||
3231 | static int is_dup_page(uint8_t *page, uint8_t ch) | |
8a7ddc38 | 3232 | { |
475e4277 AL |
3233 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
3234 | uint32_t *array = (uint32_t *)page; | |
3235 | int i; | |
3b46e624 | 3236 | |
475e4277 AL |
3237 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
3238 | if (array[i] != val) | |
3239 | return 0; | |
3240 | } | |
3241 | ||
3242 | return 1; | |
3243 | } | |
3244 | ||
3245 | static int ram_save_block(QEMUFile *f) | |
3246 | { | |
3247 | static ram_addr_t current_addr = 0; | |
3248 | ram_addr_t saved_addr = current_addr; | |
3249 | ram_addr_t addr = 0; | |
3250 | int found = 0; | |
3251 | ||
94a6b54f | 3252 | while (addr < last_ram_offset) { |
475e4277 | 3253 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
5579c7f3 | 3254 | uint8_t *p; |
475e4277 AL |
3255 | |
3256 | cpu_physical_memory_reset_dirty(current_addr, | |
3257 | current_addr + TARGET_PAGE_SIZE, | |
3258 | MIGRATION_DIRTY_FLAG); | |
3259 | ||
5579c7f3 | 3260 | p = qemu_get_ram_ptr(current_addr); |
475e4277 | 3261 | |
5579c7f3 | 3262 | if (is_dup_page(p, *p)) { |
475e4277 | 3263 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
5579c7f3 | 3264 | qemu_put_byte(f, *p); |
475e4277 AL |
3265 | } else { |
3266 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); | |
5579c7f3 | 3267 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
c88676f8 | 3268 | } |
475e4277 AL |
3269 | |
3270 | found = 1; | |
3271 | break; | |
c88676f8 | 3272 | } |
475e4277 | 3273 | addr += TARGET_PAGE_SIZE; |
94a6b54f | 3274 | current_addr = (saved_addr + addr) % last_ram_offset; |
8a7ddc38 | 3275 | } |
475e4277 AL |
3276 | |
3277 | return found; | |
8a7ddc38 FB |
3278 | } |
3279 | ||
475e4277 AL |
3280 | static ram_addr_t ram_save_threshold = 10; |
3281 | ||
3282 | static ram_addr_t ram_save_remaining(void) | |
3283 | { | |
3284 | ram_addr_t addr; | |
3285 | ram_addr_t count = 0; | |
3286 | ||
94a6b54f | 3287 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
3288 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
3289 | count++; | |
3290 | } | |
3291 | ||
3292 | return count; | |
3293 | } | |
3294 | ||
3295 | static int ram_save_live(QEMUFile *f, int stage, void *opaque) | |
3296 | { | |
3297 | ram_addr_t addr; | |
3298 | ||
3299 | if (stage == 1) { | |
3300 | /* Make sure all dirty bits are set */ | |
94a6b54f | 3301 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
3302 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
3303 | cpu_physical_memory_set_dirty(addr); | |
3304 | } | |
3305 | ||
3306 | /* Enable dirty memory tracking */ | |
3307 | cpu_physical_memory_set_dirty_tracking(1); | |
3308 | ||
94a6b54f | 3309 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
475e4277 AL |
3310 | } |
3311 | ||
3312 | while (!qemu_file_rate_limit(f)) { | |
3313 | int ret; | |
3314 | ||
3315 | ret = ram_save_block(f); | |
3316 | if (ret == 0) /* no more blocks */ | |
3317 | break; | |
3318 | } | |
3319 | ||
3320 | /* try transferring iterative blocks of memory */ | |
3321 | ||
3322 | if (stage == 3) { | |
475e4277 AL |
3323 | |
3324 | /* flush all remaining blocks regardless of rate limiting */ | |
3325 | while (ram_save_block(f) != 0); | |
8215e914 | 3326 | cpu_physical_memory_set_dirty_tracking(0); |
475e4277 AL |
3327 | } |
3328 | ||
3329 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
3330 | ||
3331 | return (stage == 2) && (ram_save_remaining() < ram_save_threshold); | |
3332 | } | |
3333 | ||
3334 | static int ram_load_dead(QEMUFile *f, void *opaque) | |
8a7ddc38 | 3335 | { |
c88676f8 FB |
3336 | RamDecompressState s1, *s = &s1; |
3337 | uint8_t buf[10]; | |
00f82b8a | 3338 | ram_addr_t i; |
8a7ddc38 | 3339 | |
c88676f8 FB |
3340 | if (ram_decompress_open(s, f) < 0) |
3341 | return -EINVAL; | |
94a6b54f | 3342 | for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) { |
c88676f8 FB |
3343 | if (ram_decompress_buf(s, buf, 1) < 0) { |
3344 | fprintf(stderr, "Error while reading ram block header\n"); | |
3345 | goto error; | |
3346 | } | |
3347 | if (buf[0] == 0) { | |
5579c7f3 PB |
3348 | if (ram_decompress_buf(s, qemu_get_ram_ptr(i), |
3349 | BDRV_HASH_BLOCK_SIZE) < 0) { | |
00f82b8a | 3350 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i); |
c88676f8 FB |
3351 | goto error; |
3352 | } | |
475e4277 | 3353 | } else { |
c88676f8 FB |
3354 | error: |
3355 | printf("Error block header\n"); | |
3356 | return -EINVAL; | |
3357 | } | |
8a7ddc38 | 3358 | } |
c88676f8 | 3359 | ram_decompress_close(s); |
475e4277 AL |
3360 | |
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | static int ram_load(QEMUFile *f, void *opaque, int version_id) | |
3365 | { | |
3366 | ram_addr_t addr; | |
3367 | int flags; | |
3368 | ||
3369 | if (version_id == 1) | |
3370 | return ram_load_v1(f, opaque); | |
3371 | ||
3372 | if (version_id == 2) { | |
94a6b54f | 3373 | if (qemu_get_be32(f) != last_ram_offset) |
475e4277 AL |
3374 | return -EINVAL; |
3375 | return ram_load_dead(f, opaque); | |
3376 | } | |
3377 | ||
3378 | if (version_id != 3) | |
3379 | return -EINVAL; | |
3380 | ||
3381 | do { | |
3382 | addr = qemu_get_be64(f); | |
3383 | ||
3384 | flags = addr & ~TARGET_PAGE_MASK; | |
3385 | addr &= TARGET_PAGE_MASK; | |
3386 | ||
3387 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { | |
94a6b54f | 3388 | if (addr != last_ram_offset) |
475e4277 AL |
3389 | return -EINVAL; |
3390 | } | |
3391 | ||
3392 | if (flags & RAM_SAVE_FLAG_FULL) { | |
3393 | if (ram_load_dead(f, opaque) < 0) | |
3394 | return -EINVAL; | |
3395 | } | |
3396 | ||
3397 | if (flags & RAM_SAVE_FLAG_COMPRESS) { | |
3398 | uint8_t ch = qemu_get_byte(f); | |
5579c7f3 | 3399 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
475e4277 | 3400 | } else if (flags & RAM_SAVE_FLAG_PAGE) |
5579c7f3 | 3401 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
475e4277 AL |
3402 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
3403 | ||
8a7ddc38 FB |
3404 | return 0; |
3405 | } | |
3406 | ||
9e472e10 AL |
3407 | void qemu_service_io(void) |
3408 | { | |
3409 | CPUState *env = cpu_single_env; | |
3410 | if (env) { | |
3098dba0 | 3411 | cpu_exit(env); |
640f42e4 | 3412 | #ifdef CONFIG_KQEMU |
9e472e10 AL |
3413 | if (env->kqemu_enabled) { |
3414 | kqemu_cpu_interrupt(env); | |
3415 | } | |
3416 | #endif | |
3417 | } | |
3418 | } | |
3419 | ||
83f64091 FB |
3420 | /***********************************************************/ |
3421 | /* bottom halves (can be seen as timers which expire ASAP) */ | |
3422 | ||
3423 | struct QEMUBH { | |
3424 | QEMUBHFunc *cb; | |
3425 | void *opaque; | |
3426 | int scheduled; | |
1b435b10 AL |
3427 | int idle; |
3428 | int deleted; | |
83f64091 FB |
3429 | QEMUBH *next; |
3430 | }; | |
3431 | ||
3432 | static QEMUBH *first_bh = NULL; | |
3433 | ||
3434 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) | |
3435 | { | |
3436 | QEMUBH *bh; | |
3437 | bh = qemu_mallocz(sizeof(QEMUBH)); | |
83f64091 FB |
3438 | bh->cb = cb; |
3439 | bh->opaque = opaque; | |
1b435b10 AL |
3440 | bh->next = first_bh; |
3441 | first_bh = bh; | |
83f64091 FB |
3442 | return bh; |
3443 | } | |
3444 | ||
6eb5733a | 3445 | int qemu_bh_poll(void) |
83f64091 | 3446 | { |
1b435b10 | 3447 | QEMUBH *bh, **bhp; |
6eb5733a | 3448 | int ret; |
83f64091 | 3449 | |
6eb5733a | 3450 | ret = 0; |
1b435b10 AL |
3451 | for (bh = first_bh; bh; bh = bh->next) { |
3452 | if (!bh->deleted && bh->scheduled) { | |
3453 | bh->scheduled = 0; | |
3454 | if (!bh->idle) | |
3455 | ret = 1; | |
3456 | bh->idle = 0; | |
3457 | bh->cb(bh->opaque); | |
3458 | } | |
83f64091 | 3459 | } |
1b435b10 AL |
3460 | |
3461 | /* remove deleted bhs */ | |
3462 | bhp = &first_bh; | |
3463 | while (*bhp) { | |
3464 | bh = *bhp; | |
3465 | if (bh->deleted) { | |
3466 | *bhp = bh->next; | |
3467 | qemu_free(bh); | |
3468 | } else | |
3469 | bhp = &bh->next; | |
3470 | } | |
3471 | ||
6eb5733a | 3472 | return ret; |
83f64091 FB |
3473 | } |
3474 | ||
1b435b10 AL |
3475 | void qemu_bh_schedule_idle(QEMUBH *bh) |
3476 | { | |
3477 | if (bh->scheduled) | |
3478 | return; | |
3479 | bh->scheduled = 1; | |
3480 | bh->idle = 1; | |
3481 | } | |
3482 | ||
83f64091 FB |
3483 | void qemu_bh_schedule(QEMUBH *bh) |
3484 | { | |
3485 | CPUState *env = cpu_single_env; | |
3486 | if (bh->scheduled) | |
3487 | return; | |
3488 | bh->scheduled = 1; | |
1b435b10 | 3489 | bh->idle = 0; |
83f64091 FB |
3490 | /* stop the currently executing CPU to execute the BH ASAP */ |
3491 | if (env) { | |
3098dba0 | 3492 | cpu_exit(env); |
83f64091 FB |
3493 | } |
3494 | } | |
3495 | ||
3496 | void qemu_bh_cancel(QEMUBH *bh) | |
3497 | { | |
1b435b10 | 3498 | bh->scheduled = 0; |
83f64091 FB |
3499 | } |
3500 | ||
3501 | void qemu_bh_delete(QEMUBH *bh) | |
3502 | { | |
1b435b10 AL |
3503 | bh->scheduled = 0; |
3504 | bh->deleted = 1; | |
83f64091 FB |
3505 | } |
3506 | ||
56f3a5d0 AL |
3507 | static void qemu_bh_update_timeout(int *timeout) |
3508 | { | |
3509 | QEMUBH *bh; | |
3510 | ||
3511 | for (bh = first_bh; bh; bh = bh->next) { | |
3512 | if (!bh->deleted && bh->scheduled) { | |
3513 | if (bh->idle) { | |
3514 | /* idle bottom halves will be polled at least | |
3515 | * every 10ms */ | |
3516 | *timeout = MIN(10, *timeout); | |
3517 | } else { | |
3518 | /* non-idle bottom halves will be executed | |
3519 | * immediately */ | |
3520 | *timeout = 0; | |
3521 | break; | |
3522 | } | |
3523 | } | |
3524 | } | |
3525 | } | |
3526 | ||
cc1daa40 FB |
3527 | /***********************************************************/ |
3528 | /* machine registration */ | |
3529 | ||
bdaf78e0 | 3530 | static QEMUMachine *first_machine = NULL; |
6f338c34 | 3531 | QEMUMachine *current_machine = NULL; |
cc1daa40 FB |
3532 | |
3533 | int qemu_register_machine(QEMUMachine *m) | |
3534 | { | |
3535 | QEMUMachine **pm; | |
3536 | pm = &first_machine; | |
3537 | while (*pm != NULL) | |
3538 | pm = &(*pm)->next; | |
3539 | m->next = NULL; | |
3540 | *pm = m; | |
3541 | return 0; | |
3542 | } | |
3543 | ||
9596ebb7 | 3544 | static QEMUMachine *find_machine(const char *name) |
cc1daa40 FB |
3545 | { |
3546 | QEMUMachine *m; | |
3547 | ||
3548 | for(m = first_machine; m != NULL; m = m->next) { | |
3549 | if (!strcmp(m->name, name)) | |
3550 | return m; | |
3551 | } | |
3552 | return NULL; | |
3553 | } | |
3554 | ||
8a7ddc38 FB |
3555 | /***********************************************************/ |
3556 | /* main execution loop */ | |
3557 | ||
9596ebb7 | 3558 | static void gui_update(void *opaque) |
8a7ddc38 | 3559 | { |
7d957bd8 | 3560 | uint64_t interval = GUI_REFRESH_INTERVAL; |
740733bb | 3561 | DisplayState *ds = opaque; |
7d957bd8 AL |
3562 | DisplayChangeListener *dcl = ds->listeners; |
3563 | ||
3564 | dpy_refresh(ds); | |
3565 | ||
3566 | while (dcl != NULL) { | |
3567 | if (dcl->gui_timer_interval && | |
3568 | dcl->gui_timer_interval < interval) | |
3569 | interval = dcl->gui_timer_interval; | |
3570 | dcl = dcl->next; | |
3571 | } | |
3572 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); | |
8a7ddc38 FB |
3573 | } |
3574 | ||
9043b62d BS |
3575 | static void nographic_update(void *opaque) |
3576 | { | |
3577 | uint64_t interval = GUI_REFRESH_INTERVAL; | |
3578 | ||
3579 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); | |
3580 | } | |
3581 | ||
0bd48850 FB |
3582 | struct vm_change_state_entry { |
3583 | VMChangeStateHandler *cb; | |
3584 | void *opaque; | |
3585 | LIST_ENTRY (vm_change_state_entry) entries; | |
3586 | }; | |
3587 | ||
3588 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; | |
3589 | ||
3590 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, | |
3591 | void *opaque) | |
3592 | { | |
3593 | VMChangeStateEntry *e; | |
3594 | ||
3595 | e = qemu_mallocz(sizeof (*e)); | |
0bd48850 FB |
3596 | |
3597 | e->cb = cb; | |
3598 | e->opaque = opaque; | |
3599 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); | |
3600 | return e; | |
3601 | } | |
3602 | ||
3603 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) | |
3604 | { | |
3605 | LIST_REMOVE (e, entries); | |
3606 | qemu_free (e); | |
3607 | } | |
3608 | ||
9781e040 | 3609 | static void vm_state_notify(int running, int reason) |
0bd48850 FB |
3610 | { |
3611 | VMChangeStateEntry *e; | |
3612 | ||
3613 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { | |
9781e040 | 3614 | e->cb(e->opaque, running, reason); |
0bd48850 FB |
3615 | } |
3616 | } | |
3617 | ||
8a7ddc38 FB |
3618 | void vm_start(void) |
3619 | { | |
3620 | if (!vm_running) { | |
3621 | cpu_enable_ticks(); | |
3622 | vm_running = 1; | |
9781e040 | 3623 | vm_state_notify(1, 0); |
efe75411 | 3624 | qemu_rearm_alarm_timer(alarm_timer); |
8a7ddc38 FB |
3625 | } |
3626 | } | |
3627 | ||
5fafdf24 | 3628 | void vm_stop(int reason) |
8a7ddc38 FB |
3629 | { |
3630 | if (vm_running) { | |
3631 | cpu_disable_ticks(); | |
3632 | vm_running = 0; | |
9781e040 | 3633 | vm_state_notify(0, reason); |
8a7ddc38 FB |
3634 | } |
3635 | } | |
3636 | ||
bb0c6722 FB |
3637 | /* reset/shutdown handler */ |
3638 | ||
3639 | typedef struct QEMUResetEntry { | |
3640 | QEMUResetHandler *func; | |
3641 | void *opaque; | |
3642 | struct QEMUResetEntry *next; | |
3643 | } QEMUResetEntry; | |
3644 | ||
3645 | static QEMUResetEntry *first_reset_entry; | |
3646 | static int reset_requested; | |
3647 | static int shutdown_requested; | |
3475187d | 3648 | static int powerdown_requested; |
bb0c6722 | 3649 | |
cf7a2fe2 AJ |
3650 | int qemu_shutdown_requested(void) |
3651 | { | |
3652 | int r = shutdown_requested; | |
3653 | shutdown_requested = 0; | |
3654 | return r; | |
3655 | } | |
3656 | ||
3657 | int qemu_reset_requested(void) | |
3658 | { | |
3659 | int r = reset_requested; | |
3660 | reset_requested = 0; | |
3661 | return r; | |
3662 | } | |
3663 | ||
3664 | int qemu_powerdown_requested(void) | |
3665 | { | |
3666 | int r = powerdown_requested; | |
3667 | powerdown_requested = 0; | |
3668 | return r; | |
3669 | } | |
3670 | ||
bb0c6722 FB |
3671 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
3672 | { | |
3673 | QEMUResetEntry **pre, *re; | |
3674 | ||
3675 | pre = &first_reset_entry; | |
3676 | while (*pre != NULL) | |
3677 | pre = &(*pre)->next; | |
3678 | re = qemu_mallocz(sizeof(QEMUResetEntry)); | |
3679 | re->func = func; | |
3680 | re->opaque = opaque; | |
3681 | re->next = NULL; | |
3682 | *pre = re; | |
3683 | } | |
3684 | ||
cf7a2fe2 | 3685 | void qemu_system_reset(void) |
bb0c6722 FB |
3686 | { |
3687 | QEMUResetEntry *re; | |
3688 | ||
3689 | /* reset all devices */ | |
3690 | for(re = first_reset_entry; re != NULL; re = re->next) { | |
3691 | re->func(re->opaque); | |
3692 | } | |
29203dcf AL |
3693 | if (kvm_enabled()) |
3694 | kvm_sync_vcpus(); | |
bb0c6722 FB |
3695 | } |
3696 | ||
3697 | void qemu_system_reset_request(void) | |
3698 | { | |
d1beab82 FB |
3699 | if (no_reboot) { |
3700 | shutdown_requested = 1; | |
3701 | } else { | |
3702 | reset_requested = 1; | |
3703 | } | |
6a00d601 | 3704 | if (cpu_single_env) |
3098dba0 | 3705 | cpu_exit(cpu_single_env); |
bb0c6722 FB |
3706 | } |
3707 | ||
3708 | void qemu_system_shutdown_request(void) | |
3709 | { | |
3710 | shutdown_requested = 1; | |
6a00d601 | 3711 | if (cpu_single_env) |
3098dba0 | 3712 | cpu_exit(cpu_single_env); |
bb0c6722 FB |
3713 | } |
3714 | ||
3475187d FB |
3715 | void qemu_system_powerdown_request(void) |
3716 | { | |
3717 | powerdown_requested = 1; | |
6a00d601 | 3718 | if (cpu_single_env) |
3098dba0 | 3719 | cpu_exit(cpu_single_env); |
bb0c6722 FB |
3720 | } |
3721 | ||
877cf882 | 3722 | #ifdef _WIN32 |
69d6451c | 3723 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
3724 | { |
3725 | int ret, ret2, i; | |
f331110f FB |
3726 | PollingEntry *pe; |
3727 | ||
c4b1fcc0 | 3728 | |
f331110f FB |
3729 | /* XXX: need to suppress polling by better using win32 events */ |
3730 | ret = 0; | |
3731 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { | |
3732 | ret |= pe->func(pe->opaque); | |
3733 | } | |
e6b1e558 | 3734 | if (ret == 0) { |
a18e524a FB |
3735 | int err; |
3736 | WaitObjects *w = &wait_objects; | |
3b46e624 | 3737 | |
56f3a5d0 | 3738 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
a18e524a FB |
3739 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
3740 | if (w->func[ret - WAIT_OBJECT_0]) | |
3741 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); | |
3b46e624 | 3742 | |
5fafdf24 | 3743 | /* Check for additional signaled events */ |
e6b1e558 | 3744 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
3b46e624 | 3745 | |
e6b1e558 TS |
3746 | /* Check if event is signaled */ |
3747 | ret2 = WaitForSingleObject(w->events[i], 0); | |
3748 | if(ret2 == WAIT_OBJECT_0) { | |
3749 | if (w->func[i]) | |
3750 | w->func[i](w->opaque[i]); | |
3751 | } else if (ret2 == WAIT_TIMEOUT) { | |
3752 | } else { | |
3753 | err = GetLastError(); | |
3754 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); | |
3b46e624 TS |
3755 | } |
3756 | } | |
a18e524a FB |
3757 | } else if (ret == WAIT_TIMEOUT) { |
3758 | } else { | |
3759 | err = GetLastError(); | |
e6b1e558 | 3760 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
a18e524a | 3761 | } |
f331110f | 3762 | } |
56f3a5d0 AL |
3763 | |
3764 | *timeout = 0; | |
3765 | } | |
3766 | #else | |
69d6451c | 3767 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
3768 | { |
3769 | } | |
fd1dff4b | 3770 | #endif |
56f3a5d0 AL |
3771 | |
3772 | void main_loop_wait(int timeout) | |
3773 | { | |
3774 | IOHandlerRecord *ioh; | |
3775 | fd_set rfds, wfds, xfds; | |
3776 | int ret, nfds; | |
3777 | struct timeval tv; | |
3778 | ||
3779 | qemu_bh_update_timeout(&timeout); | |
3780 | ||
3781 | host_main_loop_wait(&timeout); | |
3782 | ||
fd1dff4b FB |
3783 | /* poll any events */ |
3784 | /* XXX: separate device handlers from system ones */ | |
6abfbd79 | 3785 | nfds = -1; |
fd1dff4b FB |
3786 | FD_ZERO(&rfds); |
3787 | FD_ZERO(&wfds); | |
e035649e | 3788 | FD_ZERO(&xfds); |
fd1dff4b | 3789 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
cafffd40 TS |
3790 | if (ioh->deleted) |
3791 | continue; | |
fd1dff4b FB |
3792 | if (ioh->fd_read && |
3793 | (!ioh->fd_read_poll || | |
3794 | ioh->fd_read_poll(ioh->opaque) != 0)) { | |
3795 | FD_SET(ioh->fd, &rfds); | |
3796 | if (ioh->fd > nfds) | |
3797 | nfds = ioh->fd; | |
3798 | } | |
3799 | if (ioh->fd_write) { | |
3800 | FD_SET(ioh->fd, &wfds); | |
3801 | if (ioh->fd > nfds) | |
3802 | nfds = ioh->fd; | |
3803 | } | |
3804 | } | |
3b46e624 | 3805 | |
56f3a5d0 AL |
3806 | tv.tv_sec = timeout / 1000; |
3807 | tv.tv_usec = (timeout % 1000) * 1000; | |
3808 | ||
e035649e | 3809 | #if defined(CONFIG_SLIRP) |
63a01ef8 | 3810 | if (slirp_is_inited()) { |
e035649e FB |
3811 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
3812 | } | |
3813 | #endif | |
3814 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); | |
fd1dff4b | 3815 | if (ret > 0) { |
cafffd40 TS |
3816 | IOHandlerRecord **pioh; |
3817 | ||
3818 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { | |
6ab43fdc | 3819 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
fd1dff4b | 3820 | ioh->fd_read(ioh->opaque); |
7c9d8e07 | 3821 | } |
6ab43fdc | 3822 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
fd1dff4b | 3823 | ioh->fd_write(ioh->opaque); |
c4b1fcc0 | 3824 | } |
b4608c04 | 3825 | } |
cafffd40 TS |
3826 | |
3827 | /* remove deleted IO handlers */ | |
3828 | pioh = &first_io_handler; | |
3829 | while (*pioh) { | |
3830 | ioh = *pioh; | |
3831 | if (ioh->deleted) { | |
3832 | *pioh = ioh->next; | |
3833 | qemu_free(ioh); | |
5fafdf24 | 3834 | } else |
cafffd40 TS |
3835 | pioh = &ioh->next; |
3836 | } | |
fd1dff4b | 3837 | } |
c20709aa | 3838 | #if defined(CONFIG_SLIRP) |
63a01ef8 | 3839 | if (slirp_is_inited()) { |
e035649e FB |
3840 | if (ret < 0) { |
3841 | FD_ZERO(&rfds); | |
3842 | FD_ZERO(&wfds); | |
3843 | FD_ZERO(&xfds); | |
c20709aa | 3844 | } |
e035649e | 3845 | slirp_select_poll(&rfds, &wfds, &xfds); |
fd1dff4b | 3846 | } |
e035649e | 3847 | #endif |
b4608c04 | 3848 | |
357c692c AL |
3849 | /* vm time timers */ |
3850 | if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) | |
3851 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], | |
3852 | qemu_get_clock(vm_clock)); | |
3853 | ||
3854 | /* real time timers */ | |
3855 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], | |
3856 | qemu_get_clock(rt_clock)); | |
3857 | ||
423f0742 PB |
3858 | /* Check bottom-halves last in case any of the earlier events triggered |
3859 | them. */ | |
3860 | qemu_bh_poll(); | |
3b46e624 | 3861 | |
5905b2e5 FB |
3862 | } |
3863 | ||
9596ebb7 | 3864 | static int main_loop(void) |
5905b2e5 FB |
3865 | { |
3866 | int ret, timeout; | |
89bfc105 FB |
3867 | #ifdef CONFIG_PROFILER |
3868 | int64_t ti; | |
3869 | #endif | |
6a00d601 | 3870 | CPUState *env; |
5905b2e5 | 3871 | |
6a00d601 | 3872 | cur_cpu = first_cpu; |
ee5605e5 | 3873 | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
5905b2e5 FB |
3874 | for(;;) { |
3875 | if (vm_running) { | |
15a76449 | 3876 | |
15a76449 FB |
3877 | for(;;) { |
3878 | /* get next cpu */ | |
ee5605e5 | 3879 | env = next_cpu; |
89bfc105 FB |
3880 | #ifdef CONFIG_PROFILER |
3881 | ti = profile_getclock(); | |
3882 | #endif | |
2e70f6ef PB |
3883 | if (use_icount) { |
3884 | int64_t count; | |
3885 | int decr; | |
3886 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
3887 | env->icount_decr.u16.low = 0; | |
3888 | env->icount_extra = 0; | |
3889 | count = qemu_next_deadline(); | |
3890 | count = (count + (1 << icount_time_shift) - 1) | |
3891 | >> icount_time_shift; | |
3892 | qemu_icount += count; | |
3893 | decr = (count > 0xffff) ? 0xffff : count; | |
3894 | count -= decr; | |
3895 | env->icount_decr.u16.low = decr; | |
3896 | env->icount_extra = count; | |
3897 | } | |
6a00d601 | 3898 | ret = cpu_exec(env); |
89bfc105 FB |
3899 | #ifdef CONFIG_PROFILER |
3900 | qemu_time += profile_getclock() - ti; | |
3901 | #endif | |
2e70f6ef PB |
3902 | if (use_icount) { |
3903 | /* Fold pending instructions back into the | |
3904 | instruction counter, and clear the interrupt flag. */ | |
3905 | qemu_icount -= (env->icount_decr.u16.low | |
3906 | + env->icount_extra); | |
3907 | env->icount_decr.u32 = 0; | |
3908 | env->icount_extra = 0; | |
3909 | } | |
ee5605e5 | 3910 | next_cpu = env->next_cpu ?: first_cpu; |
95b01009 | 3911 | if (event_pending && likely(ret != EXCP_DEBUG)) { |
ee5605e5 AZ |
3912 | ret = EXCP_INTERRUPT; |
3913 | event_pending = 0; | |
3914 | break; | |
3915 | } | |
bd967e05 PB |
3916 | if (ret == EXCP_HLT) { |
3917 | /* Give the next CPU a chance to run. */ | |
3918 | cur_cpu = env; | |
3919 | continue; | |
3920 | } | |
15a76449 FB |
3921 | if (ret != EXCP_HALTED) |
3922 | break; | |
3923 | /* all CPUs are halted ? */ | |
bd967e05 | 3924 | if (env == cur_cpu) |
15a76449 | 3925 | break; |
15a76449 FB |
3926 | } |
3927 | cur_cpu = env; | |
3928 | ||
5905b2e5 | 3929 | if (shutdown_requested) { |
3475187d | 3930 | ret = EXCP_INTERRUPT; |
b2f76161 AJ |
3931 | if (no_shutdown) { |
3932 | vm_stop(0); | |
3933 | no_shutdown = 0; | |
3934 | } | |
3935 | else | |
3936 | break; | |
5905b2e5 FB |
3937 | } |
3938 | if (reset_requested) { | |
3939 | reset_requested = 0; | |
3940 | qemu_system_reset(); | |
3475187d FB |
3941 | ret = EXCP_INTERRUPT; |
3942 | } | |
3943 | if (powerdown_requested) { | |
3944 | powerdown_requested = 0; | |
3945 | qemu_system_powerdown(); | |
3946 | ret = EXCP_INTERRUPT; | |
5905b2e5 | 3947 | } |
95b01009 | 3948 | if (unlikely(ret == EXCP_DEBUG)) { |
880a7578 | 3949 | gdb_set_stop_cpu(cur_cpu); |
5905b2e5 FB |
3950 | vm_stop(EXCP_DEBUG); |
3951 | } | |
bd967e05 | 3952 | /* If all cpus are halted then wait until the next IRQ */ |
5905b2e5 | 3953 | /* XXX: use timeout computed from timers */ |
2e70f6ef PB |
3954 | if (ret == EXCP_HALTED) { |
3955 | if (use_icount) { | |
3956 | int64_t add; | |
3957 | int64_t delta; | |
3958 | /* Advance virtual time to the next event. */ | |
3959 | if (use_icount == 1) { | |
3960 | /* When not using an adaptive execution frequency | |
3961 | we tend to get badly out of sync with real time, | |
bf20dc07 | 3962 | so just delay for a reasonable amount of time. */ |
2e70f6ef PB |
3963 | delta = 0; |
3964 | } else { | |
3965 | delta = cpu_get_icount() - cpu_get_clock(); | |
3966 | } | |
3967 | if (delta > 0) { | |
3968 | /* If virtual time is ahead of real time then just | |
3969 | wait for IO. */ | |
3970 | timeout = (delta / 1000000) + 1; | |
3971 | } else { | |
3972 | /* Wait for either IO to occur or the next | |
3973 | timer event. */ | |
3974 | add = qemu_next_deadline(); | |
3975 | /* We advance the timer before checking for IO. | |
3976 | Limit the amount we advance so that early IO | |
3977 | activity won't get the guest too far ahead. */ | |
3978 | if (add > 10000000) | |
3979 | add = 10000000; | |
3980 | delta += add; | |
3981 | add = (add + (1 << icount_time_shift) - 1) | |
3982 | >> icount_time_shift; | |
3983 | qemu_icount += add; | |
3984 | timeout = delta / 1000000; | |
3985 | if (timeout < 0) | |
3986 | timeout = 0; | |
3987 | } | |
3988 | } else { | |
0a1af395 | 3989 | timeout = 5000; |
2e70f6ef PB |
3990 | } |
3991 | } else { | |
5905b2e5 | 3992 | timeout = 0; |
2e70f6ef | 3993 | } |
5905b2e5 | 3994 | } else { |
98448f58 BS |
3995 | if (shutdown_requested) { |
3996 | ret = EXCP_INTERRUPT; | |
5b08fc10 | 3997 | break; |
98448f58 | 3998 | } |
0a1af395 | 3999 | timeout = 5000; |
5905b2e5 | 4000 | } |
89bfc105 FB |
4001 | #ifdef CONFIG_PROFILER |
4002 | ti = profile_getclock(); | |
4003 | #endif | |
5905b2e5 | 4004 | main_loop_wait(timeout); |
89bfc105 FB |
4005 | #ifdef CONFIG_PROFILER |
4006 | dev_time += profile_getclock() - ti; | |
4007 | #endif | |
b4608c04 | 4008 | } |
34865134 FB |
4009 | cpu_disable_ticks(); |
4010 | return ret; | |
b4608c04 FB |
4011 | } |
4012 | ||
9bd7e6d9 PB |
4013 | static void version(void) |
4014 | { | |
4a19f1ec | 4015 | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
9bd7e6d9 PB |
4016 | } |
4017 | ||
15f82208 | 4018 | static void help(int exitcode) |
0824d6fc | 4019 | { |
9bd7e6d9 PB |
4020 | version(); |
4021 | printf("usage: %s [options] [disk_image]\n" | |
0824d6fc | 4022 | "\n" |
a20dd508 | 4023 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" |
fc01f7e7 | 4024 | "\n" |
5824d651 BS |
4025 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4026 | opt_help | |
4027 | #define DEFHEADING(text) stringify(text) "\n" | |
4028 | #include "qemu-options.h" | |
4029 | #undef DEF | |
4030 | #undef DEFHEADING | |
4031 | #undef GEN_DOCS | |
0824d6fc | 4032 | "\n" |
82c643ff | 4033 | "During emulation, the following keys are useful:\n" |
032a8c9e FB |
4034 | "ctrl-alt-f toggle full screen\n" |
4035 | "ctrl-alt-n switch to virtual console 'n'\n" | |
4036 | "ctrl-alt toggle mouse and keyboard grab\n" | |
82c643ff FB |
4037 | "\n" |
4038 | "When using -nographic, press 'ctrl-a h' to get some help.\n" | |
4039 | , | |
0db63474 | 4040 | "qemu", |
a00bad7e | 4041 | DEFAULT_RAM_SIZE, |
7c9d8e07 | 4042 | #ifndef _WIN32 |
a00bad7e | 4043 | DEFAULT_NETWORK_SCRIPT, |
b46a8906 | 4044 | DEFAULT_NETWORK_DOWN_SCRIPT, |
7c9d8e07 | 4045 | #endif |
6e44ba7f | 4046 | DEFAULT_GDBSTUB_PORT, |
bce61846 | 4047 | "/tmp/qemu.log"); |
15f82208 | 4048 | exit(exitcode); |
0824d6fc FB |
4049 | } |
4050 | ||
cd6f1169 FB |
4051 | #define HAS_ARG 0x0001 |
4052 | ||
4053 | enum { | |
5824d651 BS |
4054 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4055 | opt_enum, | |
4056 | #define DEFHEADING(text) | |
4057 | #include "qemu-options.h" | |
4058 | #undef DEF | |
4059 | #undef DEFHEADING | |
4060 | #undef GEN_DOCS | |
cd6f1169 FB |
4061 | }; |
4062 | ||
4063 | typedef struct QEMUOption { | |
4064 | const char *name; | |
4065 | int flags; | |
4066 | int index; | |
4067 | } QEMUOption; | |
4068 | ||
dbed7e40 | 4069 | static const QEMUOption qemu_options[] = { |
cd6f1169 | 4070 | { "h", 0, QEMU_OPTION_h }, |
5824d651 BS |
4071 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4072 | { option, opt_arg, opt_enum }, | |
4073 | #define DEFHEADING(text) | |
4074 | #include "qemu-options.h" | |
4075 | #undef DEF | |
4076 | #undef DEFHEADING | |
4077 | #undef GEN_DOCS | |
cd6f1169 | 4078 | { NULL }, |
fc01f7e7 FB |
4079 | }; |
4080 | ||
1d14ffa9 | 4081 | #ifdef HAS_AUDIO |
6a36d84e | 4082 | struct soundhw soundhw[] = { |
b00052e4 | 4083 | #ifdef HAS_AUDIO_CHOICE |
4ce7ff6e | 4084 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
fd06c375 FB |
4085 | { |
4086 | "pcspk", | |
4087 | "PC speaker", | |
4088 | 0, | |
4089 | 1, | |
4090 | { .init_isa = pcspk_audio_init } | |
4091 | }, | |
4092 | #endif | |
4c9b53e3 | 4093 | |
4094 | #ifdef CONFIG_SB16 | |
6a36d84e FB |
4095 | { |
4096 | "sb16", | |
4097 | "Creative Sound Blaster 16", | |
4098 | 0, | |
4099 | 1, | |
4100 | { .init_isa = SB16_init } | |
4101 | }, | |
4c9b53e3 | 4102 | #endif |
6a36d84e | 4103 | |
cc53d26d | 4104 | #ifdef CONFIG_CS4231A |
4105 | { | |
4106 | "cs4231a", | |
4107 | "CS4231A", | |
4108 | 0, | |
4109 | 1, | |
4110 | { .init_isa = cs4231a_init } | |
4111 | }, | |
4112 | #endif | |
4113 | ||
1d14ffa9 | 4114 | #ifdef CONFIG_ADLIB |
6a36d84e FB |
4115 | { |
4116 | "adlib", | |
1d14ffa9 | 4117 | #ifdef HAS_YMF262 |
6a36d84e | 4118 | "Yamaha YMF262 (OPL3)", |
1d14ffa9 | 4119 | #else |
6a36d84e | 4120 | "Yamaha YM3812 (OPL2)", |
1d14ffa9 | 4121 | #endif |
6a36d84e FB |
4122 | 0, |
4123 | 1, | |
4124 | { .init_isa = Adlib_init } | |
4125 | }, | |
1d14ffa9 | 4126 | #endif |
6a36d84e | 4127 | |
1d14ffa9 | 4128 | #ifdef CONFIG_GUS |
6a36d84e FB |
4129 | { |
4130 | "gus", | |
4131 | "Gravis Ultrasound GF1", | |
4132 | 0, | |
4133 | 1, | |
4134 | { .init_isa = GUS_init } | |
4135 | }, | |
1d14ffa9 | 4136 | #endif |
6a36d84e | 4137 | |
4c9b53e3 | 4138 | #ifdef CONFIG_AC97 |
e5c9a13e AZ |
4139 | { |
4140 | "ac97", | |
4141 | "Intel 82801AA AC97 Audio", | |
4142 | 0, | |
4143 | 0, | |
4144 | { .init_pci = ac97_init } | |
4145 | }, | |
4c9b53e3 | 4146 | #endif |
e5c9a13e | 4147 | |
4c9b53e3 | 4148 | #ifdef CONFIG_ES1370 |
6a36d84e FB |
4149 | { |
4150 | "es1370", | |
4151 | "ENSONIQ AudioPCI ES1370", | |
4152 | 0, | |
4153 | 0, | |
4154 | { .init_pci = es1370_init } | |
4155 | }, | |
b00052e4 | 4156 | #endif |
6a36d84e | 4157 | |
4c9b53e3 | 4158 | #endif /* HAS_AUDIO_CHOICE */ |
4159 | ||
6a36d84e FB |
4160 | { NULL, NULL, 0, 0, { NULL } } |
4161 | }; | |
4162 | ||
4163 | static void select_soundhw (const char *optarg) | |
4164 | { | |
4165 | struct soundhw *c; | |
4166 | ||
4167 | if (*optarg == '?') { | |
4168 | show_valid_cards: | |
4169 | ||
4170 | printf ("Valid sound card names (comma separated):\n"); | |
4171 | for (c = soundhw; c->name; ++c) { | |
4172 | printf ("%-11s %s\n", c->name, c->descr); | |
4173 | } | |
4174 | printf ("\n-soundhw all will enable all of the above\n"); | |
1d14ffa9 FB |
4175 | exit (*optarg != '?'); |
4176 | } | |
4177 | else { | |
6a36d84e | 4178 | size_t l; |
1d14ffa9 FB |
4179 | const char *p; |
4180 | char *e; | |
4181 | int bad_card = 0; | |
4182 | ||
6a36d84e FB |
4183 | if (!strcmp (optarg, "all")) { |
4184 | for (c = soundhw; c->name; ++c) { | |
4185 | c->enabled = 1; | |
4186 | } | |
4187 | return; | |
4188 | } | |
1d14ffa9 | 4189 | |
6a36d84e | 4190 | p = optarg; |
1d14ffa9 FB |
4191 | while (*p) { |
4192 | e = strchr (p, ','); | |
4193 | l = !e ? strlen (p) : (size_t) (e - p); | |
6a36d84e FB |
4194 | |
4195 | for (c = soundhw; c->name; ++c) { | |
4196 | if (!strncmp (c->name, p, l)) { | |
4197 | c->enabled = 1; | |
1d14ffa9 FB |
4198 | break; |
4199 | } | |
4200 | } | |
6a36d84e FB |
4201 | |
4202 | if (!c->name) { | |
1d14ffa9 FB |
4203 | if (l > 80) { |
4204 | fprintf (stderr, | |
4205 | "Unknown sound card name (too big to show)\n"); | |
4206 | } | |
4207 | else { | |
4208 | fprintf (stderr, "Unknown sound card name `%.*s'\n", | |
4209 | (int) l, p); | |
4210 | } | |
4211 | bad_card = 1; | |
4212 | } | |
4213 | p += l + (e != NULL); | |
4214 | } | |
4215 | ||
4216 | if (bad_card) | |
4217 | goto show_valid_cards; | |
4218 | } | |
4219 | } | |
4220 | #endif | |
4221 | ||
3893c124 | 4222 | static void select_vgahw (const char *p) |
4223 | { | |
4224 | const char *opts; | |
4225 | ||
28b85ed8 AL |
4226 | cirrus_vga_enabled = 0; |
4227 | std_vga_enabled = 0; | |
4228 | vmsvga_enabled = 0; | |
94909d9f | 4229 | xenfb_enabled = 0; |
3893c124 | 4230 | if (strstart(p, "std", &opts)) { |
c2b3b41a | 4231 | std_vga_enabled = 1; |
3893c124 | 4232 | } else if (strstart(p, "cirrus", &opts)) { |
4233 | cirrus_vga_enabled = 1; | |
3893c124 | 4234 | } else if (strstart(p, "vmware", &opts)) { |
3893c124 | 4235 | vmsvga_enabled = 1; |
94909d9f AL |
4236 | } else if (strstart(p, "xenfb", &opts)) { |
4237 | xenfb_enabled = 1; | |
28b85ed8 | 4238 | } else if (!strstart(p, "none", &opts)) { |
3893c124 | 4239 | invalid_vga: |
4240 | fprintf(stderr, "Unknown vga type: %s\n", p); | |
4241 | exit(1); | |
4242 | } | |
cb5a7aa8 | 4243 | while (*opts) { |
4244 | const char *nextopt; | |
4245 | ||
4246 | if (strstart(opts, ",retrace=", &nextopt)) { | |
4247 | opts = nextopt; | |
4248 | if (strstart(opts, "dumb", &nextopt)) | |
4249 | vga_retrace_method = VGA_RETRACE_DUMB; | |
4250 | else if (strstart(opts, "precise", &nextopt)) | |
4251 | vga_retrace_method = VGA_RETRACE_PRECISE; | |
4252 | else goto invalid_vga; | |
4253 | } else goto invalid_vga; | |
4254 | opts = nextopt; | |
4255 | } | |
3893c124 | 4256 | } |
4257 | ||
3587d7e6 FB |
4258 | #ifdef _WIN32 |
4259 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) | |
4260 | { | |
4261 | exit(STATUS_CONTROL_C_EXIT); | |
4262 | return TRUE; | |
4263 | } | |
4264 | #endif | |
4265 | ||
c4be29ff | 4266 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
8fcb1b90 BS |
4267 | { |
4268 | int ret; | |
4269 | ||
4270 | if(strlen(str) != 36) | |
4271 | return -1; | |
4272 | ||
4273 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
4274 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
4275 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); | |
4276 | ||
4277 | if(ret != 16) | |
4278 | return -1; | |
4279 | ||
b6f6e3d3 AL |
4280 | #ifdef TARGET_I386 |
4281 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); | |
4282 | #endif | |
4283 | ||
8fcb1b90 BS |
4284 | return 0; |
4285 | } | |
4286 | ||
7c9d8e07 | 4287 | #define MAX_NET_CLIENTS 32 |
c20709aa | 4288 | |
5b08fc10 AL |
4289 | #ifndef _WIN32 |
4290 | ||
4291 | static void termsig_handler(int signal) | |
4292 | { | |
4293 | qemu_system_shutdown_request(); | |
4294 | } | |
4295 | ||
6f9e3801 | 4296 | static void termsig_setup(void) |
5b08fc10 AL |
4297 | { |
4298 | struct sigaction act; | |
4299 | ||
4300 | memset(&act, 0, sizeof(act)); | |
4301 | act.sa_handler = termsig_handler; | |
4302 | sigaction(SIGINT, &act, NULL); | |
4303 | sigaction(SIGHUP, &act, NULL); | |
4304 | sigaction(SIGTERM, &act, NULL); | |
4305 | } | |
4306 | ||
4307 | #endif | |
4308 | ||
902b3d5c | 4309 | int main(int argc, char **argv, char **envp) |
0824d6fc | 4310 | { |
67b915a5 | 4311 | #ifdef CONFIG_GDBSTUB |
59030a8c | 4312 | const char *gdbstub_dev = NULL; |
67b915a5 | 4313 | #endif |
28c5af54 | 4314 | uint32_t boot_devices_bitmap = 0; |
e4bcb14c | 4315 | int i; |
28c5af54 | 4316 | int snapshot, linux_boot, net_boot; |
7f7f9873 | 4317 | const char *initrd_filename; |
a20dd508 | 4318 | const char *kernel_filename, *kernel_cmdline; |
28c5af54 | 4319 | const char *boot_devices = ""; |
3023f332 | 4320 | DisplayState *ds; |
7d957bd8 | 4321 | DisplayChangeListener *dcl; |
46d4767d | 4322 | int cyls, heads, secs, translation; |
fd5f393a | 4323 | const char *net_clients[MAX_NET_CLIENTS]; |
7c9d8e07 | 4324 | int nb_net_clients; |
dc72ac14 AZ |
4325 | const char *bt_opts[MAX_BT_CMDLINE]; |
4326 | int nb_bt_opts; | |
e4bcb14c | 4327 | int hda_index; |
cd6f1169 FB |
4328 | int optind; |
4329 | const char *r, *optarg; | |
4c621805 | 4330 | CharDriverState *monitor_hd = NULL; |
fd5f393a PB |
4331 | const char *monitor_device; |
4332 | const char *serial_devices[MAX_SERIAL_PORTS]; | |
8d11df9e | 4333 | int serial_device_index; |
fd5f393a | 4334 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
6508fe59 | 4335 | int parallel_device_index; |
9ede2fde AL |
4336 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
4337 | int virtio_console_index; | |
d63d307f | 4338 | const char *loadvm = NULL; |
cc1daa40 | 4339 | QEMUMachine *machine; |
94fc95cd | 4340 | const char *cpu_model; |
fd5f393a | 4341 | const char *usb_devices[MAX_USB_CMDLINE]; |
a594cfbf | 4342 | int usb_devices_index; |
b9e82a59 | 4343 | #ifndef _WIN32 |
71e3ceb8 | 4344 | int fds[2]; |
b9e82a59 | 4345 | #endif |
26a5f13b | 4346 | int tb_size; |
93815bc2 | 4347 | const char *pid_file = NULL; |
5bb7910a | 4348 | const char *incoming = NULL; |
b9e82a59 | 4349 | #ifndef _WIN32 |
54042bcf AL |
4350 | int fd = 0; |
4351 | struct passwd *pwd = NULL; | |
0858532e AL |
4352 | const char *chroot_dir = NULL; |
4353 | const char *run_as = NULL; | |
b9e82a59 | 4354 | #endif |
268a362c | 4355 | CPUState *env; |
0bd48850 | 4356 | |
902b3d5c | 4357 | qemu_cache_utils_init(envp); |
4358 | ||
0bd48850 | 4359 | LIST_INIT (&vm_change_state_head); |
be995c27 FB |
4360 | #ifndef _WIN32 |
4361 | { | |
4362 | struct sigaction act; | |
4363 | sigfillset(&act.sa_mask); | |
4364 | act.sa_flags = 0; | |
4365 | act.sa_handler = SIG_IGN; | |
4366 | sigaction(SIGPIPE, &act, NULL); | |
4367 | } | |
3587d7e6 FB |
4368 | #else |
4369 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); | |
a8e5ac33 FB |
4370 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
4371 | QEMU to run on a single CPU */ | |
4372 | { | |
4373 | HANDLE h; | |
4374 | DWORD mask, smask; | |
4375 | int i; | |
4376 | h = GetCurrentProcess(); | |
4377 | if (GetProcessAffinityMask(h, &mask, &smask)) { | |
4378 | for(i = 0; i < 32; i++) { | |
4379 | if (mask & (1 << i)) | |
4380 | break; | |
4381 | } | |
4382 | if (i != 32) { | |
4383 | mask = 1 << i; | |
4384 | SetProcessAffinityMask(h, mask); | |
4385 | } | |
4386 | } | |
4387 | } | |
67b915a5 | 4388 | #endif |
be995c27 | 4389 | |
cc1daa40 FB |
4390 | register_machines(); |
4391 | machine = first_machine; | |
94fc95cd | 4392 | cpu_model = NULL; |
fc01f7e7 | 4393 | initrd_filename = NULL; |
4fc5d071 | 4394 | ram_size = 0; |
313aa567 | 4395 | vga_ram_size = VGA_RAM_SIZE; |
33e3963e | 4396 | snapshot = 0; |
a20dd508 | 4397 | nographic = 0; |
4d3b6f6e | 4398 | curses = 0; |
a20dd508 FB |
4399 | kernel_filename = NULL; |
4400 | kernel_cmdline = ""; | |
c4b1fcc0 | 4401 | cyls = heads = secs = 0; |
46d4767d | 4402 | translation = BIOS_ATA_TRANSLATION_AUTO; |
d47d13b9 | 4403 | monitor_device = "vc:80Cx24C"; |
c4b1fcc0 | 4404 | |
c75a823c | 4405 | serial_devices[0] = "vc:80Cx24C"; |
8d11df9e | 4406 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
fd5f393a | 4407 | serial_devices[i] = NULL; |
8d11df9e | 4408 | serial_device_index = 0; |
3b46e624 | 4409 | |
8290edda | 4410 | parallel_devices[0] = "vc:80Cx24C"; |
6508fe59 | 4411 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
fd5f393a | 4412 | parallel_devices[i] = NULL; |
6508fe59 | 4413 | parallel_device_index = 0; |
3b46e624 | 4414 | |
1b8fc811 | 4415 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
9ede2fde AL |
4416 | virtio_consoles[i] = NULL; |
4417 | virtio_console_index = 0; | |
4418 | ||
268a362c AL |
4419 | for (i = 0; i < MAX_NODES; i++) { |
4420 | node_mem[i] = 0; | |
4421 | node_cpumask[i] = 0; | |
4422 | } | |
4423 | ||
a594cfbf | 4424 | usb_devices_index = 0; |
3b46e624 | 4425 | |
7c9d8e07 | 4426 | nb_net_clients = 0; |
dc72ac14 | 4427 | nb_bt_opts = 0; |
e4bcb14c TS |
4428 | nb_drives = 0; |
4429 | nb_drives_opt = 0; | |
268a362c | 4430 | nb_numa_nodes = 0; |
e4bcb14c | 4431 | hda_index = -1; |
7c9d8e07 FB |
4432 | |
4433 | nb_nics = 0; | |
3b46e624 | 4434 | |
26a5f13b | 4435 | tb_size = 0; |
41bd639b BS |
4436 | autostart= 1; |
4437 | ||
cd6f1169 | 4438 | optind = 1; |
0824d6fc | 4439 | for(;;) { |
cd6f1169 | 4440 | if (optind >= argc) |
0824d6fc | 4441 | break; |
cd6f1169 FB |
4442 | r = argv[optind]; |
4443 | if (r[0] != '-') { | |
609497ab | 4444 | hda_index = drive_add(argv[optind++], HD_ALIAS, 0); |
cd6f1169 FB |
4445 | } else { |
4446 | const QEMUOption *popt; | |
4447 | ||
4448 | optind++; | |
dff5efc8 PB |
4449 | /* Treat --foo the same as -foo. */ |
4450 | if (r[1] == '-') | |
4451 | r++; | |
cd6f1169 FB |
4452 | popt = qemu_options; |
4453 | for(;;) { | |
4454 | if (!popt->name) { | |
5fafdf24 | 4455 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
cd6f1169 FB |
4456 | argv[0], r); |
4457 | exit(1); | |
4458 | } | |
4459 | if (!strcmp(popt->name, r + 1)) | |
4460 | break; | |
4461 | popt++; | |
4462 | } | |
4463 | if (popt->flags & HAS_ARG) { | |
4464 | if (optind >= argc) { | |
4465 | fprintf(stderr, "%s: option '%s' requires an argument\n", | |
4466 | argv[0], r); | |
4467 | exit(1); | |
4468 | } | |
4469 | optarg = argv[optind++]; | |
4470 | } else { | |
4471 | optarg = NULL; | |
4472 | } | |
4473 | ||
4474 | switch(popt->index) { | |
cc1daa40 FB |
4475 | case QEMU_OPTION_M: |
4476 | machine = find_machine(optarg); | |
4477 | if (!machine) { | |
4478 | QEMUMachine *m; | |
4479 | printf("Supported machines are:\n"); | |
4480 | for(m = first_machine; m != NULL; m = m->next) { | |
4481 | printf("%-10s %s%s\n", | |
5fafdf24 | 4482 | m->name, m->desc, |
cc1daa40 FB |
4483 | m == first_machine ? " (default)" : ""); |
4484 | } | |
15f82208 | 4485 | exit(*optarg != '?'); |
cc1daa40 FB |
4486 | } |
4487 | break; | |
94fc95cd JM |
4488 | case QEMU_OPTION_cpu: |
4489 | /* hw initialization will check this */ | |
15f82208 | 4490 | if (*optarg == '?') { |
c732abe2 JM |
4491 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
4492 | #if defined(cpu_list) | |
4493 | cpu_list(stdout, &fprintf); | |
94fc95cd | 4494 | #endif |
15f82208 | 4495 | exit(0); |
94fc95cd JM |
4496 | } else { |
4497 | cpu_model = optarg; | |
4498 | } | |
4499 | break; | |
cd6f1169 | 4500 | case QEMU_OPTION_initrd: |
fc01f7e7 FB |
4501 | initrd_filename = optarg; |
4502 | break; | |
cd6f1169 | 4503 | case QEMU_OPTION_hda: |
e4bcb14c | 4504 | if (cyls == 0) |
609497ab | 4505 | hda_index = drive_add(optarg, HD_ALIAS, 0); |
e4bcb14c | 4506 | else |
609497ab | 4507 | hda_index = drive_add(optarg, HD_ALIAS |
e4bcb14c | 4508 | ",cyls=%d,heads=%d,secs=%d%s", |
609497ab | 4509 | 0, cyls, heads, secs, |
e4bcb14c TS |
4510 | translation == BIOS_ATA_TRANSLATION_LBA ? |
4511 | ",trans=lba" : | |
4512 | translation == BIOS_ATA_TRANSLATION_NONE ? | |
4513 | ",trans=none" : ""); | |
4514 | break; | |
cd6f1169 | 4515 | case QEMU_OPTION_hdb: |
cc1daa40 FB |
4516 | case QEMU_OPTION_hdc: |
4517 | case QEMU_OPTION_hdd: | |
609497ab | 4518 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
fc01f7e7 | 4519 | break; |
e4bcb14c | 4520 | case QEMU_OPTION_drive: |
609497ab | 4521 | drive_add(NULL, "%s", optarg); |
e4bcb14c | 4522 | break; |
3e3d5815 | 4523 | case QEMU_OPTION_mtdblock: |
609497ab | 4524 | drive_add(optarg, MTD_ALIAS); |
3e3d5815 | 4525 | break; |
a1bb27b1 | 4526 | case QEMU_OPTION_sd: |
609497ab | 4527 | drive_add(optarg, SD_ALIAS); |
a1bb27b1 | 4528 | break; |
86f55663 | 4529 | case QEMU_OPTION_pflash: |
609497ab | 4530 | drive_add(optarg, PFLASH_ALIAS); |
86f55663 | 4531 | break; |
cd6f1169 | 4532 | case QEMU_OPTION_snapshot: |
33e3963e FB |
4533 | snapshot = 1; |
4534 | break; | |
cd6f1169 | 4535 | case QEMU_OPTION_hdachs: |
330d0414 | 4536 | { |
330d0414 FB |
4537 | const char *p; |
4538 | p = optarg; | |
4539 | cyls = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4540 | if (cyls < 1 || cyls > 16383) |
4541 | goto chs_fail; | |
330d0414 FB |
4542 | if (*p != ',') |
4543 | goto chs_fail; | |
4544 | p++; | |
4545 | heads = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4546 | if (heads < 1 || heads > 16) |
4547 | goto chs_fail; | |
330d0414 FB |
4548 | if (*p != ',') |
4549 | goto chs_fail; | |
4550 | p++; | |
4551 | secs = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4552 | if (secs < 1 || secs > 63) |
4553 | goto chs_fail; | |
4554 | if (*p == ',') { | |
4555 | p++; | |
4556 | if (!strcmp(p, "none")) | |
4557 | translation = BIOS_ATA_TRANSLATION_NONE; | |
4558 | else if (!strcmp(p, "lba")) | |
4559 | translation = BIOS_ATA_TRANSLATION_LBA; | |
4560 | else if (!strcmp(p, "auto")) | |
4561 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
4562 | else | |
4563 | goto chs_fail; | |
4564 | } else if (*p != '\0') { | |
c4b1fcc0 | 4565 | chs_fail: |
46d4767d FB |
4566 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
4567 | exit(1); | |
c4b1fcc0 | 4568 | } |
e4bcb14c | 4569 | if (hda_index != -1) |
609497ab AZ |
4570 | snprintf(drives_opt[hda_index].opt, |
4571 | sizeof(drives_opt[hda_index].opt), | |
4572 | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s", | |
4573 | 0, cyls, heads, secs, | |
e4bcb14c TS |
4574 | translation == BIOS_ATA_TRANSLATION_LBA ? |
4575 | ",trans=lba" : | |
4576 | translation == BIOS_ATA_TRANSLATION_NONE ? | |
4577 | ",trans=none" : ""); | |
330d0414 FB |
4578 | } |
4579 | break; | |
268a362c AL |
4580 | case QEMU_OPTION_numa: |
4581 | if (nb_numa_nodes >= MAX_NODES) { | |
4582 | fprintf(stderr, "qemu: too many NUMA nodes\n"); | |
4583 | exit(1); | |
4584 | } | |
4585 | numa_add(optarg); | |
4586 | break; | |
cd6f1169 | 4587 | case QEMU_OPTION_nographic: |
a20dd508 FB |
4588 | nographic = 1; |
4589 | break; | |
4d3b6f6e AZ |
4590 | #ifdef CONFIG_CURSES |
4591 | case QEMU_OPTION_curses: | |
4592 | curses = 1; | |
4593 | break; | |
4594 | #endif | |
a171fe39 AZ |
4595 | case QEMU_OPTION_portrait: |
4596 | graphic_rotate = 1; | |
4597 | break; | |
cd6f1169 | 4598 | case QEMU_OPTION_kernel: |
a20dd508 FB |
4599 | kernel_filename = optarg; |
4600 | break; | |
cd6f1169 | 4601 | case QEMU_OPTION_append: |
a20dd508 | 4602 | kernel_cmdline = optarg; |
313aa567 | 4603 | break; |
cd6f1169 | 4604 | case QEMU_OPTION_cdrom: |
609497ab | 4605 | drive_add(optarg, CDROM_ALIAS); |
36b486bb | 4606 | break; |
cd6f1169 | 4607 | case QEMU_OPTION_boot: |
28c5af54 JM |
4608 | boot_devices = optarg; |
4609 | /* We just do some generic consistency checks */ | |
4610 | { | |
4611 | /* Could easily be extended to 64 devices if needed */ | |
60fe76f3 | 4612 | const char *p; |
28c5af54 JM |
4613 | |
4614 | boot_devices_bitmap = 0; | |
4615 | for (p = boot_devices; *p != '\0'; p++) { | |
4616 | /* Allowed boot devices are: | |
4617 | * a b : floppy disk drives | |
4618 | * c ... f : IDE disk drives | |
4619 | * g ... m : machine implementation dependant drives | |
4620 | * n ... p : network devices | |
4621 | * It's up to each machine implementation to check | |
4622 | * if the given boot devices match the actual hardware | |
4623 | * implementation and firmware features. | |
4624 | */ | |
4625 | if (*p < 'a' || *p > 'q') { | |
4626 | fprintf(stderr, "Invalid boot device '%c'\n", *p); | |
4627 | exit(1); | |
4628 | } | |
4629 | if (boot_devices_bitmap & (1 << (*p - 'a'))) { | |
4630 | fprintf(stderr, | |
4631 | "Boot device '%c' was given twice\n",*p); | |
4632 | exit(1); | |
4633 | } | |
4634 | boot_devices_bitmap |= 1 << (*p - 'a'); | |
4635 | } | |
36b486bb FB |
4636 | } |
4637 | break; | |
cd6f1169 | 4638 | case QEMU_OPTION_fda: |
cd6f1169 | 4639 | case QEMU_OPTION_fdb: |
609497ab | 4640 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
c45886db | 4641 | break; |
52ca8d6a FB |
4642 | #ifdef TARGET_I386 |
4643 | case QEMU_OPTION_no_fd_bootchk: | |
4644 | fd_bootchk = 0; | |
4645 | break; | |
4646 | #endif | |
7c9d8e07 FB |
4647 | case QEMU_OPTION_net: |
4648 | if (nb_net_clients >= MAX_NET_CLIENTS) { | |
4649 | fprintf(stderr, "qemu: too many network clients\n"); | |
c4b1fcc0 FB |
4650 | exit(1); |
4651 | } | |
fd5f393a | 4652 | net_clients[nb_net_clients] = optarg; |
7c9d8e07 | 4653 | nb_net_clients++; |
702c651c | 4654 | break; |
c7f74643 FB |
4655 | #ifdef CONFIG_SLIRP |
4656 | case QEMU_OPTION_tftp: | |
c7f74643 | 4657 | tftp_prefix = optarg; |
9bf05444 | 4658 | break; |
47d5d01a TS |
4659 | case QEMU_OPTION_bootp: |
4660 | bootp_filename = optarg; | |
4661 | break; | |
c94c8d64 | 4662 | #ifndef _WIN32 |
9d728e8c FB |
4663 | case QEMU_OPTION_smb: |
4664 | net_slirp_smb(optarg); | |
4665 | break; | |
c94c8d64 | 4666 | #endif |
9bf05444 | 4667 | case QEMU_OPTION_redir: |
d4ebe193 | 4668 | net_slirp_redir(NULL, optarg); |
9bf05444 | 4669 | break; |
c7f74643 | 4670 | #endif |
dc72ac14 AZ |
4671 | case QEMU_OPTION_bt: |
4672 | if (nb_bt_opts >= MAX_BT_CMDLINE) { | |
4673 | fprintf(stderr, "qemu: too many bluetooth options\n"); | |
4674 | exit(1); | |
4675 | } | |
4676 | bt_opts[nb_bt_opts++] = optarg; | |
4677 | break; | |
1d14ffa9 | 4678 | #ifdef HAS_AUDIO |
1d14ffa9 FB |
4679 | case QEMU_OPTION_audio_help: |
4680 | AUD_help (); | |
4681 | exit (0); | |
4682 | break; | |
4683 | case QEMU_OPTION_soundhw: | |
4684 | select_soundhw (optarg); | |
4685 | break; | |
4686 | #endif | |
cd6f1169 | 4687 | case QEMU_OPTION_h: |
15f82208 | 4688 | help(0); |
cd6f1169 | 4689 | break; |
9bd7e6d9 PB |
4690 | case QEMU_OPTION_version: |
4691 | version(); | |
4692 | exit(0); | |
4693 | break; | |
00f82b8a AJ |
4694 | case QEMU_OPTION_m: { |
4695 | uint64_t value; | |
4696 | char *ptr; | |
4697 | ||
4698 | value = strtoul(optarg, &ptr, 10); | |
4699 | switch (*ptr) { | |
4700 | case 0: case 'M': case 'm': | |
4701 | value <<= 20; | |
4702 | break; | |
4703 | case 'G': case 'g': | |
4704 | value <<= 30; | |
4705 | break; | |
4706 | default: | |
4707 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); | |
cd6f1169 FB |
4708 | exit(1); |
4709 | } | |
00f82b8a AJ |
4710 | |
4711 | /* On 32-bit hosts, QEMU is limited by virtual address space */ | |
4712 | if (value > (2047 << 20) | |
640f42e4 | 4713 | #ifndef CONFIG_KQEMU |
00f82b8a AJ |
4714 | && HOST_LONG_BITS == 32 |
4715 | #endif | |
4716 | ) { | |
4717 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); | |
4718 | exit(1); | |
4719 | } | |
4720 | if (value != (uint64_t)(ram_addr_t)value) { | |
4721 | fprintf(stderr, "qemu: ram size too large\n"); | |
4722 | exit(1); | |
4723 | } | |
4724 | ram_size = value; | |
cd6f1169 | 4725 | break; |
00f82b8a | 4726 | } |
cd6f1169 FB |
4727 | case QEMU_OPTION_d: |
4728 | { | |
4729 | int mask; | |
c7cd6a37 | 4730 | const CPULogItem *item; |
3b46e624 | 4731 | |
cd6f1169 FB |
4732 | mask = cpu_str_to_log_mask(optarg); |
4733 | if (!mask) { | |
4734 | printf("Log items (comma separated):\n"); | |
f193c797 FB |
4735 | for(item = cpu_log_items; item->mask != 0; item++) { |
4736 | printf("%-10s %s\n", item->name, item->help); | |
4737 | } | |
4738 | exit(1); | |
cd6f1169 FB |
4739 | } |
4740 | cpu_set_log(mask); | |
f193c797 | 4741 | } |
cd6f1169 | 4742 | break; |
67b915a5 | 4743 | #ifdef CONFIG_GDBSTUB |
cd6f1169 | 4744 | case QEMU_OPTION_s: |
59030a8c | 4745 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
cd6f1169 | 4746 | break; |
59030a8c AL |
4747 | case QEMU_OPTION_gdb: |
4748 | gdbstub_dev = optarg; | |
cd6f1169 | 4749 | break; |
67b915a5 | 4750 | #endif |
cd6f1169 FB |
4751 | case QEMU_OPTION_L: |
4752 | bios_dir = optarg; | |
4753 | break; | |
1192dad8 JM |
4754 | case QEMU_OPTION_bios: |
4755 | bios_name = optarg; | |
4756 | break; | |
1b530a6d AJ |
4757 | case QEMU_OPTION_singlestep: |
4758 | singlestep = 1; | |
4759 | break; | |
cd6f1169 | 4760 | case QEMU_OPTION_S: |
3c07f8e8 | 4761 | autostart = 0; |
cd6f1169 | 4762 | break; |
5824d651 | 4763 | #ifndef _WIN32 |
3d11d0eb FB |
4764 | case QEMU_OPTION_k: |
4765 | keyboard_layout = optarg; | |
4766 | break; | |
5824d651 | 4767 | #endif |
ee22c2f7 FB |
4768 | case QEMU_OPTION_localtime: |
4769 | rtc_utc = 0; | |
4770 | break; | |
3893c124 | 4771 | case QEMU_OPTION_vga: |
4772 | select_vgahw (optarg); | |
1bfe856e | 4773 | break; |
5824d651 | 4774 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
e9b137c2 FB |
4775 | case QEMU_OPTION_g: |
4776 | { | |
4777 | const char *p; | |
4778 | int w, h, depth; | |
4779 | p = optarg; | |
4780 | w = strtol(p, (char **)&p, 10); | |
4781 | if (w <= 0) { | |
4782 | graphic_error: | |
4783 | fprintf(stderr, "qemu: invalid resolution or depth\n"); | |
4784 | exit(1); | |
4785 | } | |
4786 | if (*p != 'x') | |
4787 | goto graphic_error; | |
4788 | p++; | |
4789 | h = strtol(p, (char **)&p, 10); | |
4790 | if (h <= 0) | |
4791 | goto graphic_error; | |
4792 | if (*p == 'x') { | |
4793 | p++; | |
4794 | depth = strtol(p, (char **)&p, 10); | |
5fafdf24 | 4795 | if (depth != 8 && depth != 15 && depth != 16 && |
e9b137c2 FB |
4796 | depth != 24 && depth != 32) |
4797 | goto graphic_error; | |
4798 | } else if (*p == '\0') { | |
4799 | depth = graphic_depth; | |
4800 | } else { | |
4801 | goto graphic_error; | |
4802 | } | |
3b46e624 | 4803 | |
e9b137c2 FB |
4804 | graphic_width = w; |
4805 | graphic_height = h; | |
4806 | graphic_depth = depth; | |
4807 | } | |
4808 | break; | |
5824d651 | 4809 | #endif |
20d8a3ed TS |
4810 | case QEMU_OPTION_echr: |
4811 | { | |
4812 | char *r; | |
4813 | term_escape_char = strtol(optarg, &r, 0); | |
4814 | if (r == optarg) | |
4815 | printf("Bad argument to echr\n"); | |
4816 | break; | |
4817 | } | |
82c643ff | 4818 | case QEMU_OPTION_monitor: |
fd5f393a | 4819 | monitor_device = optarg; |
82c643ff FB |
4820 | break; |
4821 | case QEMU_OPTION_serial: | |
8d11df9e FB |
4822 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
4823 | fprintf(stderr, "qemu: too many serial ports\n"); | |
4824 | exit(1); | |
4825 | } | |
fd5f393a | 4826 | serial_devices[serial_device_index] = optarg; |
8d11df9e | 4827 | serial_device_index++; |
82c643ff | 4828 | break; |
51ecf136 AL |
4829 | case QEMU_OPTION_virtiocon: |
4830 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { | |
4831 | fprintf(stderr, "qemu: too many virtio consoles\n"); | |
4832 | exit(1); | |
4833 | } | |
4834 | virtio_consoles[virtio_console_index] = optarg; | |
4835 | virtio_console_index++; | |
4836 | break; | |
6508fe59 FB |
4837 | case QEMU_OPTION_parallel: |
4838 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { | |
4839 | fprintf(stderr, "qemu: too many parallel ports\n"); | |
4840 | exit(1); | |
4841 | } | |
fd5f393a | 4842 | parallel_devices[parallel_device_index] = optarg; |
6508fe59 FB |
4843 | parallel_device_index++; |
4844 | break; | |
d63d307f FB |
4845 | case QEMU_OPTION_loadvm: |
4846 | loadvm = optarg; | |
4847 | break; | |
4848 | case QEMU_OPTION_full_screen: | |
4849 | full_screen = 1; | |
4850 | break; | |
667accab | 4851 | #ifdef CONFIG_SDL |
43523e93 TS |
4852 | case QEMU_OPTION_no_frame: |
4853 | no_frame = 1; | |
4854 | break; | |
3780e197 TS |
4855 | case QEMU_OPTION_alt_grab: |
4856 | alt_grab = 1; | |
4857 | break; | |
667accab TS |
4858 | case QEMU_OPTION_no_quit: |
4859 | no_quit = 1; | |
4860 | break; | |
7d957bd8 AL |
4861 | case QEMU_OPTION_sdl: |
4862 | sdl = 1; | |
4863 | break; | |
667accab | 4864 | #endif |
f7cce898 | 4865 | case QEMU_OPTION_pidfile: |
93815bc2 | 4866 | pid_file = optarg; |
f7cce898 | 4867 | break; |
a09db21f FB |
4868 | #ifdef TARGET_I386 |
4869 | case QEMU_OPTION_win2k_hack: | |
4870 | win2k_install_hack = 1; | |
4871 | break; | |
73822ec8 AL |
4872 | case QEMU_OPTION_rtc_td_hack: |
4873 | rtc_td_hack = 1; | |
4874 | break; | |
8a92ea2f AL |
4875 | case QEMU_OPTION_acpitable: |
4876 | if(acpi_table_add(optarg) < 0) { | |
4877 | fprintf(stderr, "Wrong acpi table provided\n"); | |
4878 | exit(1); | |
4879 | } | |
4880 | break; | |
b6f6e3d3 AL |
4881 | case QEMU_OPTION_smbios: |
4882 | if(smbios_entry_add(optarg) < 0) { | |
4883 | fprintf(stderr, "Wrong smbios provided\n"); | |
4884 | exit(1); | |
4885 | } | |
4886 | break; | |
a09db21f | 4887 | #endif |
640f42e4 | 4888 | #ifdef CONFIG_KQEMU |
d993e026 FB |
4889 | case QEMU_OPTION_no_kqemu: |
4890 | kqemu_allowed = 0; | |
4891 | break; | |
89bfc105 FB |
4892 | case QEMU_OPTION_kernel_kqemu: |
4893 | kqemu_allowed = 2; | |
4894 | break; | |
7ba1e619 AL |
4895 | #endif |
4896 | #ifdef CONFIG_KVM | |
4897 | case QEMU_OPTION_enable_kvm: | |
4898 | kvm_allowed = 1; | |
640f42e4 | 4899 | #ifdef CONFIG_KQEMU |
7ba1e619 AL |
4900 | kqemu_allowed = 0; |
4901 | #endif | |
4902 | break; | |
d993e026 | 4903 | #endif |
bb36d470 FB |
4904 | case QEMU_OPTION_usb: |
4905 | usb_enabled = 1; | |
4906 | break; | |
a594cfbf FB |
4907 | case QEMU_OPTION_usbdevice: |
4908 | usb_enabled = 1; | |
0d92ed30 | 4909 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
a594cfbf FB |
4910 | fprintf(stderr, "Too many USB devices\n"); |
4911 | exit(1); | |
4912 | } | |
fd5f393a | 4913 | usb_devices[usb_devices_index] = optarg; |
a594cfbf FB |
4914 | usb_devices_index++; |
4915 | break; | |
6a00d601 FB |
4916 | case QEMU_OPTION_smp: |
4917 | smp_cpus = atoi(optarg); | |
b2097003 | 4918 | if (smp_cpus < 1) { |
6a00d601 FB |
4919 | fprintf(stderr, "Invalid number of CPUs\n"); |
4920 | exit(1); | |
4921 | } | |
4922 | break; | |
24236869 | 4923 | case QEMU_OPTION_vnc: |
73fc9742 | 4924 | vnc_display = optarg; |
24236869 | 4925 | break; |
5824d651 | 4926 | #ifdef TARGET_I386 |
6515b203 FB |
4927 | case QEMU_OPTION_no_acpi: |
4928 | acpi_enabled = 0; | |
4929 | break; | |
16b29ae1 AL |
4930 | case QEMU_OPTION_no_hpet: |
4931 | no_hpet = 1; | |
4932 | break; | |
5824d651 | 4933 | #endif |
d1beab82 FB |
4934 | case QEMU_OPTION_no_reboot: |
4935 | no_reboot = 1; | |
4936 | break; | |
b2f76161 AJ |
4937 | case QEMU_OPTION_no_shutdown: |
4938 | no_shutdown = 1; | |
4939 | break; | |
9467cd46 AZ |
4940 | case QEMU_OPTION_show_cursor: |
4941 | cursor_hide = 0; | |
4942 | break; | |
8fcb1b90 BS |
4943 | case QEMU_OPTION_uuid: |
4944 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { | |
4945 | fprintf(stderr, "Fail to parse UUID string." | |
4946 | " Wrong format.\n"); | |
4947 | exit(1); | |
4948 | } | |
4949 | break; | |
5824d651 | 4950 | #ifndef _WIN32 |
71e3ceb8 TS |
4951 | case QEMU_OPTION_daemonize: |
4952 | daemonize = 1; | |
4953 | break; | |
5824d651 | 4954 | #endif |
9ae02555 TS |
4955 | case QEMU_OPTION_option_rom: |
4956 | if (nb_option_roms >= MAX_OPTION_ROMS) { | |
4957 | fprintf(stderr, "Too many option ROMs\n"); | |
4958 | exit(1); | |
4959 | } | |
4960 | option_rom[nb_option_roms] = optarg; | |
4961 | nb_option_roms++; | |
4962 | break; | |
5824d651 | 4963 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
8e71621f PB |
4964 | case QEMU_OPTION_semihosting: |
4965 | semihosting_enabled = 1; | |
4966 | break; | |
5824d651 | 4967 | #endif |
c35734b2 TS |
4968 | case QEMU_OPTION_name: |
4969 | qemu_name = optarg; | |
4970 | break; | |
95efd11c | 4971 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
4972 | case QEMU_OPTION_prom_env: |
4973 | if (nb_prom_envs >= MAX_PROM_ENVS) { | |
4974 | fprintf(stderr, "Too many prom variables\n"); | |
4975 | exit(1); | |
4976 | } | |
4977 | prom_envs[nb_prom_envs] = optarg; | |
4978 | nb_prom_envs++; | |
4979 | break; | |
2b8f2d41 AZ |
4980 | #endif |
4981 | #ifdef TARGET_ARM | |
4982 | case QEMU_OPTION_old_param: | |
4983 | old_param = 1; | |
05ebd537 | 4984 | break; |
66508601 | 4985 | #endif |
f3dcfada TS |
4986 | case QEMU_OPTION_clock: |
4987 | configure_alarms(optarg); | |
4988 | break; | |
7e0af5d0 FB |
4989 | case QEMU_OPTION_startdate: |
4990 | { | |
4991 | struct tm tm; | |
f6503059 | 4992 | time_t rtc_start_date; |
7e0af5d0 | 4993 | if (!strcmp(optarg, "now")) { |
f6503059 | 4994 | rtc_date_offset = -1; |
7e0af5d0 FB |
4995 | } else { |
4996 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", | |
4997 | &tm.tm_year, | |
4998 | &tm.tm_mon, | |
4999 | &tm.tm_mday, | |
5000 | &tm.tm_hour, | |
5001 | &tm.tm_min, | |
5002 | &tm.tm_sec) == 6) { | |
5003 | /* OK */ | |
5004 | } else if (sscanf(optarg, "%d-%d-%d", | |
5005 | &tm.tm_year, | |
5006 | &tm.tm_mon, | |
5007 | &tm.tm_mday) == 3) { | |
5008 | tm.tm_hour = 0; | |
5009 | tm.tm_min = 0; | |
5010 | tm.tm_sec = 0; | |
5011 | } else { | |
5012 | goto date_fail; | |
5013 | } | |
5014 | tm.tm_year -= 1900; | |
5015 | tm.tm_mon--; | |
3c6b2088 | 5016 | rtc_start_date = mktimegm(&tm); |
7e0af5d0 FB |
5017 | if (rtc_start_date == -1) { |
5018 | date_fail: | |
5019 | fprintf(stderr, "Invalid date format. Valid format are:\n" | |
5020 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); | |
5021 | exit(1); | |
5022 | } | |
f6503059 | 5023 | rtc_date_offset = time(NULL) - rtc_start_date; |
7e0af5d0 FB |
5024 | } |
5025 | } | |
5026 | break; | |
26a5f13b FB |
5027 | case QEMU_OPTION_tb_size: |
5028 | tb_size = strtol(optarg, NULL, 0); | |
5029 | if (tb_size < 0) | |
5030 | tb_size = 0; | |
5031 | break; | |
2e70f6ef PB |
5032 | case QEMU_OPTION_icount: |
5033 | use_icount = 1; | |
5034 | if (strcmp(optarg, "auto") == 0) { | |
5035 | icount_time_shift = -1; | |
5036 | } else { | |
5037 | icount_time_shift = strtol(optarg, NULL, 0); | |
5038 | } | |
5039 | break; | |
5bb7910a AL |
5040 | case QEMU_OPTION_incoming: |
5041 | incoming = optarg; | |
5042 | break; | |
5824d651 | 5043 | #ifndef _WIN32 |
0858532e AL |
5044 | case QEMU_OPTION_chroot: |
5045 | chroot_dir = optarg; | |
5046 | break; | |
5047 | case QEMU_OPTION_runas: | |
5048 | run_as = optarg; | |
5049 | break; | |
e37630ca AL |
5050 | #endif |
5051 | #ifdef CONFIG_XEN | |
5052 | case QEMU_OPTION_xen_domid: | |
5053 | xen_domid = atoi(optarg); | |
5054 | break; | |
5055 | case QEMU_OPTION_xen_create: | |
5056 | xen_mode = XEN_CREATE; | |
5057 | break; | |
5058 | case QEMU_OPTION_xen_attach: | |
5059 | xen_mode = XEN_ATTACH; | |
5060 | break; | |
5824d651 | 5061 | #endif |
cd6f1169 | 5062 | } |
0824d6fc FB |
5063 | } |
5064 | } | |
330d0414 | 5065 | |
640f42e4 | 5066 | #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU) |
7ba1e619 AL |
5067 | if (kvm_allowed && kqemu_allowed) { |
5068 | fprintf(stderr, | |
5069 | "You can not enable both KVM and kqemu at the same time\n"); | |
5070 | exit(1); | |
5071 | } | |
5072 | #endif | |
5073 | ||
3d878caa | 5074 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
b2097003 AL |
5075 | if (smp_cpus > machine->max_cpus) { |
5076 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " | |
5077 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, | |
5078 | machine->max_cpus); | |
5079 | exit(1); | |
5080 | } | |
5081 | ||
bc0129d9 AL |
5082 | if (nographic) { |
5083 | if (serial_device_index == 0) | |
5084 | serial_devices[0] = "stdio"; | |
5085 | if (parallel_device_index == 0) | |
5086 | parallel_devices[0] = "null"; | |
5087 | if (strncmp(monitor_device, "vc", 2) == 0) | |
5088 | monitor_device = "stdio"; | |
5089 | } | |
5090 | ||
71e3ceb8 | 5091 | #ifndef _WIN32 |
71e3ceb8 TS |
5092 | if (daemonize) { |
5093 | pid_t pid; | |
5094 | ||
5095 | if (pipe(fds) == -1) | |
5096 | exit(1); | |
5097 | ||
5098 | pid = fork(); | |
5099 | if (pid > 0) { | |
5100 | uint8_t status; | |
5101 | ssize_t len; | |
5102 | ||
5103 | close(fds[1]); | |
5104 | ||
5105 | again: | |
93815bc2 TS |
5106 | len = read(fds[0], &status, 1); |
5107 | if (len == -1 && (errno == EINTR)) | |
5108 | goto again; | |
5109 | ||
5110 | if (len != 1) | |
5111 | exit(1); | |
5112 | else if (status == 1) { | |
5113 | fprintf(stderr, "Could not acquire pidfile\n"); | |
5114 | exit(1); | |
5115 | } else | |
5116 | exit(0); | |
71e3ceb8 | 5117 | } else if (pid < 0) |
93815bc2 | 5118 | exit(1); |
71e3ceb8 TS |
5119 | |
5120 | setsid(); | |
5121 | ||
5122 | pid = fork(); | |
5123 | if (pid > 0) | |
5124 | exit(0); | |
5125 | else if (pid < 0) | |
5126 | exit(1); | |
5127 | ||
5128 | umask(027); | |
71e3ceb8 TS |
5129 | |
5130 | signal(SIGTSTP, SIG_IGN); | |
5131 | signal(SIGTTOU, SIG_IGN); | |
5132 | signal(SIGTTIN, SIG_IGN); | |
5133 | } | |
71e3ceb8 | 5134 | |
aa26bb2d | 5135 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
93815bc2 TS |
5136 | if (daemonize) { |
5137 | uint8_t status = 1; | |
5138 | write(fds[1], &status, 1); | |
5139 | } else | |
5140 | fprintf(stderr, "Could not acquire pid file\n"); | |
5141 | exit(1); | |
5142 | } | |
b9e82a59 | 5143 | #endif |
93815bc2 | 5144 | |
640f42e4 | 5145 | #ifdef CONFIG_KQEMU |
ff3fbb30 FB |
5146 | if (smp_cpus > 1) |
5147 | kqemu_allowed = 0; | |
5148 | #endif | |
a20dd508 | 5149 | linux_boot = (kernel_filename != NULL); |
7317b8ca | 5150 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
6c41b272 | 5151 | |
f8d39c01 TS |
5152 | if (!linux_boot && *kernel_cmdline != '\0') { |
5153 | fprintf(stderr, "-append only allowed with -kernel option\n"); | |
5154 | exit(1); | |
5155 | } | |
5156 | ||
5157 | if (!linux_boot && initrd_filename != NULL) { | |
5158 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); | |
5159 | exit(1); | |
5160 | } | |
5161 | ||
96d30e48 | 5162 | /* boot to floppy or the default cd if no hard disk defined yet */ |
28c5af54 | 5163 | if (!boot_devices[0]) { |
e4bcb14c | 5164 | boot_devices = "cad"; |
96d30e48 | 5165 | } |
b118d61e | 5166 | setvbuf(stdout, NULL, _IOLBF, 0); |
3b46e624 | 5167 | |
634fce96 | 5168 | init_timers(); |
7183b4b4 AL |
5169 | if (init_timer_alarm() < 0) { |
5170 | fprintf(stderr, "could not initialize alarm timer\n"); | |
5171 | exit(1); | |
5172 | } | |
2e70f6ef PB |
5173 | if (use_icount && icount_time_shift < 0) { |
5174 | use_icount = 2; | |
5175 | /* 125MIPS seems a reasonable initial guess at the guest speed. | |
5176 | It will be corrected fairly quickly anyway. */ | |
5177 | icount_time_shift = 3; | |
5178 | init_icount_adjust(); | |
5179 | } | |
634fce96 | 5180 | |
fd1dff4b FB |
5181 | #ifdef _WIN32 |
5182 | socket_init(); | |
5183 | #endif | |
5184 | ||
7c9d8e07 FB |
5185 | /* init network clients */ |
5186 | if (nb_net_clients == 0) { | |
5187 | /* if no clients, we use a default config */ | |
f441b28b AL |
5188 | net_clients[nb_net_clients++] = "nic"; |
5189 | #ifdef CONFIG_SLIRP | |
5190 | net_clients[nb_net_clients++] = "user"; | |
5191 | #endif | |
c20709aa FB |
5192 | } |
5193 | ||
7c9d8e07 | 5194 | for(i = 0;i < nb_net_clients; i++) { |
9ad97e65 | 5195 | if (net_client_parse(net_clients[i]) < 0) |
7c9d8e07 | 5196 | exit(1); |
702c651c | 5197 | } |
63a01ef8 | 5198 | net_client_check(); |
f1510b2c | 5199 | |
eec85c2a | 5200 | #ifdef TARGET_I386 |
ed494d87 | 5201 | /* XXX: this should be moved in the PC machine instantiation code */ |
28c5af54 JM |
5202 | if (net_boot != 0) { |
5203 | int netroms = 0; | |
5204 | for (i = 0; i < nb_nics && i < 4; i++) { | |
eec85c2a TS |
5205 | const char *model = nd_table[i].model; |
5206 | char buf[1024]; | |
28c5af54 JM |
5207 | if (net_boot & (1 << i)) { |
5208 | if (model == NULL) | |
5209 | model = "ne2k_pci"; | |
5210 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); | |
5211 | if (get_image_size(buf) > 0) { | |
5212 | if (nb_option_roms >= MAX_OPTION_ROMS) { | |
5213 | fprintf(stderr, "Too many option ROMs\n"); | |
5214 | exit(1); | |
5215 | } | |
5216 | option_rom[nb_option_roms] = strdup(buf); | |
5217 | nb_option_roms++; | |
5218 | netroms++; | |
5219 | } | |
5220 | } | |
eec85c2a | 5221 | } |
28c5af54 | 5222 | if (netroms == 0) { |
eec85c2a TS |
5223 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
5224 | exit(1); | |
5225 | } | |
eec85c2a TS |
5226 | } |
5227 | #endif | |
5228 | ||
dc72ac14 AZ |
5229 | /* init the bluetooth world */ |
5230 | for (i = 0; i < nb_bt_opts; i++) | |
5231 | if (bt_parse(bt_opts[i])) | |
5232 | exit(1); | |
5233 | ||
0824d6fc | 5234 | /* init the memory */ |
94a6b54f PB |
5235 | if (ram_size == 0) |
5236 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; | |
9ae02555 | 5237 | |
640f42e4 | 5238 | #ifdef CONFIG_KQEMU |
94a6b54f PB |
5239 | /* FIXME: This is a nasty hack because kqemu can't cope with dynamic |
5240 | guest ram allocation. It needs to go away. */ | |
5241 | if (kqemu_allowed) { | |
5242 | kqemu_phys_ram_size = ram_size + VGA_RAM_SIZE + 4 * 1024 * 1024; | |
5243 | kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size); | |
5244 | if (!kqemu_phys_ram_base) { | |
5245 | fprintf(stderr, "Could not allocate physical memory\n"); | |
5246 | exit(1); | |
5247 | } | |
0824d6fc | 5248 | } |
94a6b54f | 5249 | #endif |
0824d6fc | 5250 | |
26a5f13b FB |
5251 | /* init the dynamic translator */ |
5252 | cpu_exec_init_all(tb_size * 1024 * 1024); | |
5253 | ||
5905b2e5 | 5254 | bdrv_init(); |
6512a2a7 | 5255 | dma_helper_init(); |
c4b1fcc0 | 5256 | |
e4bcb14c | 5257 | /* we always create the cdrom drive, even if no disk is there */ |
c4b1fcc0 | 5258 | |
e4bcb14c | 5259 | if (nb_drives_opt < MAX_DRIVES) |
609497ab | 5260 | drive_add(NULL, CDROM_ALIAS); |
c4b1fcc0 | 5261 | |
9d413d1d | 5262 | /* we always create at least one floppy */ |
33e3963e | 5263 | |
e4bcb14c | 5264 | if (nb_drives_opt < MAX_DRIVES) |
609497ab | 5265 | drive_add(NULL, FD_ALIAS, 0); |
86f55663 | 5266 | |
9d413d1d AZ |
5267 | /* we always create one sd slot, even if no card is in it */ |
5268 | ||
5269 | if (nb_drives_opt < MAX_DRIVES) | |
609497ab | 5270 | drive_add(NULL, SD_ALIAS); |
9d413d1d | 5271 | |
e4bcb14c TS |
5272 | /* open the virtual block devices */ |
5273 | ||
5274 | for(i = 0; i < nb_drives_opt; i++) | |
609497ab | 5275 | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
e4bcb14c | 5276 | exit(1); |
3e3d5815 | 5277 | |
c88676f8 | 5278 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
475e4277 | 5279 | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
8a7ddc38 | 5280 | |
3023f332 AL |
5281 | #ifndef _WIN32 |
5282 | /* must be after terminal init, SDL library changes signal handlers */ | |
5283 | termsig_setup(); | |
5284 | #endif | |
5285 | ||
5286 | /* Maintain compatibility with multiple stdio monitors */ | |
5287 | if (!strcmp(monitor_device,"stdio")) { | |
5288 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { | |
5289 | const char *devname = serial_devices[i]; | |
5290 | if (devname && !strcmp(devname,"mon:stdio")) { | |
5291 | monitor_device = NULL; | |
5292 | break; | |
5293 | } else if (devname && !strcmp(devname,"stdio")) { | |
5294 | monitor_device = NULL; | |
5295 | serial_devices[i] = "mon:stdio"; | |
5296 | break; | |
5297 | } | |
5298 | } | |
5299 | } | |
5300 | ||
268a362c AL |
5301 | if (nb_numa_nodes > 0) { |
5302 | int i; | |
5303 | ||
5304 | if (nb_numa_nodes > smp_cpus) { | |
5305 | nb_numa_nodes = smp_cpus; | |
5306 | } | |
5307 | ||
5308 | /* If no memory size if given for any node, assume the default case | |
5309 | * and distribute the available memory equally across all nodes | |
5310 | */ | |
5311 | for (i = 0; i < nb_numa_nodes; i++) { | |
5312 | if (node_mem[i] != 0) | |
5313 | break; | |
5314 | } | |
5315 | if (i == nb_numa_nodes) { | |
5316 | uint64_t usedmem = 0; | |
5317 | ||
5318 | /* On Linux, the each node's border has to be 8MB aligned, | |
5319 | * the final node gets the rest. | |
5320 | */ | |
5321 | for (i = 0; i < nb_numa_nodes - 1; i++) { | |
5322 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); | |
5323 | usedmem += node_mem[i]; | |
5324 | } | |
5325 | node_mem[i] = ram_size - usedmem; | |
5326 | } | |
5327 | ||
5328 | for (i = 0; i < nb_numa_nodes; i++) { | |
5329 | if (node_cpumask[i] != 0) | |
5330 | break; | |
5331 | } | |
5332 | /* assigning the VCPUs round-robin is easier to implement, guest OSes | |
5333 | * must cope with this anyway, because there are BIOSes out there in | |
5334 | * real machines which also use this scheme. | |
5335 | */ | |
5336 | if (i == nb_numa_nodes) { | |
5337 | for (i = 0; i < smp_cpus; i++) { | |
5338 | node_cpumask[i % nb_numa_nodes] |= 1 << i; | |
5339 | } | |
5340 | } | |
5341 | } | |
5342 | ||
3023f332 AL |
5343 | if (kvm_enabled()) { |
5344 | int ret; | |
5345 | ||
5346 | ret = kvm_init(smp_cpus); | |
5347 | if (ret < 0) { | |
5348 | fprintf(stderr, "failed to initialize KVM\n"); | |
5349 | exit(1); | |
5350 | } | |
5351 | } | |
5352 | ||
4c621805 | 5353 | if (monitor_device) { |
ceecf1d1 | 5354 | monitor_hd = qemu_chr_open("monitor", monitor_device, NULL); |
4c621805 AL |
5355 | if (!monitor_hd) { |
5356 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); | |
5357 | exit(1); | |
5358 | } | |
5359 | } | |
5360 | ||
2796dae0 AL |
5361 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5362 | const char *devname = serial_devices[i]; | |
5363 | if (devname && strcmp(devname, "none")) { | |
5364 | char label[32]; | |
5365 | snprintf(label, sizeof(label), "serial%d", i); | |
ceecf1d1 | 5366 | serial_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5367 | if (!serial_hds[i]) { |
5368 | fprintf(stderr, "qemu: could not open serial device '%s'\n", | |
5369 | devname); | |
5370 | exit(1); | |
5371 | } | |
5372 | } | |
5373 | } | |
5374 | ||
5375 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { | |
5376 | const char *devname = parallel_devices[i]; | |
5377 | if (devname && strcmp(devname, "none")) { | |
5378 | char label[32]; | |
5379 | snprintf(label, sizeof(label), "parallel%d", i); | |
ceecf1d1 | 5380 | parallel_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5381 | if (!parallel_hds[i]) { |
5382 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", | |
5383 | devname); | |
5384 | exit(1); | |
5385 | } | |
5386 | } | |
5387 | } | |
5388 | ||
5389 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { | |
5390 | const char *devname = virtio_consoles[i]; | |
5391 | if (devname && strcmp(devname, "none")) { | |
5392 | char label[32]; | |
5393 | snprintf(label, sizeof(label), "virtcon%d", i); | |
ceecf1d1 | 5394 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5395 | if (!virtcon_hds[i]) { |
5396 | fprintf(stderr, "qemu: could not open virtio console '%s'\n", | |
5397 | devname); | |
5398 | exit(1); | |
5399 | } | |
5400 | } | |
5401 | } | |
5402 | ||
3023f332 AL |
5403 | machine->init(ram_size, vga_ram_size, boot_devices, |
5404 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); | |
5405 | ||
268a362c AL |
5406 | |
5407 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
5408 | for (i = 0; i < nb_numa_nodes; i++) { | |
5409 | if (node_cpumask[i] & (1 << env->cpu_index)) { | |
5410 | env->numa_node = i; | |
5411 | } | |
5412 | } | |
5413 | } | |
5414 | ||
6f338c34 AL |
5415 | current_machine = machine; |
5416 | ||
3023f332 AL |
5417 | /* Set KVM's vcpu state to qemu's initial CPUState. */ |
5418 | if (kvm_enabled()) { | |
5419 | int ret; | |
5420 | ||
5421 | ret = kvm_sync_vcpus(); | |
5422 | if (ret < 0) { | |
5423 | fprintf(stderr, "failed to initialize vcpus\n"); | |
5424 | exit(1); | |
5425 | } | |
5426 | } | |
5427 | ||
5428 | /* init USB devices */ | |
5429 | if (usb_enabled) { | |
5430 | for(i = 0; i < usb_devices_index; i++) { | |
c0f4ce77 | 5431 | if (usb_device_add(usb_devices[i], 0) < 0) { |
3023f332 AL |
5432 | fprintf(stderr, "Warning: could not add USB device %s\n", |
5433 | usb_devices[i]); | |
5434 | } | |
5435 | } | |
5436 | } | |
5437 | ||
8f391ab4 AL |
5438 | if (!display_state) |
5439 | dumb_display_init(); | |
3023f332 AL |
5440 | /* just use the first displaystate for the moment */ |
5441 | ds = display_state; | |
313aa567 | 5442 | /* terminal init */ |
a20dd508 | 5443 | if (nographic) { |
4d3b6f6e AZ |
5444 | if (curses) { |
5445 | fprintf(stderr, "fatal: -nographic can't be used with -curses\n"); | |
5446 | exit(1); | |
5447 | } | |
7d957bd8 | 5448 | } else { |
4d3b6f6e | 5449 | #if defined(CONFIG_CURSES) |
7d957bd8 AL |
5450 | if (curses) { |
5451 | /* At the moment curses cannot be used with other displays */ | |
5452 | curses_display_init(ds, full_screen); | |
5453 | } else | |
4d3b6f6e | 5454 | #endif |
7d957bd8 AL |
5455 | { |
5456 | if (vnc_display != NULL) { | |
5457 | vnc_display_init(ds); | |
5458 | if (vnc_display_open(ds, vnc_display) < 0) | |
5459 | exit(1); | |
5460 | } | |
5b0753e0 | 5461 | #if defined(CONFIG_SDL) |
d268de04 | 5462 | if (sdl || !vnc_display) |
7d957bd8 | 5463 | sdl_display_init(ds, full_screen, no_frame); |
5b0753e0 | 5464 | #elif defined(CONFIG_COCOA) |
d268de04 | 5465 | if (sdl || !vnc_display) |
7d957bd8 | 5466 | cocoa_display_init(ds, full_screen); |
313aa567 | 5467 | #endif |
7d957bd8 | 5468 | } |
313aa567 | 5469 | } |
7d957bd8 | 5470 | dpy_resize(ds); |
5b08fc10 | 5471 | |
3023f332 AL |
5472 | dcl = ds->listeners; |
5473 | while (dcl != NULL) { | |
5474 | if (dcl->dpy_refresh != NULL) { | |
5475 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); | |
5476 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); | |
20d8a3ed | 5477 | } |
3023f332 | 5478 | dcl = dcl->next; |
20d8a3ed | 5479 | } |
3023f332 | 5480 | |
9043b62d BS |
5481 | if (nographic || (vnc_display && !sdl)) { |
5482 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); | |
5483 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); | |
5484 | } | |
5485 | ||
2796dae0 | 5486 | text_consoles_set_display(display_state); |
2970a6c9 | 5487 | qemu_chr_initial_reset(); |
2796dae0 | 5488 | |
4c621805 | 5489 | if (monitor_device && monitor_hd) |
cde76ee1 | 5490 | monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT); |
82c643ff | 5491 | |
8d11df9e | 5492 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
c03b0f0f | 5493 | const char *devname = serial_devices[i]; |
fd5f393a | 5494 | if (devname && strcmp(devname, "none")) { |
5ccfae10 AL |
5495 | char label[32]; |
5496 | snprintf(label, sizeof(label), "serial%d", i); | |
af3a9031 | 5497 | if (strstart(devname, "vc", 0)) |
7ba1260a | 5498 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
8d11df9e | 5499 | } |
82c643ff | 5500 | } |
82c643ff | 5501 | |
6508fe59 | 5502 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
c03b0f0f | 5503 | const char *devname = parallel_devices[i]; |
fd5f393a | 5504 | if (devname && strcmp(devname, "none")) { |
5ccfae10 AL |
5505 | char label[32]; |
5506 | snprintf(label, sizeof(label), "parallel%d", i); | |
af3a9031 | 5507 | if (strstart(devname, "vc", 0)) |
7ba1260a | 5508 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
6508fe59 FB |
5509 | } |
5510 | } | |
5511 | ||
9ede2fde AL |
5512 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5513 | const char *devname = virtio_consoles[i]; | |
2796dae0 | 5514 | if (virtcon_hds[i] && devname) { |
9ede2fde AL |
5515 | char label[32]; |
5516 | snprintf(label, sizeof(label), "virtcon%d", i); | |
9ede2fde AL |
5517 | if (strstart(devname, "vc", 0)) |
5518 | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i); | |
5519 | } | |
5520 | } | |
5521 | ||
67b915a5 | 5522 | #ifdef CONFIG_GDBSTUB |
59030a8c AL |
5523 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
5524 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", | |
5525 | gdbstub_dev); | |
5526 | exit(1); | |
45669e00 | 5527 | } |
67b915a5 | 5528 | #endif |
45669e00 | 5529 | |
d63d307f | 5530 | if (loadvm) |
376253ec | 5531 | do_loadvm(cur_mon, loadvm); |
d63d307f | 5532 | |
5bb7910a AL |
5533 | if (incoming) { |
5534 | autostart = 0; /* fixme how to deal with -daemonize */ | |
5535 | qemu_start_incoming_migration(incoming); | |
5536 | } | |
5537 | ||
c0f4ce77 AL |
5538 | if (autostart) |
5539 | vm_start(); | |
ffd843bc | 5540 | |
b9e82a59 | 5541 | #ifndef _WIN32 |
71e3ceb8 TS |
5542 | if (daemonize) { |
5543 | uint8_t status = 0; | |
5544 | ssize_t len; | |
71e3ceb8 TS |
5545 | |
5546 | again1: | |
5547 | len = write(fds[1], &status, 1); | |
5548 | if (len == -1 && (errno == EINTR)) | |
5549 | goto again1; | |
5550 | ||
5551 | if (len != 1) | |
5552 | exit(1); | |
5553 | ||
bd54b863 | 5554 | chdir("/"); |
aeb30be6 | 5555 | TFR(fd = open("/dev/null", O_RDWR)); |
71e3ceb8 TS |
5556 | if (fd == -1) |
5557 | exit(1); | |
0858532e | 5558 | } |
71e3ceb8 | 5559 | |
0858532e AL |
5560 | if (run_as) { |
5561 | pwd = getpwnam(run_as); | |
5562 | if (!pwd) { | |
5563 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); | |
5564 | exit(1); | |
5565 | } | |
5566 | } | |
5567 | ||
5568 | if (chroot_dir) { | |
5569 | if (chroot(chroot_dir) < 0) { | |
5570 | fprintf(stderr, "chroot failed\n"); | |
5571 | exit(1); | |
5572 | } | |
5573 | chdir("/"); | |
5574 | } | |
5575 | ||
5576 | if (run_as) { | |
5577 | if (setgid(pwd->pw_gid) < 0) { | |
5578 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); | |
5579 | exit(1); | |
5580 | } | |
5581 | if (setuid(pwd->pw_uid) < 0) { | |
5582 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); | |
5583 | exit(1); | |
5584 | } | |
5585 | if (setuid(0) != -1) { | |
5586 | fprintf(stderr, "Dropping privileges failed\n"); | |
5587 | exit(1); | |
5588 | } | |
5589 | } | |
0858532e AL |
5590 | |
5591 | if (daemonize) { | |
5592 | dup2(fd, 0); | |
5593 | dup2(fd, 1); | |
5594 | dup2(fd, 2); | |
71e3ceb8 | 5595 | |
0858532e | 5596 | close(fd); |
71e3ceb8 | 5597 | } |
b9e82a59 | 5598 | #endif |
71e3ceb8 | 5599 | |
8a7ddc38 | 5600 | main_loop(); |
40c3bac3 | 5601 | quit_timers(); |
63a01ef8 | 5602 | net_cleanup(); |
b46a8906 | 5603 | |
0824d6fc FB |
5604 | return 0; |
5605 | } |