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