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