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