]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2004 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 "vl.h" | |
25 | ||
26 | #include <unistd.h> | |
27 | #include <fcntl.h> | |
28 | #include <signal.h> | |
29 | #include <time.h> | |
30 | #include <errno.h> | |
31 | #include <sys/time.h> | |
32 | ||
33 | #ifndef _WIN32 | |
34 | #include <sys/times.h> | |
35 | #include <sys/wait.h> | |
36 | #include <termios.h> | |
37 | #include <sys/poll.h> | |
38 | #include <sys/mman.h> | |
39 | #include <sys/ioctl.h> | |
40 | #include <sys/socket.h> | |
41 | #include <netinet/in.h> | |
42 | #include <dirent.h> | |
43 | #ifdef _BSD | |
44 | #include <sys/stat.h> | |
45 | #ifndef __APPLE__ | |
46 | #include <libutil.h> | |
47 | #endif | |
48 | #else | |
49 | #include <linux/if.h> | |
50 | #include <linux/if_tun.h> | |
51 | #include <pty.h> | |
52 | #include <malloc.h> | |
53 | #include <linux/rtc.h> | |
54 | #endif | |
55 | #endif | |
56 | ||
57 | #if defined(CONFIG_SLIRP) | |
58 | #include "libslirp.h" | |
59 | #endif | |
60 | ||
61 | #ifdef _WIN32 | |
62 | #include <malloc.h> | |
63 | #include <sys/timeb.h> | |
64 | #include <windows.h> | |
65 | #define getopt_long_only getopt_long | |
66 | #define memalign(align, size) malloc(size) | |
67 | #endif | |
68 | ||
69 | #ifdef CONFIG_SDL | |
70 | #ifdef __APPLE__ | |
71 | #include <SDL/SDL.h> | |
72 | #endif | |
73 | #endif /* CONFIG_SDL */ | |
74 | ||
75 | #include "disas.h" | |
76 | ||
77 | #include "exec-all.h" | |
78 | ||
79 | //#define DO_TB_FLUSH | |
80 | ||
81 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" | |
82 | ||
83 | //#define DEBUG_UNUSED_IOPORT | |
84 | //#define DEBUG_IOPORT | |
85 | ||
86 | #if !defined(CONFIG_SOFTMMU) | |
87 | #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) | |
88 | #else | |
89 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) | |
90 | #endif | |
91 | ||
92 | #ifdef TARGET_PPC | |
93 | #define DEFAULT_RAM_SIZE 144 | |
94 | #else | |
95 | #define DEFAULT_RAM_SIZE 128 | |
96 | #endif | |
97 | /* in ms */ | |
98 | #define GUI_REFRESH_INTERVAL 30 | |
99 | ||
100 | /* XXX: use a two level table to limit memory usage */ | |
101 | #define MAX_IOPORTS 65536 | |
102 | ||
103 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; | |
104 | char phys_ram_file[1024]; | |
105 | CPUState *global_env; | |
106 | CPUState *cpu_single_env; | |
107 | void *ioport_opaque[MAX_IOPORTS]; | |
108 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; | |
109 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; | |
110 | BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; | |
111 | int vga_ram_size; | |
112 | int bios_size; | |
113 | static DisplayState display_state; | |
114 | int nographic; | |
115 | int64_t ticks_per_sec; | |
116 | int boot_device = 'c'; | |
117 | int ram_size; | |
118 | static char network_script[1024]; | |
119 | int pit_min_timer_count = 0; | |
120 | int nb_nics; | |
121 | NetDriverState nd_table[MAX_NICS]; | |
122 | QEMUTimer *gui_timer; | |
123 | int vm_running; | |
124 | int audio_enabled = 0; | |
125 | int sb16_enabled = 1; | |
126 | int adlib_enabled = 1; | |
127 | int gus_enabled = 1; | |
128 | int pci_enabled = 1; | |
129 | int prep_enabled = 0; | |
130 | int rtc_utc = 1; | |
131 | int cirrus_vga_enabled = 1; | |
132 | int graphic_width = 800; | |
133 | int graphic_height = 600; | |
134 | int graphic_depth = 15; | |
135 | int full_screen = 0; | |
136 | TextConsole *vga_console; | |
137 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; | |
138 | ||
139 | /***********************************************************/ | |
140 | /* x86 ISA bus support */ | |
141 | ||
142 | target_phys_addr_t isa_mem_base = 0; | |
143 | ||
144 | uint32_t default_ioport_readb(void *opaque, uint32_t address) | |
145 | { | |
146 | #ifdef DEBUG_UNUSED_IOPORT | |
147 | fprintf(stderr, "inb: port=0x%04x\n", address); | |
148 | #endif | |
149 | return 0xff; | |
150 | } | |
151 | ||
152 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) | |
153 | { | |
154 | #ifdef DEBUG_UNUSED_IOPORT | |
155 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); | |
156 | #endif | |
157 | } | |
158 | ||
159 | /* default is to make two byte accesses */ | |
160 | uint32_t default_ioport_readw(void *opaque, uint32_t address) | |
161 | { | |
162 | uint32_t data; | |
163 | data = ioport_read_table[0][address](ioport_opaque[address], address); | |
164 | address = (address + 1) & (MAX_IOPORTS - 1); | |
165 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; | |
166 | return data; | |
167 | } | |
168 | ||
169 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) | |
170 | { | |
171 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); | |
172 | address = (address + 1) & (MAX_IOPORTS - 1); | |
173 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); | |
174 | } | |
175 | ||
176 | uint32_t default_ioport_readl(void *opaque, uint32_t address) | |
177 | { | |
178 | #ifdef DEBUG_UNUSED_IOPORT | |
179 | fprintf(stderr, "inl: port=0x%04x\n", address); | |
180 | #endif | |
181 | return 0xffffffff; | |
182 | } | |
183 | ||
184 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) | |
185 | { | |
186 | #ifdef DEBUG_UNUSED_IOPORT | |
187 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); | |
188 | #endif | |
189 | } | |
190 | ||
191 | void init_ioports(void) | |
192 | { | |
193 | int i; | |
194 | ||
195 | for(i = 0; i < MAX_IOPORTS; i++) { | |
196 | ioport_read_table[0][i] = default_ioport_readb; | |
197 | ioport_write_table[0][i] = default_ioport_writeb; | |
198 | ioport_read_table[1][i] = default_ioport_readw; | |
199 | ioport_write_table[1][i] = default_ioport_writew; | |
200 | ioport_read_table[2][i] = default_ioport_readl; | |
201 | ioport_write_table[2][i] = default_ioport_writel; | |
202 | } | |
203 | } | |
204 | ||
205 | /* size is the word size in byte */ | |
206 | int register_ioport_read(int start, int length, int size, | |
207 | IOPortReadFunc *func, void *opaque) | |
208 | { | |
209 | int i, bsize; | |
210 | ||
211 | if (size == 1) { | |
212 | bsize = 0; | |
213 | } else if (size == 2) { | |
214 | bsize = 1; | |
215 | } else if (size == 4) { | |
216 | bsize = 2; | |
217 | } else { | |
218 | hw_error("register_ioport_read: invalid size"); | |
219 | return -1; | |
220 | } | |
221 | for(i = start; i < start + length; i += size) { | |
222 | ioport_read_table[bsize][i] = func; | |
223 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) | |
224 | hw_error("register_ioport_read: invalid opaque"); | |
225 | ioport_opaque[i] = opaque; | |
226 | } | |
227 | return 0; | |
228 | } | |
229 | ||
230 | /* size is the word size in byte */ | |
231 | int register_ioport_write(int start, int length, int size, | |
232 | IOPortWriteFunc *func, void *opaque) | |
233 | { | |
234 | int i, bsize; | |
235 | ||
236 | if (size == 1) { | |
237 | bsize = 0; | |
238 | } else if (size == 2) { | |
239 | bsize = 1; | |
240 | } else if (size == 4) { | |
241 | bsize = 2; | |
242 | } else { | |
243 | hw_error("register_ioport_write: invalid size"); | |
244 | return -1; | |
245 | } | |
246 | for(i = start; i < start + length; i += size) { | |
247 | ioport_write_table[bsize][i] = func; | |
248 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) | |
249 | hw_error("register_ioport_read: invalid opaque"); | |
250 | ioport_opaque[i] = opaque; | |
251 | } | |
252 | return 0; | |
253 | } | |
254 | ||
255 | void isa_unassign_ioport(int start, int length) | |
256 | { | |
257 | int i; | |
258 | ||
259 | for(i = start; i < start + length; i++) { | |
260 | ioport_read_table[0][i] = default_ioport_readb; | |
261 | ioport_read_table[1][i] = default_ioport_readw; | |
262 | ioport_read_table[2][i] = default_ioport_readl; | |
263 | ||
264 | ioport_write_table[0][i] = default_ioport_writeb; | |
265 | ioport_write_table[1][i] = default_ioport_writew; | |
266 | ioport_write_table[2][i] = default_ioport_writel; | |
267 | } | |
268 | } | |
269 | ||
270 | void pstrcpy(char *buf, int buf_size, const char *str) | |
271 | { | |
272 | int c; | |
273 | char *q = buf; | |
274 | ||
275 | if (buf_size <= 0) | |
276 | return; | |
277 | ||
278 | for(;;) { | |
279 | c = *str++; | |
280 | if (c == 0 || q >= buf + buf_size - 1) | |
281 | break; | |
282 | *q++ = c; | |
283 | } | |
284 | *q = '\0'; | |
285 | } | |
286 | ||
287 | /* strcat and truncate. */ | |
288 | char *pstrcat(char *buf, int buf_size, const char *s) | |
289 | { | |
290 | int len; | |
291 | len = strlen(buf); | |
292 | if (len < buf_size) | |
293 | pstrcpy(buf + len, buf_size - len, s); | |
294 | return buf; | |
295 | } | |
296 | ||
297 | int strstart(const char *str, const char *val, const char **ptr) | |
298 | { | |
299 | const char *p, *q; | |
300 | p = str; | |
301 | q = val; | |
302 | while (*q != '\0') { | |
303 | if (*p != *q) | |
304 | return 0; | |
305 | p++; | |
306 | q++; | |
307 | } | |
308 | if (ptr) | |
309 | *ptr = p; | |
310 | return 1; | |
311 | } | |
312 | ||
313 | /* return the size or -1 if error */ | |
314 | int get_image_size(const char *filename) | |
315 | { | |
316 | int fd, size; | |
317 | fd = open(filename, O_RDONLY | O_BINARY); | |
318 | if (fd < 0) | |
319 | return -1; | |
320 | size = lseek(fd, 0, SEEK_END); | |
321 | close(fd); | |
322 | return size; | |
323 | } | |
324 | ||
325 | /* return the size or -1 if error */ | |
326 | int load_image(const char *filename, uint8_t *addr) | |
327 | { | |
328 | int fd, size; | |
329 | fd = open(filename, O_RDONLY | O_BINARY); | |
330 | if (fd < 0) | |
331 | return -1; | |
332 | size = lseek(fd, 0, SEEK_END); | |
333 | lseek(fd, 0, SEEK_SET); | |
334 | if (read(fd, addr, size) != size) { | |
335 | close(fd); | |
336 | return -1; | |
337 | } | |
338 | close(fd); | |
339 | return size; | |
340 | } | |
341 | ||
342 | void cpu_outb(CPUState *env, int addr, int val) | |
343 | { | |
344 | #ifdef DEBUG_IOPORT | |
345 | if (loglevel & CPU_LOG_IOPORT) | |
346 | fprintf(logfile, "outb: %04x %02x\n", addr, val); | |
347 | #endif | |
348 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); | |
349 | } | |
350 | ||
351 | void cpu_outw(CPUState *env, int addr, int val) | |
352 | { | |
353 | #ifdef DEBUG_IOPORT | |
354 | if (loglevel & CPU_LOG_IOPORT) | |
355 | fprintf(logfile, "outw: %04x %04x\n", addr, val); | |
356 | #endif | |
357 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); | |
358 | } | |
359 | ||
360 | void cpu_outl(CPUState *env, int addr, int val) | |
361 | { | |
362 | #ifdef DEBUG_IOPORT | |
363 | if (loglevel & CPU_LOG_IOPORT) | |
364 | fprintf(logfile, "outl: %04x %08x\n", addr, val); | |
365 | #endif | |
366 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); | |
367 | } | |
368 | ||
369 | int cpu_inb(CPUState *env, int addr) | |
370 | { | |
371 | int val; | |
372 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); | |
373 | #ifdef DEBUG_IOPORT | |
374 | if (loglevel & CPU_LOG_IOPORT) | |
375 | fprintf(logfile, "inb : %04x %02x\n", addr, val); | |
376 | #endif | |
377 | return val; | |
378 | } | |
379 | ||
380 | int cpu_inw(CPUState *env, int addr) | |
381 | { | |
382 | int val; | |
383 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); | |
384 | #ifdef DEBUG_IOPORT | |
385 | if (loglevel & CPU_LOG_IOPORT) | |
386 | fprintf(logfile, "inw : %04x %04x\n", addr, val); | |
387 | #endif | |
388 | return val; | |
389 | } | |
390 | ||
391 | int cpu_inl(CPUState *env, int addr) | |
392 | { | |
393 | int val; | |
394 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); | |
395 | #ifdef DEBUG_IOPORT | |
396 | if (loglevel & CPU_LOG_IOPORT) | |
397 | fprintf(logfile, "inl : %04x %08x\n", addr, val); | |
398 | #endif | |
399 | return val; | |
400 | } | |
401 | ||
402 | /***********************************************************/ | |
403 | void hw_error(const char *fmt, ...) | |
404 | { | |
405 | va_list ap; | |
406 | ||
407 | va_start(ap, fmt); | |
408 | fprintf(stderr, "qemu: hardware error: "); | |
409 | vfprintf(stderr, fmt, ap); | |
410 | fprintf(stderr, "\n"); | |
411 | #ifdef TARGET_I386 | |
412 | cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); | |
413 | #else | |
414 | cpu_dump_state(global_env, stderr, fprintf, 0); | |
415 | #endif | |
416 | va_end(ap); | |
417 | abort(); | |
418 | } | |
419 | ||
420 | /***********************************************************/ | |
421 | /* keyboard/mouse */ | |
422 | ||
423 | static QEMUPutKBDEvent *qemu_put_kbd_event; | |
424 | static void *qemu_put_kbd_event_opaque; | |
425 | static QEMUPutMouseEvent *qemu_put_mouse_event; | |
426 | static void *qemu_put_mouse_event_opaque; | |
427 | ||
428 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) | |
429 | { | |
430 | qemu_put_kbd_event_opaque = opaque; | |
431 | qemu_put_kbd_event = func; | |
432 | } | |
433 | ||
434 | void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque) | |
435 | { | |
436 | qemu_put_mouse_event_opaque = opaque; | |
437 | qemu_put_mouse_event = func; | |
438 | } | |
439 | ||
440 | void kbd_put_keycode(int keycode) | |
441 | { | |
442 | if (qemu_put_kbd_event) { | |
443 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); | |
444 | } | |
445 | } | |
446 | ||
447 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) | |
448 | { | |
449 | if (qemu_put_mouse_event) { | |
450 | qemu_put_mouse_event(qemu_put_mouse_event_opaque, | |
451 | dx, dy, dz, buttons_state); | |
452 | } | |
453 | } | |
454 | ||
455 | /***********************************************************/ | |
456 | /* timers */ | |
457 | ||
458 | #if defined(__powerpc__) | |
459 | ||
460 | static inline uint32_t get_tbl(void) | |
461 | { | |
462 | uint32_t tbl; | |
463 | asm volatile("mftb %0" : "=r" (tbl)); | |
464 | return tbl; | |
465 | } | |
466 | ||
467 | static inline uint32_t get_tbu(void) | |
468 | { | |
469 | uint32_t tbl; | |
470 | asm volatile("mftbu %0" : "=r" (tbl)); | |
471 | return tbl; | |
472 | } | |
473 | ||
474 | int64_t cpu_get_real_ticks(void) | |
475 | { | |
476 | uint32_t l, h, h1; | |
477 | /* NOTE: we test if wrapping has occurred */ | |
478 | do { | |
479 | h = get_tbu(); | |
480 | l = get_tbl(); | |
481 | h1 = get_tbu(); | |
482 | } while (h != h1); | |
483 | return ((int64_t)h << 32) | l; | |
484 | } | |
485 | ||
486 | #elif defined(__i386__) | |
487 | ||
488 | int64_t cpu_get_real_ticks(void) | |
489 | { | |
490 | int64_t val; | |
491 | asm volatile ("rdtsc" : "=A" (val)); | |
492 | return val; | |
493 | } | |
494 | ||
495 | #elif defined(__x86_64__) | |
496 | ||
497 | int64_t cpu_get_real_ticks(void) | |
498 | { | |
499 | uint32_t low,high; | |
500 | int64_t val; | |
501 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | |
502 | val = high; | |
503 | val <<= 32; | |
504 | val |= low; | |
505 | return val; | |
506 | } | |
507 | ||
508 | #else | |
509 | #error unsupported CPU | |
510 | #endif | |
511 | ||
512 | static int64_t cpu_ticks_offset; | |
513 | static int cpu_ticks_enabled; | |
514 | ||
515 | static inline int64_t cpu_get_ticks(void) | |
516 | { | |
517 | if (!cpu_ticks_enabled) { | |
518 | return cpu_ticks_offset; | |
519 | } else { | |
520 | return cpu_get_real_ticks() + cpu_ticks_offset; | |
521 | } | |
522 | } | |
523 | ||
524 | /* enable cpu_get_ticks() */ | |
525 | void cpu_enable_ticks(void) | |
526 | { | |
527 | if (!cpu_ticks_enabled) { | |
528 | cpu_ticks_offset -= cpu_get_real_ticks(); | |
529 | cpu_ticks_enabled = 1; | |
530 | } | |
531 | } | |
532 | ||
533 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
534 | cpu_get_ticks() after that. */ | |
535 | void cpu_disable_ticks(void) | |
536 | { | |
537 | if (cpu_ticks_enabled) { | |
538 | cpu_ticks_offset = cpu_get_ticks(); | |
539 | cpu_ticks_enabled = 0; | |
540 | } | |
541 | } | |
542 | ||
543 | static int64_t get_clock(void) | |
544 | { | |
545 | #ifdef _WIN32 | |
546 | struct _timeb tb; | |
547 | _ftime(&tb); | |
548 | return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000; | |
549 | #else | |
550 | struct timeval tv; | |
551 | gettimeofday(&tv, NULL); | |
552 | return tv.tv_sec * 1000000LL + tv.tv_usec; | |
553 | #endif | |
554 | } | |
555 | ||
556 | void cpu_calibrate_ticks(void) | |
557 | { | |
558 | int64_t usec, ticks; | |
559 | ||
560 | usec = get_clock(); | |
561 | ticks = cpu_get_real_ticks(); | |
562 | #ifdef _WIN32 | |
563 | Sleep(50); | |
564 | #else | |
565 | usleep(50 * 1000); | |
566 | #endif | |
567 | usec = get_clock() - usec; | |
568 | ticks = cpu_get_real_ticks() - ticks; | |
569 | ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; | |
570 | } | |
571 | ||
572 | /* compute with 96 bit intermediate result: (a*b)/c */ | |
573 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) | |
574 | { | |
575 | union { | |
576 | uint64_t ll; | |
577 | struct { | |
578 | #ifdef WORDS_BIGENDIAN | |
579 | uint32_t high, low; | |
580 | #else | |
581 | uint32_t low, high; | |
582 | #endif | |
583 | } l; | |
584 | } u, res; | |
585 | uint64_t rl, rh; | |
586 | ||
587 | u.ll = a; | |
588 | rl = (uint64_t)u.l.low * (uint64_t)b; | |
589 | rh = (uint64_t)u.l.high * (uint64_t)b; | |
590 | rh += (rl >> 32); | |
591 | res.l.high = rh / c; | |
592 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; | |
593 | return res.ll; | |
594 | } | |
595 | ||
596 | #define QEMU_TIMER_REALTIME 0 | |
597 | #define QEMU_TIMER_VIRTUAL 1 | |
598 | ||
599 | struct QEMUClock { | |
600 | int type; | |
601 | /* XXX: add frequency */ | |
602 | }; | |
603 | ||
604 | struct QEMUTimer { | |
605 | QEMUClock *clock; | |
606 | int64_t expire_time; | |
607 | QEMUTimerCB *cb; | |
608 | void *opaque; | |
609 | struct QEMUTimer *next; | |
610 | }; | |
611 | ||
612 | QEMUClock *rt_clock; | |
613 | QEMUClock *vm_clock; | |
614 | ||
615 | static QEMUTimer *active_timers[2]; | |
616 | #ifdef _WIN32 | |
617 | static MMRESULT timerID; | |
618 | #else | |
619 | /* frequency of the times() clock tick */ | |
620 | static int timer_freq; | |
621 | #endif | |
622 | ||
623 | QEMUClock *qemu_new_clock(int type) | |
624 | { | |
625 | QEMUClock *clock; | |
626 | clock = qemu_mallocz(sizeof(QEMUClock)); | |
627 | if (!clock) | |
628 | return NULL; | |
629 | clock->type = type; | |
630 | return clock; | |
631 | } | |
632 | ||
633 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) | |
634 | { | |
635 | QEMUTimer *ts; | |
636 | ||
637 | ts = qemu_mallocz(sizeof(QEMUTimer)); | |
638 | ts->clock = clock; | |
639 | ts->cb = cb; | |
640 | ts->opaque = opaque; | |
641 | return ts; | |
642 | } | |
643 | ||
644 | void qemu_free_timer(QEMUTimer *ts) | |
645 | { | |
646 | qemu_free(ts); | |
647 | } | |
648 | ||
649 | /* stop a timer, but do not dealloc it */ | |
650 | void qemu_del_timer(QEMUTimer *ts) | |
651 | { | |
652 | QEMUTimer **pt, *t; | |
653 | ||
654 | /* NOTE: this code must be signal safe because | |
655 | qemu_timer_expired() can be called from a signal. */ | |
656 | pt = &active_timers[ts->clock->type]; | |
657 | for(;;) { | |
658 | t = *pt; | |
659 | if (!t) | |
660 | break; | |
661 | if (t == ts) { | |
662 | *pt = t->next; | |
663 | break; | |
664 | } | |
665 | pt = &t->next; | |
666 | } | |
667 | } | |
668 | ||
669 | /* modify the current timer so that it will be fired when current_time | |
670 | >= expire_time. The corresponding callback will be called. */ | |
671 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) | |
672 | { | |
673 | QEMUTimer **pt, *t; | |
674 | ||
675 | qemu_del_timer(ts); | |
676 | ||
677 | /* add the timer in the sorted list */ | |
678 | /* NOTE: this code must be signal safe because | |
679 | qemu_timer_expired() can be called from a signal. */ | |
680 | pt = &active_timers[ts->clock->type]; | |
681 | for(;;) { | |
682 | t = *pt; | |
683 | if (!t) | |
684 | break; | |
685 | if (t->expire_time > expire_time) | |
686 | break; | |
687 | pt = &t->next; | |
688 | } | |
689 | ts->expire_time = expire_time; | |
690 | ts->next = *pt; | |
691 | *pt = ts; | |
692 | } | |
693 | ||
694 | int qemu_timer_pending(QEMUTimer *ts) | |
695 | { | |
696 | QEMUTimer *t; | |
697 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { | |
698 | if (t == ts) | |
699 | return 1; | |
700 | } | |
701 | return 0; | |
702 | } | |
703 | ||
704 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) | |
705 | { | |
706 | if (!timer_head) | |
707 | return 0; | |
708 | return (timer_head->expire_time <= current_time); | |
709 | } | |
710 | ||
711 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) | |
712 | { | |
713 | QEMUTimer *ts; | |
714 | ||
715 | for(;;) { | |
716 | ts = *ptimer_head; | |
717 | if (!ts || ts->expire_time > current_time) | |
718 | break; | |
719 | /* remove timer from the list before calling the callback */ | |
720 | *ptimer_head = ts->next; | |
721 | ts->next = NULL; | |
722 | ||
723 | /* run the callback (the timer list can be modified) */ | |
724 | ts->cb(ts->opaque); | |
725 | } | |
726 | } | |
727 | ||
728 | int64_t qemu_get_clock(QEMUClock *clock) | |
729 | { | |
730 | switch(clock->type) { | |
731 | case QEMU_TIMER_REALTIME: | |
732 | #ifdef _WIN32 | |
733 | return GetTickCount(); | |
734 | #else | |
735 | { | |
736 | struct tms tp; | |
737 | ||
738 | /* Note that using gettimeofday() is not a good solution | |
739 | for timers because its value change when the date is | |
740 | modified. */ | |
741 | if (timer_freq == 100) { | |
742 | return times(&tp) * 10; | |
743 | } else { | |
744 | return ((int64_t)times(&tp) * 1000) / timer_freq; | |
745 | } | |
746 | } | |
747 | #endif | |
748 | default: | |
749 | case QEMU_TIMER_VIRTUAL: | |
750 | return cpu_get_ticks(); | |
751 | } | |
752 | } | |
753 | ||
754 | /* save a timer */ | |
755 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) | |
756 | { | |
757 | uint64_t expire_time; | |
758 | ||
759 | if (qemu_timer_pending(ts)) { | |
760 | expire_time = ts->expire_time; | |
761 | } else { | |
762 | expire_time = -1; | |
763 | } | |
764 | qemu_put_be64(f, expire_time); | |
765 | } | |
766 | ||
767 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) | |
768 | { | |
769 | uint64_t expire_time; | |
770 | ||
771 | expire_time = qemu_get_be64(f); | |
772 | if (expire_time != -1) { | |
773 | qemu_mod_timer(ts, expire_time); | |
774 | } else { | |
775 | qemu_del_timer(ts); | |
776 | } | |
777 | } | |
778 | ||
779 | static void timer_save(QEMUFile *f, void *opaque) | |
780 | { | |
781 | if (cpu_ticks_enabled) { | |
782 | hw_error("cannot save state if virtual timers are running"); | |
783 | } | |
784 | qemu_put_be64s(f, &cpu_ticks_offset); | |
785 | qemu_put_be64s(f, &ticks_per_sec); | |
786 | } | |
787 | ||
788 | static int timer_load(QEMUFile *f, void *opaque, int version_id) | |
789 | { | |
790 | if (version_id != 1) | |
791 | return -EINVAL; | |
792 | if (cpu_ticks_enabled) { | |
793 | return -EINVAL; | |
794 | } | |
795 | qemu_get_be64s(f, &cpu_ticks_offset); | |
796 | qemu_get_be64s(f, &ticks_per_sec); | |
797 | return 0; | |
798 | } | |
799 | ||
800 | #ifdef _WIN32 | |
801 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, | |
802 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) | |
803 | #else | |
804 | static void host_alarm_handler(int host_signum) | |
805 | #endif | |
806 | { | |
807 | #if 0 | |
808 | #define DISP_FREQ 1000 | |
809 | { | |
810 | static int64_t delta_min = INT64_MAX; | |
811 | static int64_t delta_max, delta_cum, last_clock, delta, ti; | |
812 | static int count; | |
813 | ti = qemu_get_clock(vm_clock); | |
814 | if (last_clock != 0) { | |
815 | delta = ti - last_clock; | |
816 | if (delta < delta_min) | |
817 | delta_min = delta; | |
818 | if (delta > delta_max) | |
819 | delta_max = delta; | |
820 | delta_cum += delta; | |
821 | if (++count == DISP_FREQ) { | |
822 | printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n", | |
823 | muldiv64(delta_min, 1000000, ticks_per_sec), | |
824 | muldiv64(delta_max, 1000000, ticks_per_sec), | |
825 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), | |
826 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); | |
827 | count = 0; | |
828 | delta_min = INT64_MAX; | |
829 | delta_max = 0; | |
830 | delta_cum = 0; | |
831 | } | |
832 | } | |
833 | last_clock = ti; | |
834 | } | |
835 | #endif | |
836 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], | |
837 | qemu_get_clock(vm_clock)) || | |
838 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], | |
839 | qemu_get_clock(rt_clock))) { | |
840 | /* stop the cpu because a timer occured */ | |
841 | cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); | |
842 | } | |
843 | } | |
844 | ||
845 | #ifndef _WIN32 | |
846 | ||
847 | #if defined(__linux__) | |
848 | ||
849 | #define RTC_FREQ 1024 | |
850 | ||
851 | static int rtc_fd; | |
852 | ||
853 | static int start_rtc_timer(void) | |
854 | { | |
855 | rtc_fd = open("/dev/rtc", O_RDONLY); | |
856 | if (rtc_fd < 0) | |
857 | return -1; | |
858 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { | |
859 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" | |
860 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" | |
861 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); | |
862 | goto fail; | |
863 | } | |
864 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { | |
865 | fail: | |
866 | close(rtc_fd); | |
867 | return -1; | |
868 | } | |
869 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; | |
870 | return 0; | |
871 | } | |
872 | ||
873 | #else | |
874 | ||
875 | static int start_rtc_timer(void) | |
876 | { | |
877 | return -1; | |
878 | } | |
879 | ||
880 | #endif /* !defined(__linux__) */ | |
881 | ||
882 | #endif /* !defined(_WIN32) */ | |
883 | ||
884 | static void init_timers(void) | |
885 | { | |
886 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); | |
887 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); | |
888 | ||
889 | #ifdef _WIN32 | |
890 | { | |
891 | int count=0; | |
892 | timerID = timeSetEvent(10, // interval (ms) | |
893 | 0, // resolution | |
894 | host_alarm_handler, // function | |
895 | (DWORD)&count, // user parameter | |
896 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); | |
897 | if( !timerID ) { | |
898 | perror("failed timer alarm"); | |
899 | exit(1); | |
900 | } | |
901 | } | |
902 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; | |
903 | #else | |
904 | { | |
905 | struct sigaction act; | |
906 | struct itimerval itv; | |
907 | ||
908 | /* get times() syscall frequency */ | |
909 | timer_freq = sysconf(_SC_CLK_TCK); | |
910 | ||
911 | /* timer signal */ | |
912 | sigfillset(&act.sa_mask); | |
913 | act.sa_flags = 0; | |
914 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) | |
915 | act.sa_flags |= SA_ONSTACK; | |
916 | #endif | |
917 | act.sa_handler = host_alarm_handler; | |
918 | sigaction(SIGALRM, &act, NULL); | |
919 | ||
920 | itv.it_interval.tv_sec = 0; | |
921 | itv.it_interval.tv_usec = 1000; | |
922 | itv.it_value.tv_sec = 0; | |
923 | itv.it_value.tv_usec = 10 * 1000; | |
924 | setitimer(ITIMER_REAL, &itv, NULL); | |
925 | /* we probe the tick duration of the kernel to inform the user if | |
926 | the emulated kernel requested a too high timer frequency */ | |
927 | getitimer(ITIMER_REAL, &itv); | |
928 | ||
929 | #if defined(__linux__) | |
930 | if (itv.it_interval.tv_usec > 1000) { | |
931 | /* try to use /dev/rtc to have a faster timer */ | |
932 | if (start_rtc_timer() < 0) | |
933 | goto use_itimer; | |
934 | /* disable itimer */ | |
935 | itv.it_interval.tv_sec = 0; | |
936 | itv.it_interval.tv_usec = 0; | |
937 | itv.it_value.tv_sec = 0; | |
938 | itv.it_value.tv_usec = 0; | |
939 | setitimer(ITIMER_REAL, &itv, NULL); | |
940 | ||
941 | /* use the RTC */ | |
942 | sigaction(SIGIO, &act, NULL); | |
943 | fcntl(rtc_fd, F_SETFL, O_ASYNC); | |
944 | fcntl(rtc_fd, F_SETOWN, getpid()); | |
945 | } else | |
946 | #endif /* defined(__linux__) */ | |
947 | { | |
948 | use_itimer: | |
949 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * | |
950 | PIT_FREQ) / 1000000; | |
951 | } | |
952 | } | |
953 | #endif | |
954 | } | |
955 | ||
956 | void quit_timers(void) | |
957 | { | |
958 | #ifdef _WIN32 | |
959 | timeKillEvent(timerID); | |
960 | #endif | |
961 | } | |
962 | ||
963 | /***********************************************************/ | |
964 | /* character device */ | |
965 | ||
966 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) | |
967 | { | |
968 | return s->chr_write(s, buf, len); | |
969 | } | |
970 | ||
971 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) | |
972 | { | |
973 | char buf[4096]; | |
974 | va_list ap; | |
975 | va_start(ap, fmt); | |
976 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
977 | qemu_chr_write(s, buf, strlen(buf)); | |
978 | va_end(ap); | |
979 | } | |
980 | ||
981 | void qemu_chr_send_event(CharDriverState *s, int event) | |
982 | { | |
983 | if (s->chr_send_event) | |
984 | s->chr_send_event(s, event); | |
985 | } | |
986 | ||
987 | void qemu_chr_add_read_handler(CharDriverState *s, | |
988 | IOCanRWHandler *fd_can_read, | |
989 | IOReadHandler *fd_read, void *opaque) | |
990 | { | |
991 | s->chr_add_read_handler(s, fd_can_read, fd_read, opaque); | |
992 | } | |
993 | ||
994 | void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event) | |
995 | { | |
996 | s->chr_event = chr_event; | |
997 | } | |
998 | ||
999 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
1000 | { | |
1001 | return len; | |
1002 | } | |
1003 | ||
1004 | static void null_chr_add_read_handler(CharDriverState *chr, | |
1005 | IOCanRWHandler *fd_can_read, | |
1006 | IOReadHandler *fd_read, void *opaque) | |
1007 | { | |
1008 | } | |
1009 | ||
1010 | CharDriverState *qemu_chr_open_null(void) | |
1011 | { | |
1012 | CharDriverState *chr; | |
1013 | ||
1014 | chr = qemu_mallocz(sizeof(CharDriverState)); | |
1015 | if (!chr) | |
1016 | return NULL; | |
1017 | chr->chr_write = null_chr_write; | |
1018 | chr->chr_add_read_handler = null_chr_add_read_handler; | |
1019 | return chr; | |
1020 | } | |
1021 | ||
1022 | #ifndef _WIN32 | |
1023 | ||
1024 | typedef struct { | |
1025 | int fd_in, fd_out; | |
1026 | /* for nographic stdio only */ | |
1027 | IOCanRWHandler *fd_can_read; | |
1028 | IOReadHandler *fd_read; | |
1029 | void *fd_opaque; | |
1030 | } FDCharDriver; | |
1031 | ||
1032 | #define STDIO_MAX_CLIENTS 2 | |
1033 | ||
1034 | static int stdio_nb_clients; | |
1035 | static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS]; | |
1036 | ||
1037 | static int unix_write(int fd, const uint8_t *buf, int len1) | |
1038 | { | |
1039 | int ret, len; | |
1040 | ||
1041 | len = len1; | |
1042 | while (len > 0) { | |
1043 | ret = write(fd, buf, len); | |
1044 | if (ret < 0) { | |
1045 | if (errno != EINTR && errno != EAGAIN) | |
1046 | return -1; | |
1047 | } else if (ret == 0) { | |
1048 | break; | |
1049 | } else { | |
1050 | buf += ret; | |
1051 | len -= ret; | |
1052 | } | |
1053 | } | |
1054 | return len1 - len; | |
1055 | } | |
1056 | ||
1057 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
1058 | { | |
1059 | FDCharDriver *s = chr->opaque; | |
1060 | return unix_write(s->fd_out, buf, len); | |
1061 | } | |
1062 | ||
1063 | static void fd_chr_add_read_handler(CharDriverState *chr, | |
1064 | IOCanRWHandler *fd_can_read, | |
1065 | IOReadHandler *fd_read, void *opaque) | |
1066 | { | |
1067 | FDCharDriver *s = chr->opaque; | |
1068 | ||
1069 | if (nographic && s->fd_in == 0) { | |
1070 | s->fd_can_read = fd_can_read; | |
1071 | s->fd_read = fd_read; | |
1072 | s->fd_opaque = opaque; | |
1073 | } else { | |
1074 | qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque); | |
1075 | } | |
1076 | } | |
1077 | ||
1078 | /* open a character device to a unix fd */ | |
1079 | CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) | |
1080 | { | |
1081 | CharDriverState *chr; | |
1082 | FDCharDriver *s; | |
1083 | ||
1084 | chr = qemu_mallocz(sizeof(CharDriverState)); | |
1085 | if (!chr) | |
1086 | return NULL; | |
1087 | s = qemu_mallocz(sizeof(FDCharDriver)); | |
1088 | if (!s) { | |
1089 | free(chr); | |
1090 | return NULL; | |
1091 | } | |
1092 | s->fd_in = fd_in; | |
1093 | s->fd_out = fd_out; | |
1094 | chr->opaque = s; | |
1095 | chr->chr_write = fd_chr_write; | |
1096 | chr->chr_add_read_handler = fd_chr_add_read_handler; | |
1097 | return chr; | |
1098 | } | |
1099 | ||
1100 | /* for STDIO, we handle the case where several clients use it | |
1101 | (nographic mode) */ | |
1102 | ||
1103 | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ | |
1104 | ||
1105 | static int term_got_escape, client_index; | |
1106 | ||
1107 | void term_print_help(void) | |
1108 | { | |
1109 | printf("\n" | |
1110 | "C-a h print this help\n" | |
1111 | "C-a x exit emulator\n" | |
1112 | "C-a s save disk data back to file (if -snapshot)\n" | |
1113 | "C-a b send break (magic sysrq)\n" | |
1114 | "C-a c switch between console and monitor\n" | |
1115 | "C-a C-a send C-a\n" | |
1116 | ); | |
1117 | } | |
1118 | ||
1119 | /* called when a char is received */ | |
1120 | static void stdio_received_byte(int ch) | |
1121 | { | |
1122 | if (term_got_escape) { | |
1123 | term_got_escape = 0; | |
1124 | switch(ch) { | |
1125 | case 'h': | |
1126 | term_print_help(); | |
1127 | break; | |
1128 | case 'x': | |
1129 | exit(0); | |
1130 | break; | |
1131 | case 's': | |
1132 | { | |
1133 | int i; | |
1134 | for (i = 0; i < MAX_DISKS; i++) { | |
1135 | if (bs_table[i]) | |
1136 | bdrv_commit(bs_table[i]); | |
1137 | } | |
1138 | } | |
1139 | break; | |
1140 | case 'b': | |
1141 | if (client_index < stdio_nb_clients) { | |
1142 | CharDriverState *chr; | |
1143 | FDCharDriver *s; | |
1144 | ||
1145 | chr = stdio_clients[client_index]; | |
1146 | s = chr->opaque; | |
1147 | chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK); | |
1148 | } | |
1149 | break; | |
1150 | case 'c': | |
1151 | client_index++; | |
1152 | if (client_index >= stdio_nb_clients) | |
1153 | client_index = 0; | |
1154 | if (client_index == 0) { | |
1155 | /* send a new line in the monitor to get the prompt */ | |
1156 | ch = '\r'; | |
1157 | goto send_char; | |
1158 | } | |
1159 | break; | |
1160 | case TERM_ESCAPE: | |
1161 | goto send_char; | |
1162 | } | |
1163 | } else if (ch == TERM_ESCAPE) { | |
1164 | term_got_escape = 1; | |
1165 | } else { | |
1166 | send_char: | |
1167 | if (client_index < stdio_nb_clients) { | |
1168 | uint8_t buf[1]; | |
1169 | CharDriverState *chr; | |
1170 | FDCharDriver *s; | |
1171 | ||
1172 | chr = stdio_clients[client_index]; | |
1173 | s = chr->opaque; | |
1174 | buf[0] = ch; | |
1175 | /* XXX: should queue the char if the device is not | |
1176 | ready */ | |
1177 | if (s->fd_can_read(s->fd_opaque) > 0) | |
1178 | s->fd_read(s->fd_opaque, buf, 1); | |
1179 | } | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | static int stdio_can_read(void *opaque) | |
1184 | { | |
1185 | /* XXX: not strictly correct */ | |
1186 | return 1; | |
1187 | } | |
1188 | ||
1189 | static void stdio_read(void *opaque, const uint8_t *buf, int size) | |
1190 | { | |
1191 | int i; | |
1192 | for(i = 0; i < size; i++) | |
1193 | stdio_received_byte(buf[i]); | |
1194 | } | |
1195 | ||
1196 | /* init terminal so that we can grab keys */ | |
1197 | static struct termios oldtty; | |
1198 | static int old_fd0_flags; | |
1199 | ||
1200 | static void term_exit(void) | |
1201 | { | |
1202 | tcsetattr (0, TCSANOW, &oldtty); | |
1203 | fcntl(0, F_SETFL, old_fd0_flags); | |
1204 | } | |
1205 | ||
1206 | static void term_init(void) | |
1207 | { | |
1208 | struct termios tty; | |
1209 | ||
1210 | tcgetattr (0, &tty); | |
1211 | oldtty = tty; | |
1212 | old_fd0_flags = fcntl(0, F_GETFL); | |
1213 | ||
1214 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1215 | |INLCR|IGNCR|ICRNL|IXON); | |
1216 | tty.c_oflag |= OPOST; | |
1217 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
1218 | /* if graphical mode, we allow Ctrl-C handling */ | |
1219 | if (nographic) | |
1220 | tty.c_lflag &= ~ISIG; | |
1221 | tty.c_cflag &= ~(CSIZE|PARENB); | |
1222 | tty.c_cflag |= CS8; | |
1223 | tty.c_cc[VMIN] = 1; | |
1224 | tty.c_cc[VTIME] = 0; | |
1225 | ||
1226 | tcsetattr (0, TCSANOW, &tty); | |
1227 | ||
1228 | atexit(term_exit); | |
1229 | ||
1230 | fcntl(0, F_SETFL, O_NONBLOCK); | |
1231 | } | |
1232 | ||
1233 | CharDriverState *qemu_chr_open_stdio(void) | |
1234 | { | |
1235 | CharDriverState *chr; | |
1236 | ||
1237 | if (nographic) { | |
1238 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) | |
1239 | return NULL; | |
1240 | chr = qemu_chr_open_fd(0, 1); | |
1241 | if (stdio_nb_clients == 0) | |
1242 | qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL); | |
1243 | client_index = stdio_nb_clients; | |
1244 | } else { | |
1245 | if (stdio_nb_clients != 0) | |
1246 | return NULL; | |
1247 | chr = qemu_chr_open_fd(0, 1); | |
1248 | } | |
1249 | stdio_clients[stdio_nb_clients++] = chr; | |
1250 | if (stdio_nb_clients == 1) { | |
1251 | /* set the terminal in raw mode */ | |
1252 | term_init(); | |
1253 | } | |
1254 | return chr; | |
1255 | } | |
1256 | ||
1257 | #if defined(__linux__) | |
1258 | CharDriverState *qemu_chr_open_pty(void) | |
1259 | { | |
1260 | char slave_name[1024]; | |
1261 | int master_fd, slave_fd; | |
1262 | ||
1263 | /* Not satisfying */ | |
1264 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { | |
1265 | return NULL; | |
1266 | } | |
1267 | fprintf(stderr, "char device redirected to %s\n", slave_name); | |
1268 | return qemu_chr_open_fd(master_fd, master_fd); | |
1269 | } | |
1270 | #else | |
1271 | CharDriverState *qemu_chr_open_pty(void) | |
1272 | { | |
1273 | return NULL; | |
1274 | } | |
1275 | #endif | |
1276 | ||
1277 | #endif /* !defined(_WIN32) */ | |
1278 | ||
1279 | CharDriverState *qemu_chr_open(const char *filename) | |
1280 | { | |
1281 | if (!strcmp(filename, "vc")) { | |
1282 | return text_console_init(&display_state); | |
1283 | } else if (!strcmp(filename, "null")) { | |
1284 | return qemu_chr_open_null(); | |
1285 | } else | |
1286 | #ifndef _WIN32 | |
1287 | if (!strcmp(filename, "pty")) { | |
1288 | return qemu_chr_open_pty(); | |
1289 | } else if (!strcmp(filename, "stdio")) { | |
1290 | return qemu_chr_open_stdio(); | |
1291 | } else | |
1292 | #endif | |
1293 | { | |
1294 | return NULL; | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | /***********************************************************/ | |
1299 | /* Linux network device redirectors */ | |
1300 | ||
1301 | void hex_dump(FILE *f, const uint8_t *buf, int size) | |
1302 | { | |
1303 | int len, i, j, c; | |
1304 | ||
1305 | for(i=0;i<size;i+=16) { | |
1306 | len = size - i; | |
1307 | if (len > 16) | |
1308 | len = 16; | |
1309 | fprintf(f, "%08x ", i); | |
1310 | for(j=0;j<16;j++) { | |
1311 | if (j < len) | |
1312 | fprintf(f, " %02x", buf[i+j]); | |
1313 | else | |
1314 | fprintf(f, " "); | |
1315 | } | |
1316 | fprintf(f, " "); | |
1317 | for(j=0;j<len;j++) { | |
1318 | c = buf[i+j]; | |
1319 | if (c < ' ' || c > '~') | |
1320 | c = '.'; | |
1321 | fprintf(f, "%c", c); | |
1322 | } | |
1323 | fprintf(f, "\n"); | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
1328 | { | |
1329 | nd->send_packet(nd, buf, size); | |
1330 | } | |
1331 | ||
1332 | void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, | |
1333 | IOReadHandler *fd_read, void *opaque) | |
1334 | { | |
1335 | nd->add_read_packet(nd, fd_can_read, fd_read, opaque); | |
1336 | } | |
1337 | ||
1338 | /* dummy network adapter */ | |
1339 | ||
1340 | static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
1341 | { | |
1342 | } | |
1343 | ||
1344 | static void dummy_add_read_packet(NetDriverState *nd, | |
1345 | IOCanRWHandler *fd_can_read, | |
1346 | IOReadHandler *fd_read, void *opaque) | |
1347 | { | |
1348 | } | |
1349 | ||
1350 | static int net_dummy_init(NetDriverState *nd) | |
1351 | { | |
1352 | nd->send_packet = dummy_send_packet; | |
1353 | nd->add_read_packet = dummy_add_read_packet; | |
1354 | pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy"); | |
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | #if defined(CONFIG_SLIRP) | |
1359 | ||
1360 | /* slirp network adapter */ | |
1361 | ||
1362 | static void *slirp_fd_opaque; | |
1363 | static IOCanRWHandler *slirp_fd_can_read; | |
1364 | static IOReadHandler *slirp_fd_read; | |
1365 | static int slirp_inited; | |
1366 | ||
1367 | int slirp_can_output(void) | |
1368 | { | |
1369 | return slirp_fd_can_read(slirp_fd_opaque); | |
1370 | } | |
1371 | ||
1372 | void slirp_output(const uint8_t *pkt, int pkt_len) | |
1373 | { | |
1374 | #if 0 | |
1375 | printf("output:\n"); | |
1376 | hex_dump(stdout, pkt, pkt_len); | |
1377 | #endif | |
1378 | slirp_fd_read(slirp_fd_opaque, pkt, pkt_len); | |
1379 | } | |
1380 | ||
1381 | static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
1382 | { | |
1383 | #if 0 | |
1384 | printf("input:\n"); | |
1385 | hex_dump(stdout, buf, size); | |
1386 | #endif | |
1387 | slirp_input(buf, size); | |
1388 | } | |
1389 | ||
1390 | static void slirp_add_read_packet(NetDriverState *nd, | |
1391 | IOCanRWHandler *fd_can_read, | |
1392 | IOReadHandler *fd_read, void *opaque) | |
1393 | { | |
1394 | slirp_fd_opaque = opaque; | |
1395 | slirp_fd_can_read = fd_can_read; | |
1396 | slirp_fd_read = fd_read; | |
1397 | } | |
1398 | ||
1399 | static int net_slirp_init(NetDriverState *nd) | |
1400 | { | |
1401 | if (!slirp_inited) { | |
1402 | slirp_inited = 1; | |
1403 | slirp_init(); | |
1404 | } | |
1405 | nd->send_packet = slirp_send_packet; | |
1406 | nd->add_read_packet = slirp_add_read_packet; | |
1407 | pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp"); | |
1408 | return 0; | |
1409 | } | |
1410 | ||
1411 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
1412 | { | |
1413 | const char *p, *p1; | |
1414 | int len; | |
1415 | p = *pp; | |
1416 | p1 = strchr(p, sep); | |
1417 | if (!p1) | |
1418 | return -1; | |
1419 | len = p1 - p; | |
1420 | p1++; | |
1421 | if (buf_size > 0) { | |
1422 | if (len > buf_size - 1) | |
1423 | len = buf_size - 1; | |
1424 | memcpy(buf, p, len); | |
1425 | buf[len] = '\0'; | |
1426 | } | |
1427 | *pp = p1; | |
1428 | return 0; | |
1429 | } | |
1430 | ||
1431 | static void net_slirp_redir(const char *redir_str) | |
1432 | { | |
1433 | int is_udp; | |
1434 | char buf[256], *r; | |
1435 | const char *p; | |
1436 | struct in_addr guest_addr; | |
1437 | int host_port, guest_port; | |
1438 | ||
1439 | if (!slirp_inited) { | |
1440 | slirp_inited = 1; | |
1441 | slirp_init(); | |
1442 | } | |
1443 | ||
1444 | p = redir_str; | |
1445 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
1446 | goto fail; | |
1447 | if (!strcmp(buf, "tcp")) { | |
1448 | is_udp = 0; | |
1449 | } else if (!strcmp(buf, "udp")) { | |
1450 | is_udp = 1; | |
1451 | } else { | |
1452 | goto fail; | |
1453 | } | |
1454 | ||
1455 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
1456 | goto fail; | |
1457 | host_port = strtol(buf, &r, 0); | |
1458 | if (r == buf) | |
1459 | goto fail; | |
1460 | ||
1461 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
1462 | goto fail; | |
1463 | if (buf[0] == '\0') { | |
1464 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); | |
1465 | } | |
1466 | if (!inet_aton(buf, &guest_addr)) | |
1467 | goto fail; | |
1468 | ||
1469 | guest_port = strtol(p, &r, 0); | |
1470 | if (r == p) | |
1471 | goto fail; | |
1472 | ||
1473 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { | |
1474 | fprintf(stderr, "qemu: could not set up redirection\n"); | |
1475 | exit(1); | |
1476 | } | |
1477 | return; | |
1478 | fail: | |
1479 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); | |
1480 | exit(1); | |
1481 | } | |
1482 | ||
1483 | #ifndef _WIN32 | |
1484 | ||
1485 | char smb_dir[1024]; | |
1486 | ||
1487 | static void smb_exit(void) | |
1488 | { | |
1489 | DIR *d; | |
1490 | struct dirent *de; | |
1491 | char filename[1024]; | |
1492 | ||
1493 | /* erase all the files in the directory */ | |
1494 | d = opendir(smb_dir); | |
1495 | for(;;) { | |
1496 | de = readdir(d); | |
1497 | if (!de) | |
1498 | break; | |
1499 | if (strcmp(de->d_name, ".") != 0 && | |
1500 | strcmp(de->d_name, "..") != 0) { | |
1501 | snprintf(filename, sizeof(filename), "%s/%s", | |
1502 | smb_dir, de->d_name); | |
1503 | unlink(filename); | |
1504 | } | |
1505 | } | |
1506 | closedir(d); | |
1507 | rmdir(smb_dir); | |
1508 | } | |
1509 | ||
1510 | /* automatic user mode samba server configuration */ | |
1511 | void net_slirp_smb(const char *exported_dir) | |
1512 | { | |
1513 | char smb_conf[1024]; | |
1514 | char smb_cmdline[1024]; | |
1515 | FILE *f; | |
1516 | ||
1517 | if (!slirp_inited) { | |
1518 | slirp_inited = 1; | |
1519 | slirp_init(); | |
1520 | } | |
1521 | ||
1522 | /* XXX: better tmp dir construction */ | |
1523 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); | |
1524 | if (mkdir(smb_dir, 0700) < 0) { | |
1525 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); | |
1526 | exit(1); | |
1527 | } | |
1528 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); | |
1529 | ||
1530 | f = fopen(smb_conf, "w"); | |
1531 | if (!f) { | |
1532 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); | |
1533 | exit(1); | |
1534 | } | |
1535 | fprintf(f, | |
1536 | "[global]\n" | |
1537 | "pid directory=%s\n" | |
1538 | "lock directory=%s\n" | |
1539 | "log file=%s/log.smbd\n" | |
1540 | "smb passwd file=%s/smbpasswd\n" | |
1541 | "security = share\n" | |
1542 | "[qemu]\n" | |
1543 | "path=%s\n" | |
1544 | "read only=no\n" | |
1545 | "guest ok=yes\n", | |
1546 | smb_dir, | |
1547 | smb_dir, | |
1548 | smb_dir, | |
1549 | smb_dir, | |
1550 | exported_dir | |
1551 | ); | |
1552 | fclose(f); | |
1553 | atexit(smb_exit); | |
1554 | ||
1555 | snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s", | |
1556 | smb_conf); | |
1557 | ||
1558 | slirp_add_exec(0, smb_cmdline, 4, 139); | |
1559 | } | |
1560 | ||
1561 | #endif /* !defined(_WIN32) */ | |
1562 | ||
1563 | #endif /* CONFIG_SLIRP */ | |
1564 | ||
1565 | #if !defined(_WIN32) | |
1566 | #ifdef _BSD | |
1567 | static int tun_open(char *ifname, int ifname_size) | |
1568 | { | |
1569 | int fd; | |
1570 | char *dev; | |
1571 | struct stat s; | |
1572 | ||
1573 | fd = open("/dev/tap", O_RDWR); | |
1574 | if (fd < 0) { | |
1575 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); | |
1576 | return -1; | |
1577 | } | |
1578 | ||
1579 | fstat(fd, &s); | |
1580 | dev = devname(s.st_rdev, S_IFCHR); | |
1581 | pstrcpy(ifname, ifname_size, dev); | |
1582 | ||
1583 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1584 | return fd; | |
1585 | } | |
1586 | #else | |
1587 | static int tun_open(char *ifname, int ifname_size) | |
1588 | { | |
1589 | struct ifreq ifr; | |
1590 | int fd, ret; | |
1591 | ||
1592 | fd = open("/dev/net/tun", O_RDWR); | |
1593 | if (fd < 0) { | |
1594 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); | |
1595 | return -1; | |
1596 | } | |
1597 | memset(&ifr, 0, sizeof(ifr)); | |
1598 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
1599 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d"); | |
1600 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); | |
1601 | if (ret != 0) { | |
1602 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); | |
1603 | close(fd); | |
1604 | return -1; | |
1605 | } | |
1606 | printf("Connected to host network interface: %s\n", ifr.ifr_name); | |
1607 | pstrcpy(ifname, ifname_size, ifr.ifr_name); | |
1608 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1609 | return fd; | |
1610 | } | |
1611 | #endif | |
1612 | ||
1613 | static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
1614 | { | |
1615 | write(nd->fd, buf, size); | |
1616 | } | |
1617 | ||
1618 | static void tun_add_read_packet(NetDriverState *nd, | |
1619 | IOCanRWHandler *fd_can_read, | |
1620 | IOReadHandler *fd_read, void *opaque) | |
1621 | { | |
1622 | qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque); | |
1623 | } | |
1624 | ||
1625 | static int net_tun_init(NetDriverState *nd) | |
1626 | { | |
1627 | int pid, status; | |
1628 | char *args[3]; | |
1629 | char **parg; | |
1630 | ||
1631 | nd->fd = tun_open(nd->ifname, sizeof(nd->ifname)); | |
1632 | if (nd->fd < 0) | |
1633 | return -1; | |
1634 | ||
1635 | /* try to launch network init script */ | |
1636 | pid = fork(); | |
1637 | if (pid >= 0) { | |
1638 | if (pid == 0) { | |
1639 | parg = args; | |
1640 | *parg++ = network_script; | |
1641 | *parg++ = nd->ifname; | |
1642 | *parg++ = NULL; | |
1643 | execv(network_script, args); | |
1644 | exit(1); | |
1645 | } | |
1646 | while (waitpid(pid, &status, 0) != pid); | |
1647 | if (!WIFEXITED(status) || | |
1648 | WEXITSTATUS(status) != 0) { | |
1649 | fprintf(stderr, "%s: could not launch network script\n", | |
1650 | network_script); | |
1651 | } | |
1652 | } | |
1653 | nd->send_packet = tun_send_packet; | |
1654 | nd->add_read_packet = tun_add_read_packet; | |
1655 | return 0; | |
1656 | } | |
1657 | ||
1658 | static int net_fd_init(NetDriverState *nd, int fd) | |
1659 | { | |
1660 | nd->fd = fd; | |
1661 | nd->send_packet = tun_send_packet; | |
1662 | nd->add_read_packet = tun_add_read_packet; | |
1663 | pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd"); | |
1664 | return 0; | |
1665 | } | |
1666 | ||
1667 | #endif /* !_WIN32 */ | |
1668 | ||
1669 | /***********************************************************/ | |
1670 | /* dumb display */ | |
1671 | ||
1672 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) | |
1673 | { | |
1674 | } | |
1675 | ||
1676 | static void dumb_resize(DisplayState *ds, int w, int h) | |
1677 | { | |
1678 | } | |
1679 | ||
1680 | static void dumb_refresh(DisplayState *ds) | |
1681 | { | |
1682 | vga_update_display(); | |
1683 | } | |
1684 | ||
1685 | void dumb_display_init(DisplayState *ds) | |
1686 | { | |
1687 | ds->data = NULL; | |
1688 | ds->linesize = 0; | |
1689 | ds->depth = 0; | |
1690 | ds->dpy_update = dumb_update; | |
1691 | ds->dpy_resize = dumb_resize; | |
1692 | ds->dpy_refresh = dumb_refresh; | |
1693 | } | |
1694 | ||
1695 | #if !defined(CONFIG_SOFTMMU) | |
1696 | /***********************************************************/ | |
1697 | /* cpu signal handler */ | |
1698 | static void host_segv_handler(int host_signum, siginfo_t *info, | |
1699 | void *puc) | |
1700 | { | |
1701 | if (cpu_signal_handler(host_signum, info, puc)) | |
1702 | return; | |
1703 | if (stdio_nb_clients > 0) | |
1704 | term_exit(); | |
1705 | abort(); | |
1706 | } | |
1707 | #endif | |
1708 | ||
1709 | /***********************************************************/ | |
1710 | /* I/O handling */ | |
1711 | ||
1712 | #define MAX_IO_HANDLERS 64 | |
1713 | ||
1714 | typedef struct IOHandlerRecord { | |
1715 | int fd; | |
1716 | IOCanRWHandler *fd_can_read; | |
1717 | IOReadHandler *fd_read; | |
1718 | void *opaque; | |
1719 | /* temporary data */ | |
1720 | struct pollfd *ufd; | |
1721 | int max_size; | |
1722 | struct IOHandlerRecord *next; | |
1723 | } IOHandlerRecord; | |
1724 | ||
1725 | static IOHandlerRecord *first_io_handler; | |
1726 | ||
1727 | int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, | |
1728 | IOReadHandler *fd_read, void *opaque) | |
1729 | { | |
1730 | IOHandlerRecord *ioh; | |
1731 | ||
1732 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); | |
1733 | if (!ioh) | |
1734 | return -1; | |
1735 | ioh->fd = fd; | |
1736 | ioh->fd_can_read = fd_can_read; | |
1737 | ioh->fd_read = fd_read; | |
1738 | ioh->opaque = opaque; | |
1739 | ioh->next = first_io_handler; | |
1740 | first_io_handler = ioh; | |
1741 | return 0; | |
1742 | } | |
1743 | ||
1744 | void qemu_del_fd_read_handler(int fd) | |
1745 | { | |
1746 | IOHandlerRecord **pioh, *ioh; | |
1747 | ||
1748 | pioh = &first_io_handler; | |
1749 | for(;;) { | |
1750 | ioh = *pioh; | |
1751 | if (ioh == NULL) | |
1752 | break; | |
1753 | if (ioh->fd == fd) { | |
1754 | *pioh = ioh->next; | |
1755 | break; | |
1756 | } | |
1757 | pioh = &ioh->next; | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | /***********************************************************/ | |
1762 | /* savevm/loadvm support */ | |
1763 | ||
1764 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) | |
1765 | { | |
1766 | fwrite(buf, 1, size, f); | |
1767 | } | |
1768 | ||
1769 | void qemu_put_byte(QEMUFile *f, int v) | |
1770 | { | |
1771 | fputc(v, f); | |
1772 | } | |
1773 | ||
1774 | void qemu_put_be16(QEMUFile *f, unsigned int v) | |
1775 | { | |
1776 | qemu_put_byte(f, v >> 8); | |
1777 | qemu_put_byte(f, v); | |
1778 | } | |
1779 | ||
1780 | void qemu_put_be32(QEMUFile *f, unsigned int v) | |
1781 | { | |
1782 | qemu_put_byte(f, v >> 24); | |
1783 | qemu_put_byte(f, v >> 16); | |
1784 | qemu_put_byte(f, v >> 8); | |
1785 | qemu_put_byte(f, v); | |
1786 | } | |
1787 | ||
1788 | void qemu_put_be64(QEMUFile *f, uint64_t v) | |
1789 | { | |
1790 | qemu_put_be32(f, v >> 32); | |
1791 | qemu_put_be32(f, v); | |
1792 | } | |
1793 | ||
1794 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) | |
1795 | { | |
1796 | return fread(buf, 1, size, f); | |
1797 | } | |
1798 | ||
1799 | int qemu_get_byte(QEMUFile *f) | |
1800 | { | |
1801 | int v; | |
1802 | v = fgetc(f); | |
1803 | if (v == EOF) | |
1804 | return 0; | |
1805 | else | |
1806 | return v; | |
1807 | } | |
1808 | ||
1809 | unsigned int qemu_get_be16(QEMUFile *f) | |
1810 | { | |
1811 | unsigned int v; | |
1812 | v = qemu_get_byte(f) << 8; | |
1813 | v |= qemu_get_byte(f); | |
1814 | return v; | |
1815 | } | |
1816 | ||
1817 | unsigned int qemu_get_be32(QEMUFile *f) | |
1818 | { | |
1819 | unsigned int v; | |
1820 | v = qemu_get_byte(f) << 24; | |
1821 | v |= qemu_get_byte(f) << 16; | |
1822 | v |= qemu_get_byte(f) << 8; | |
1823 | v |= qemu_get_byte(f); | |
1824 | return v; | |
1825 | } | |
1826 | ||
1827 | uint64_t qemu_get_be64(QEMUFile *f) | |
1828 | { | |
1829 | uint64_t v; | |
1830 | v = (uint64_t)qemu_get_be32(f) << 32; | |
1831 | v |= qemu_get_be32(f); | |
1832 | return v; | |
1833 | } | |
1834 | ||
1835 | int64_t qemu_ftell(QEMUFile *f) | |
1836 | { | |
1837 | return ftell(f); | |
1838 | } | |
1839 | ||
1840 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) | |
1841 | { | |
1842 | if (fseek(f, pos, whence) < 0) | |
1843 | return -1; | |
1844 | return ftell(f); | |
1845 | } | |
1846 | ||
1847 | typedef struct SaveStateEntry { | |
1848 | char idstr[256]; | |
1849 | int instance_id; | |
1850 | int version_id; | |
1851 | SaveStateHandler *save_state; | |
1852 | LoadStateHandler *load_state; | |
1853 | void *opaque; | |
1854 | struct SaveStateEntry *next; | |
1855 | } SaveStateEntry; | |
1856 | ||
1857 | static SaveStateEntry *first_se; | |
1858 | ||
1859 | int register_savevm(const char *idstr, | |
1860 | int instance_id, | |
1861 | int version_id, | |
1862 | SaveStateHandler *save_state, | |
1863 | LoadStateHandler *load_state, | |
1864 | void *opaque) | |
1865 | { | |
1866 | SaveStateEntry *se, **pse; | |
1867 | ||
1868 | se = qemu_malloc(sizeof(SaveStateEntry)); | |
1869 | if (!se) | |
1870 | return -1; | |
1871 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); | |
1872 | se->instance_id = instance_id; | |
1873 | se->version_id = version_id; | |
1874 | se->save_state = save_state; | |
1875 | se->load_state = load_state; | |
1876 | se->opaque = opaque; | |
1877 | se->next = NULL; | |
1878 | ||
1879 | /* add at the end of list */ | |
1880 | pse = &first_se; | |
1881 | while (*pse != NULL) | |
1882 | pse = &(*pse)->next; | |
1883 | *pse = se; | |
1884 | return 0; | |
1885 | } | |
1886 | ||
1887 | #define QEMU_VM_FILE_MAGIC 0x5145564d | |
1888 | #define QEMU_VM_FILE_VERSION 0x00000001 | |
1889 | ||
1890 | int qemu_savevm(const char *filename) | |
1891 | { | |
1892 | SaveStateEntry *se; | |
1893 | QEMUFile *f; | |
1894 | int len, len_pos, cur_pos, saved_vm_running, ret; | |
1895 | ||
1896 | saved_vm_running = vm_running; | |
1897 | vm_stop(0); | |
1898 | ||
1899 | f = fopen(filename, "wb"); | |
1900 | if (!f) { | |
1901 | ret = -1; | |
1902 | goto the_end; | |
1903 | } | |
1904 | ||
1905 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); | |
1906 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1907 | ||
1908 | for(se = first_se; se != NULL; se = se->next) { | |
1909 | /* ID string */ | |
1910 | len = strlen(se->idstr); | |
1911 | qemu_put_byte(f, len); | |
1912 | qemu_put_buffer(f, se->idstr, len); | |
1913 | ||
1914 | qemu_put_be32(f, se->instance_id); | |
1915 | qemu_put_be32(f, se->version_id); | |
1916 | ||
1917 | /* record size: filled later */ | |
1918 | len_pos = ftell(f); | |
1919 | qemu_put_be32(f, 0); | |
1920 | ||
1921 | se->save_state(f, se->opaque); | |
1922 | ||
1923 | /* fill record size */ | |
1924 | cur_pos = ftell(f); | |
1925 | len = ftell(f) - len_pos - 4; | |
1926 | fseek(f, len_pos, SEEK_SET); | |
1927 | qemu_put_be32(f, len); | |
1928 | fseek(f, cur_pos, SEEK_SET); | |
1929 | } | |
1930 | ||
1931 | fclose(f); | |
1932 | ret = 0; | |
1933 | the_end: | |
1934 | if (saved_vm_running) | |
1935 | vm_start(); | |
1936 | return ret; | |
1937 | } | |
1938 | ||
1939 | static SaveStateEntry *find_se(const char *idstr, int instance_id) | |
1940 | { | |
1941 | SaveStateEntry *se; | |
1942 | ||
1943 | for(se = first_se; se != NULL; se = se->next) { | |
1944 | if (!strcmp(se->idstr, idstr) && | |
1945 | instance_id == se->instance_id) | |
1946 | return se; | |
1947 | } | |
1948 | return NULL; | |
1949 | } | |
1950 | ||
1951 | int qemu_loadvm(const char *filename) | |
1952 | { | |
1953 | SaveStateEntry *se; | |
1954 | QEMUFile *f; | |
1955 | int len, cur_pos, ret, instance_id, record_len, version_id; | |
1956 | int saved_vm_running; | |
1957 | unsigned int v; | |
1958 | char idstr[256]; | |
1959 | ||
1960 | saved_vm_running = vm_running; | |
1961 | vm_stop(0); | |
1962 | ||
1963 | f = fopen(filename, "rb"); | |
1964 | if (!f) { | |
1965 | ret = -1; | |
1966 | goto the_end; | |
1967 | } | |
1968 | ||
1969 | v = qemu_get_be32(f); | |
1970 | if (v != QEMU_VM_FILE_MAGIC) | |
1971 | goto fail; | |
1972 | v = qemu_get_be32(f); | |
1973 | if (v != QEMU_VM_FILE_VERSION) { | |
1974 | fail: | |
1975 | fclose(f); | |
1976 | ret = -1; | |
1977 | goto the_end; | |
1978 | } | |
1979 | for(;;) { | |
1980 | #if defined (DO_TB_FLUSH) | |
1981 | tb_flush(global_env); | |
1982 | #endif | |
1983 | len = qemu_get_byte(f); | |
1984 | if (feof(f)) | |
1985 | break; | |
1986 | qemu_get_buffer(f, idstr, len); | |
1987 | idstr[len] = '\0'; | |
1988 | instance_id = qemu_get_be32(f); | |
1989 | version_id = qemu_get_be32(f); | |
1990 | record_len = qemu_get_be32(f); | |
1991 | #if 0 | |
1992 | printf("idstr=%s instance=0x%x version=%d len=%d\n", | |
1993 | idstr, instance_id, version_id, record_len); | |
1994 | #endif | |
1995 | cur_pos = ftell(f); | |
1996 | se = find_se(idstr, instance_id); | |
1997 | if (!se) { | |
1998 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", | |
1999 | instance_id, idstr); | |
2000 | } else { | |
2001 | ret = se->load_state(f, se->opaque, version_id); | |
2002 | if (ret < 0) { | |
2003 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", | |
2004 | instance_id, idstr); | |
2005 | } | |
2006 | } | |
2007 | /* always seek to exact end of record */ | |
2008 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); | |
2009 | } | |
2010 | fclose(f); | |
2011 | ret = 0; | |
2012 | the_end: | |
2013 | if (saved_vm_running) | |
2014 | vm_start(); | |
2015 | return ret; | |
2016 | } | |
2017 | ||
2018 | /***********************************************************/ | |
2019 | /* cpu save/restore */ | |
2020 | ||
2021 | #if defined(TARGET_I386) | |
2022 | ||
2023 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) | |
2024 | { | |
2025 | qemu_put_be32(f, dt->selector); | |
2026 | qemu_put_be32(f, (uint32_t)dt->base); | |
2027 | qemu_put_be32(f, dt->limit); | |
2028 | qemu_put_be32(f, dt->flags); | |
2029 | } | |
2030 | ||
2031 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) | |
2032 | { | |
2033 | dt->selector = qemu_get_be32(f); | |
2034 | dt->base = (uint8_t *)qemu_get_be32(f); | |
2035 | dt->limit = qemu_get_be32(f); | |
2036 | dt->flags = qemu_get_be32(f); | |
2037 | } | |
2038 | ||
2039 | void cpu_save(QEMUFile *f, void *opaque) | |
2040 | { | |
2041 | CPUState *env = opaque; | |
2042 | uint16_t fptag, fpus, fpuc; | |
2043 | uint32_t hflags; | |
2044 | int i; | |
2045 | ||
2046 | for(i = 0; i < 8; i++) | |
2047 | qemu_put_be32s(f, &env->regs[i]); | |
2048 | qemu_put_be32s(f, &env->eip); | |
2049 | qemu_put_be32s(f, &env->eflags); | |
2050 | qemu_put_be32s(f, &env->eflags); | |
2051 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ | |
2052 | qemu_put_be32s(f, &hflags); | |
2053 | ||
2054 | /* FPU */ | |
2055 | fpuc = env->fpuc; | |
2056 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; | |
2057 | fptag = 0; | |
2058 | for (i=7; i>=0; i--) { | |
2059 | fptag <<= 2; | |
2060 | if (env->fptags[i]) { | |
2061 | fptag |= 3; | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | qemu_put_be16s(f, &fpuc); | |
2066 | qemu_put_be16s(f, &fpus); | |
2067 | qemu_put_be16s(f, &fptag); | |
2068 | ||
2069 | for(i = 0; i < 8; i++) { | |
2070 | uint64_t mant; | |
2071 | uint16_t exp; | |
2072 | cpu_get_fp80(&mant, &exp, env->fpregs[i]); | |
2073 | qemu_put_be64(f, mant); | |
2074 | qemu_put_be16(f, exp); | |
2075 | } | |
2076 | ||
2077 | for(i = 0; i < 6; i++) | |
2078 | cpu_put_seg(f, &env->segs[i]); | |
2079 | cpu_put_seg(f, &env->ldt); | |
2080 | cpu_put_seg(f, &env->tr); | |
2081 | cpu_put_seg(f, &env->gdt); | |
2082 | cpu_put_seg(f, &env->idt); | |
2083 | ||
2084 | qemu_put_be32s(f, &env->sysenter_cs); | |
2085 | qemu_put_be32s(f, &env->sysenter_esp); | |
2086 | qemu_put_be32s(f, &env->sysenter_eip); | |
2087 | ||
2088 | qemu_put_be32s(f, &env->cr[0]); | |
2089 | qemu_put_be32s(f, &env->cr[2]); | |
2090 | qemu_put_be32s(f, &env->cr[3]); | |
2091 | qemu_put_be32s(f, &env->cr[4]); | |
2092 | ||
2093 | for(i = 0; i < 8; i++) | |
2094 | qemu_put_be32s(f, &env->dr[i]); | |
2095 | ||
2096 | /* MMU */ | |
2097 | qemu_put_be32s(f, &env->a20_mask); | |
2098 | } | |
2099 | ||
2100 | int cpu_load(QEMUFile *f, void *opaque, int version_id) | |
2101 | { | |
2102 | CPUState *env = opaque; | |
2103 | int i; | |
2104 | uint32_t hflags; | |
2105 | uint16_t fpus, fpuc, fptag; | |
2106 | ||
2107 | if (version_id != 2) | |
2108 | return -EINVAL; | |
2109 | for(i = 0; i < 8; i++) | |
2110 | qemu_get_be32s(f, &env->regs[i]); | |
2111 | qemu_get_be32s(f, &env->eip); | |
2112 | qemu_get_be32s(f, &env->eflags); | |
2113 | qemu_get_be32s(f, &env->eflags); | |
2114 | qemu_get_be32s(f, &hflags); | |
2115 | ||
2116 | qemu_get_be16s(f, &fpuc); | |
2117 | qemu_get_be16s(f, &fpus); | |
2118 | qemu_get_be16s(f, &fptag); | |
2119 | ||
2120 | for(i = 0; i < 8; i++) { | |
2121 | uint64_t mant; | |
2122 | uint16_t exp; | |
2123 | mant = qemu_get_be64(f); | |
2124 | exp = qemu_get_be16(f); | |
2125 | env->fpregs[i] = cpu_set_fp80(mant, exp); | |
2126 | } | |
2127 | ||
2128 | env->fpuc = fpuc; | |
2129 | env->fpstt = (fpus >> 11) & 7; | |
2130 | env->fpus = fpus & ~0x3800; | |
2131 | for(i = 0; i < 8; i++) { | |
2132 | env->fptags[i] = ((fptag & 3) == 3); | |
2133 | fptag >>= 2; | |
2134 | } | |
2135 | ||
2136 | for(i = 0; i < 6; i++) | |
2137 | cpu_get_seg(f, &env->segs[i]); | |
2138 | cpu_get_seg(f, &env->ldt); | |
2139 | cpu_get_seg(f, &env->tr); | |
2140 | cpu_get_seg(f, &env->gdt); | |
2141 | cpu_get_seg(f, &env->idt); | |
2142 | ||
2143 | qemu_get_be32s(f, &env->sysenter_cs); | |
2144 | qemu_get_be32s(f, &env->sysenter_esp); | |
2145 | qemu_get_be32s(f, &env->sysenter_eip); | |
2146 | ||
2147 | qemu_get_be32s(f, &env->cr[0]); | |
2148 | qemu_get_be32s(f, &env->cr[2]); | |
2149 | qemu_get_be32s(f, &env->cr[3]); | |
2150 | qemu_get_be32s(f, &env->cr[4]); | |
2151 | ||
2152 | for(i = 0; i < 8; i++) | |
2153 | qemu_get_be32s(f, &env->dr[i]); | |
2154 | ||
2155 | /* MMU */ | |
2156 | qemu_get_be32s(f, &env->a20_mask); | |
2157 | ||
2158 | /* XXX: compute hflags from scratch, except for CPL and IIF */ | |
2159 | env->hflags = hflags; | |
2160 | tlb_flush(env, 1); | |
2161 | return 0; | |
2162 | } | |
2163 | ||
2164 | #elif defined(TARGET_PPC) | |
2165 | void cpu_save(QEMUFile *f, void *opaque) | |
2166 | { | |
2167 | } | |
2168 | ||
2169 | int cpu_load(QEMUFile *f, void *opaque, int version_id) | |
2170 | { | |
2171 | return 0; | |
2172 | } | |
2173 | #elif defined(TARGET_SPARC) | |
2174 | void cpu_save(QEMUFile *f, void *opaque) | |
2175 | { | |
2176 | } | |
2177 | ||
2178 | int cpu_load(QEMUFile *f, void *opaque, int version_id) | |
2179 | { | |
2180 | return 0; | |
2181 | } | |
2182 | #else | |
2183 | ||
2184 | #warning No CPU save/restore functions | |
2185 | ||
2186 | #endif | |
2187 | ||
2188 | /***********************************************************/ | |
2189 | /* ram save/restore */ | |
2190 | ||
2191 | /* we just avoid storing empty pages */ | |
2192 | static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len) | |
2193 | { | |
2194 | int i, v; | |
2195 | ||
2196 | v = buf[0]; | |
2197 | for(i = 1; i < len; i++) { | |
2198 | if (buf[i] != v) | |
2199 | goto normal_save; | |
2200 | } | |
2201 | qemu_put_byte(f, 1); | |
2202 | qemu_put_byte(f, v); | |
2203 | return; | |
2204 | normal_save: | |
2205 | qemu_put_byte(f, 0); | |
2206 | qemu_put_buffer(f, buf, len); | |
2207 | } | |
2208 | ||
2209 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) | |
2210 | { | |
2211 | int v; | |
2212 | ||
2213 | v = qemu_get_byte(f); | |
2214 | switch(v) { | |
2215 | case 0: | |
2216 | if (qemu_get_buffer(f, buf, len) != len) | |
2217 | return -EIO; | |
2218 | break; | |
2219 | case 1: | |
2220 | v = qemu_get_byte(f); | |
2221 | memset(buf, v, len); | |
2222 | break; | |
2223 | default: | |
2224 | return -EINVAL; | |
2225 | } | |
2226 | return 0; | |
2227 | } | |
2228 | ||
2229 | static void ram_save(QEMUFile *f, void *opaque) | |
2230 | { | |
2231 | int i; | |
2232 | qemu_put_be32(f, phys_ram_size); | |
2233 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { | |
2234 | ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); | |
2235 | } | |
2236 | } | |
2237 | ||
2238 | static int ram_load(QEMUFile *f, void *opaque, int version_id) | |
2239 | { | |
2240 | int i, ret; | |
2241 | ||
2242 | if (version_id != 1) | |
2243 | return -EINVAL; | |
2244 | if (qemu_get_be32(f) != phys_ram_size) | |
2245 | return -EINVAL; | |
2246 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { | |
2247 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); | |
2248 | if (ret) | |
2249 | return ret; | |
2250 | } | |
2251 | return 0; | |
2252 | } | |
2253 | ||
2254 | /***********************************************************/ | |
2255 | /* main execution loop */ | |
2256 | ||
2257 | void gui_update(void *opaque) | |
2258 | { | |
2259 | display_state.dpy_refresh(&display_state); | |
2260 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); | |
2261 | } | |
2262 | ||
2263 | /* XXX: support several handlers */ | |
2264 | VMStopHandler *vm_stop_cb; | |
2265 | VMStopHandler *vm_stop_opaque; | |
2266 | ||
2267 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) | |
2268 | { | |
2269 | vm_stop_cb = cb; | |
2270 | vm_stop_opaque = opaque; | |
2271 | return 0; | |
2272 | } | |
2273 | ||
2274 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) | |
2275 | { | |
2276 | vm_stop_cb = NULL; | |
2277 | } | |
2278 | ||
2279 | void vm_start(void) | |
2280 | { | |
2281 | if (!vm_running) { | |
2282 | cpu_enable_ticks(); | |
2283 | vm_running = 1; | |
2284 | } | |
2285 | } | |
2286 | ||
2287 | void vm_stop(int reason) | |
2288 | { | |
2289 | if (vm_running) { | |
2290 | cpu_disable_ticks(); | |
2291 | vm_running = 0; | |
2292 | if (reason != 0) { | |
2293 | if (vm_stop_cb) { | |
2294 | vm_stop_cb(vm_stop_opaque, reason); | |
2295 | } | |
2296 | } | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | /* reset/shutdown handler */ | |
2301 | ||
2302 | typedef struct QEMUResetEntry { | |
2303 | QEMUResetHandler *func; | |
2304 | void *opaque; | |
2305 | struct QEMUResetEntry *next; | |
2306 | } QEMUResetEntry; | |
2307 | ||
2308 | static QEMUResetEntry *first_reset_entry; | |
2309 | static int reset_requested; | |
2310 | static int shutdown_requested; | |
2311 | ||
2312 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) | |
2313 | { | |
2314 | QEMUResetEntry **pre, *re; | |
2315 | ||
2316 | pre = &first_reset_entry; | |
2317 | while (*pre != NULL) | |
2318 | pre = &(*pre)->next; | |
2319 | re = qemu_mallocz(sizeof(QEMUResetEntry)); | |
2320 | re->func = func; | |
2321 | re->opaque = opaque; | |
2322 | re->next = NULL; | |
2323 | *pre = re; | |
2324 | } | |
2325 | ||
2326 | void qemu_system_reset(void) | |
2327 | { | |
2328 | QEMUResetEntry *re; | |
2329 | ||
2330 | /* reset all devices */ | |
2331 | for(re = first_reset_entry; re != NULL; re = re->next) { | |
2332 | re->func(re->opaque); | |
2333 | } | |
2334 | } | |
2335 | ||
2336 | void qemu_system_reset_request(void) | |
2337 | { | |
2338 | reset_requested = 1; | |
2339 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); | |
2340 | } | |
2341 | ||
2342 | void qemu_system_shutdown_request(void) | |
2343 | { | |
2344 | shutdown_requested = 1; | |
2345 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); | |
2346 | } | |
2347 | ||
2348 | static void main_cpu_reset(void *opaque) | |
2349 | { | |
2350 | #ifdef TARGET_I386 | |
2351 | CPUState *env = opaque; | |
2352 | cpu_reset(env); | |
2353 | #endif | |
2354 | } | |
2355 | ||
2356 | void main_loop_wait(int timeout) | |
2357 | { | |
2358 | #ifndef _WIN32 | |
2359 | struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf; | |
2360 | IOHandlerRecord *ioh, *ioh_next; | |
2361 | uint8_t buf[4096]; | |
2362 | int n, max_size; | |
2363 | #endif | |
2364 | int ret; | |
2365 | ||
2366 | #ifdef _WIN32 | |
2367 | if (timeout > 0) | |
2368 | Sleep(timeout); | |
2369 | #else | |
2370 | /* poll any events */ | |
2371 | /* XXX: separate device handlers from system ones */ | |
2372 | pf = ufds; | |
2373 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { | |
2374 | if (!ioh->fd_can_read) { | |
2375 | max_size = 0; | |
2376 | pf->fd = ioh->fd; | |
2377 | pf->events = POLLIN; | |
2378 | ioh->ufd = pf; | |
2379 | pf++; | |
2380 | } else { | |
2381 | max_size = ioh->fd_can_read(ioh->opaque); | |
2382 | if (max_size > 0) { | |
2383 | if (max_size > sizeof(buf)) | |
2384 | max_size = sizeof(buf); | |
2385 | pf->fd = ioh->fd; | |
2386 | pf->events = POLLIN; | |
2387 | ioh->ufd = pf; | |
2388 | pf++; | |
2389 | } else { | |
2390 | ioh->ufd = NULL; | |
2391 | } | |
2392 | } | |
2393 | ioh->max_size = max_size; | |
2394 | } | |
2395 | ||
2396 | ret = poll(ufds, pf - ufds, timeout); | |
2397 | if (ret > 0) { | |
2398 | /* XXX: better handling of removal */ | |
2399 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { | |
2400 | ioh_next = ioh->next; | |
2401 | pf = ioh->ufd; | |
2402 | if (pf) { | |
2403 | if (pf->revents & POLLIN) { | |
2404 | if (ioh->max_size == 0) { | |
2405 | /* just a read event */ | |
2406 | ioh->fd_read(ioh->opaque, NULL, 0); | |
2407 | } else { | |
2408 | n = read(ioh->fd, buf, ioh->max_size); | |
2409 | if (n >= 0) { | |
2410 | ioh->fd_read(ioh->opaque, buf, n); | |
2411 | } else if (errno != EAGAIN) { | |
2412 | ioh->fd_read(ioh->opaque, NULL, -errno); | |
2413 | } | |
2414 | } | |
2415 | } | |
2416 | } | |
2417 | } | |
2418 | } | |
2419 | #endif /* !defined(_WIN32) */ | |
2420 | #if defined(CONFIG_SLIRP) | |
2421 | /* XXX: merge with poll() */ | |
2422 | if (slirp_inited) { | |
2423 | fd_set rfds, wfds, xfds; | |
2424 | int nfds; | |
2425 | struct timeval tv; | |
2426 | ||
2427 | nfds = -1; | |
2428 | FD_ZERO(&rfds); | |
2429 | FD_ZERO(&wfds); | |
2430 | FD_ZERO(&xfds); | |
2431 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); | |
2432 | tv.tv_sec = 0; | |
2433 | tv.tv_usec = 0; | |
2434 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); | |
2435 | if (ret >= 0) { | |
2436 | slirp_select_poll(&rfds, &wfds, &xfds); | |
2437 | } | |
2438 | } | |
2439 | #endif | |
2440 | ||
2441 | if (vm_running) { | |
2442 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], | |
2443 | qemu_get_clock(vm_clock)); | |
2444 | /* run dma transfers, if any */ | |
2445 | DMA_run(); | |
2446 | } | |
2447 | ||
2448 | /* real time timers */ | |
2449 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], | |
2450 | qemu_get_clock(rt_clock)); | |
2451 | } | |
2452 | ||
2453 | int main_loop(void) | |
2454 | { | |
2455 | int ret, timeout; | |
2456 | CPUState *env = global_env; | |
2457 | ||
2458 | for(;;) { | |
2459 | if (vm_running) { | |
2460 | ret = cpu_exec(env); | |
2461 | if (shutdown_requested) { | |
2462 | ret = EXCP_INTERRUPT; | |
2463 | break; | |
2464 | } | |
2465 | if (reset_requested) { | |
2466 | reset_requested = 0; | |
2467 | qemu_system_reset(); | |
2468 | ret = EXCP_INTERRUPT; | |
2469 | } | |
2470 | if (ret == EXCP_DEBUG) { | |
2471 | vm_stop(EXCP_DEBUG); | |
2472 | } | |
2473 | /* if hlt instruction, we wait until the next IRQ */ | |
2474 | /* XXX: use timeout computed from timers */ | |
2475 | if (ret == EXCP_HLT) | |
2476 | timeout = 10; | |
2477 | else | |
2478 | timeout = 0; | |
2479 | } else { | |
2480 | timeout = 10; | |
2481 | } | |
2482 | main_loop_wait(timeout); | |
2483 | } | |
2484 | cpu_disable_ticks(); | |
2485 | return ret; | |
2486 | } | |
2487 | ||
2488 | void help(void) | |
2489 | { | |
2490 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n" | |
2491 | "usage: %s [options] [disk_image]\n" | |
2492 | "\n" | |
2493 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" | |
2494 | "\n" | |
2495 | "Standard options:\n" | |
2496 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" | |
2497 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" | |
2498 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" | |
2499 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" | |
2500 | "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n" | |
2501 | "-snapshot write to temporary files instead of disk image files\n" | |
2502 | "-m megs set virtual RAM size to megs MB [default=%d]\n" | |
2503 | "-nographic disable graphical output and redirect serial I/Os to console\n" | |
2504 | "-enable-audio enable audio support\n" | |
2505 | "-localtime set the real time clock to local time [default=utc]\n" | |
2506 | "-full-screen start in full screen\n" | |
2507 | #ifdef TARGET_PPC | |
2508 | "-prep Simulate a PREP system (default is PowerMAC)\n" | |
2509 | "-g WxH[xDEPTH] Set the initial VGA graphic mode\n" | |
2510 | #endif | |
2511 | "\n" | |
2512 | "Network options:\n" | |
2513 | "-nics n simulate 'n' network cards [default=1]\n" | |
2514 | "-macaddr addr set the mac address of the first interface\n" | |
2515 | "-n script set tap/tun network init script [default=%s]\n" | |
2516 | "-tun-fd fd use this fd as already opened tap/tun interface\n" | |
2517 | #ifdef CONFIG_SLIRP | |
2518 | "-user-net use user mode network stack [default if no tap/tun script]\n" | |
2519 | "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n" | |
2520 | #ifndef _WIN32 | |
2521 | "-smb dir allow SMB access to files in 'dir' [-user-net]\n" | |
2522 | #endif | |
2523 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" | |
2524 | " redirect TCP or UDP connections from host to guest [-user-net]\n" | |
2525 | #endif | |
2526 | "-dummy-net use dummy network stack\n" | |
2527 | "\n" | |
2528 | "Linux boot specific:\n" | |
2529 | "-kernel bzImage use 'bzImage' as kernel image\n" | |
2530 | "-append cmdline use 'cmdline' as kernel command line\n" | |
2531 | "-initrd file use 'file' as initial ram disk\n" | |
2532 | "\n" | |
2533 | "Debug/Expert options:\n" | |
2534 | "-monitor dev redirect the monitor to char device 'dev'\n" | |
2535 | "-serial dev redirect the serial port to char device 'dev'\n" | |
2536 | "-S freeze CPU at startup (use 'c' to start execution)\n" | |
2537 | "-s wait gdb connection to port %d\n" | |
2538 | "-p port change gdb connection port\n" | |
2539 | "-d item1,... output log to %s (use -d ? for a list of log items)\n" | |
2540 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" | |
2541 | " translation (t=none or lba) (usually qemu can guess them)\n" | |
2542 | "-L path set the directory for the BIOS and VGA BIOS\n" | |
2543 | #ifdef USE_CODE_COPY | |
2544 | "-no-code-copy disable code copy acceleration\n" | |
2545 | #endif | |
2546 | #ifdef TARGET_I386 | |
2547 | "-isa simulate an ISA-only system (default is PCI system)\n" | |
2548 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" | |
2549 | " (default is CL-GD5446 PCI VGA)\n" | |
2550 | #endif | |
2551 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" | |
2552 | "\n" | |
2553 | "During emulation, the following keys are useful:\n" | |
2554 | "ctrl-alt-f toggle full screen\n" | |
2555 | "ctrl-alt-n switch to virtual console 'n'\n" | |
2556 | "ctrl-alt toggle mouse and keyboard grab\n" | |
2557 | "\n" | |
2558 | "When using -nographic, press 'ctrl-a h' to get some help.\n" | |
2559 | , | |
2560 | #ifdef CONFIG_SOFTMMU | |
2561 | "qemu", | |
2562 | #else | |
2563 | "qemu-fast", | |
2564 | #endif | |
2565 | DEFAULT_RAM_SIZE, | |
2566 | DEFAULT_NETWORK_SCRIPT, | |
2567 | DEFAULT_GDBSTUB_PORT, | |
2568 | "/tmp/qemu.log"); | |
2569 | #ifndef CONFIG_SOFTMMU | |
2570 | printf("\n" | |
2571 | "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n" | |
2572 | "work. Please use the 'qemu' executable to have a more accurate (but slower)\n" | |
2573 | "PC emulation.\n"); | |
2574 | #endif | |
2575 | exit(1); | |
2576 | } | |
2577 | ||
2578 | #define HAS_ARG 0x0001 | |
2579 | ||
2580 | enum { | |
2581 | QEMU_OPTION_h, | |
2582 | ||
2583 | QEMU_OPTION_fda, | |
2584 | QEMU_OPTION_fdb, | |
2585 | QEMU_OPTION_hda, | |
2586 | QEMU_OPTION_hdb, | |
2587 | QEMU_OPTION_hdc, | |
2588 | QEMU_OPTION_hdd, | |
2589 | QEMU_OPTION_cdrom, | |
2590 | QEMU_OPTION_boot, | |
2591 | QEMU_OPTION_snapshot, | |
2592 | QEMU_OPTION_m, | |
2593 | QEMU_OPTION_nographic, | |
2594 | QEMU_OPTION_enable_audio, | |
2595 | ||
2596 | QEMU_OPTION_nics, | |
2597 | QEMU_OPTION_macaddr, | |
2598 | QEMU_OPTION_n, | |
2599 | QEMU_OPTION_tun_fd, | |
2600 | QEMU_OPTION_user_net, | |
2601 | QEMU_OPTION_tftp, | |
2602 | QEMU_OPTION_smb, | |
2603 | QEMU_OPTION_redir, | |
2604 | QEMU_OPTION_dummy_net, | |
2605 | ||
2606 | QEMU_OPTION_kernel, | |
2607 | QEMU_OPTION_append, | |
2608 | QEMU_OPTION_initrd, | |
2609 | ||
2610 | QEMU_OPTION_S, | |
2611 | QEMU_OPTION_s, | |
2612 | QEMU_OPTION_p, | |
2613 | QEMU_OPTION_d, | |
2614 | QEMU_OPTION_hdachs, | |
2615 | QEMU_OPTION_L, | |
2616 | QEMU_OPTION_no_code_copy, | |
2617 | QEMU_OPTION_pci, | |
2618 | QEMU_OPTION_isa, | |
2619 | QEMU_OPTION_prep, | |
2620 | QEMU_OPTION_localtime, | |
2621 | QEMU_OPTION_cirrusvga, | |
2622 | QEMU_OPTION_g, | |
2623 | QEMU_OPTION_std_vga, | |
2624 | QEMU_OPTION_monitor, | |
2625 | QEMU_OPTION_serial, | |
2626 | QEMU_OPTION_loadvm, | |
2627 | QEMU_OPTION_full_screen, | |
2628 | }; | |
2629 | ||
2630 | typedef struct QEMUOption { | |
2631 | const char *name; | |
2632 | int flags; | |
2633 | int index; | |
2634 | } QEMUOption; | |
2635 | ||
2636 | const QEMUOption qemu_options[] = { | |
2637 | { "h", 0, QEMU_OPTION_h }, | |
2638 | ||
2639 | { "fda", HAS_ARG, QEMU_OPTION_fda }, | |
2640 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, | |
2641 | { "hda", HAS_ARG, QEMU_OPTION_hda }, | |
2642 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, | |
2643 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, | |
2644 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, | |
2645 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, | |
2646 | { "boot", HAS_ARG, QEMU_OPTION_boot }, | |
2647 | { "snapshot", 0, QEMU_OPTION_snapshot }, | |
2648 | { "m", HAS_ARG, QEMU_OPTION_m }, | |
2649 | { "nographic", 0, QEMU_OPTION_nographic }, | |
2650 | { "enable-audio", 0, QEMU_OPTION_enable_audio }, | |
2651 | ||
2652 | { "nics", HAS_ARG, QEMU_OPTION_nics}, | |
2653 | { "macaddr", HAS_ARG, QEMU_OPTION_macaddr}, | |
2654 | { "n", HAS_ARG, QEMU_OPTION_n }, | |
2655 | { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd }, | |
2656 | #ifdef CONFIG_SLIRP | |
2657 | { "user-net", 0, QEMU_OPTION_user_net }, | |
2658 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, | |
2659 | #ifndef _WIN32 | |
2660 | { "smb", HAS_ARG, QEMU_OPTION_smb }, | |
2661 | #endif | |
2662 | { "redir", HAS_ARG, QEMU_OPTION_redir }, | |
2663 | #endif | |
2664 | { "dummy-net", 0, QEMU_OPTION_dummy_net }, | |
2665 | ||
2666 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, | |
2667 | { "append", HAS_ARG, QEMU_OPTION_append }, | |
2668 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, | |
2669 | ||
2670 | { "S", 0, QEMU_OPTION_S }, | |
2671 | { "s", 0, QEMU_OPTION_s }, | |
2672 | { "p", HAS_ARG, QEMU_OPTION_p }, | |
2673 | { "d", HAS_ARG, QEMU_OPTION_d }, | |
2674 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, | |
2675 | { "L", HAS_ARG, QEMU_OPTION_L }, | |
2676 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, | |
2677 | #ifdef TARGET_PPC | |
2678 | { "prep", 0, QEMU_OPTION_prep }, | |
2679 | { "g", 1, QEMU_OPTION_g }, | |
2680 | #endif | |
2681 | { "localtime", 0, QEMU_OPTION_localtime }, | |
2682 | { "isa", 0, QEMU_OPTION_isa }, | |
2683 | { "std-vga", 0, QEMU_OPTION_std_vga }, | |
2684 | { "monitor", 1, QEMU_OPTION_monitor }, | |
2685 | { "serial", 1, QEMU_OPTION_serial }, | |
2686 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, | |
2687 | { "full-screen", 0, QEMU_OPTION_full_screen }, | |
2688 | ||
2689 | /* temporary options */ | |
2690 | { "pci", 0, QEMU_OPTION_pci }, | |
2691 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, | |
2692 | { NULL }, | |
2693 | }; | |
2694 | ||
2695 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) | |
2696 | ||
2697 | /* this stack is only used during signal handling */ | |
2698 | #define SIGNAL_STACK_SIZE 32768 | |
2699 | ||
2700 | static uint8_t *signal_stack; | |
2701 | ||
2702 | #endif | |
2703 | ||
2704 | /* password input */ | |
2705 | ||
2706 | static BlockDriverState *get_bdrv(int index) | |
2707 | { | |
2708 | BlockDriverState *bs; | |
2709 | ||
2710 | if (index < 4) { | |
2711 | bs = bs_table[index]; | |
2712 | } else if (index < 6) { | |
2713 | bs = fd_table[index - 4]; | |
2714 | } else { | |
2715 | bs = NULL; | |
2716 | } | |
2717 | return bs; | |
2718 | } | |
2719 | ||
2720 | static void read_passwords(void) | |
2721 | { | |
2722 | BlockDriverState *bs; | |
2723 | int i, j; | |
2724 | char password[256]; | |
2725 | ||
2726 | for(i = 0; i < 6; i++) { | |
2727 | bs = get_bdrv(i); | |
2728 | if (bs && bdrv_is_encrypted(bs)) { | |
2729 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); | |
2730 | for(j = 0; j < 3; j++) { | |
2731 | monitor_readline("Password: ", | |
2732 | 1, password, sizeof(password)); | |
2733 | if (bdrv_set_key(bs, password) == 0) | |
2734 | break; | |
2735 | term_printf("invalid password\n"); | |
2736 | } | |
2737 | } | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | #define NET_IF_TUN 0 | |
2742 | #define NET_IF_USER 1 | |
2743 | #define NET_IF_DUMMY 2 | |
2744 | ||
2745 | int main(int argc, char **argv) | |
2746 | { | |
2747 | #ifdef CONFIG_GDBSTUB | |
2748 | int use_gdbstub, gdbstub_port; | |
2749 | #endif | |
2750 | int i, has_cdrom; | |
2751 | int snapshot, linux_boot; | |
2752 | CPUState *env; | |
2753 | const char *initrd_filename; | |
2754 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; | |
2755 | const char *kernel_filename, *kernel_cmdline; | |
2756 | DisplayState *ds = &display_state; | |
2757 | int cyls, heads, secs, translation; | |
2758 | int start_emulation = 1; | |
2759 | uint8_t macaddr[6]; | |
2760 | int net_if_type, nb_tun_fds, tun_fds[MAX_NICS]; | |
2761 | int optind; | |
2762 | const char *r, *optarg; | |
2763 | CharDriverState *monitor_hd; | |
2764 | char monitor_device[128]; | |
2765 | char serial_devices[MAX_SERIAL_PORTS][128]; | |
2766 | int serial_device_index; | |
2767 | const char *loadvm = NULL; | |
2768 | ||
2769 | #if !defined(CONFIG_SOFTMMU) | |
2770 | /* we never want that malloc() uses mmap() */ | |
2771 | mallopt(M_MMAP_THRESHOLD, 4096 * 1024); | |
2772 | #endif | |
2773 | initrd_filename = NULL; | |
2774 | for(i = 0; i < MAX_FD; i++) | |
2775 | fd_filename[i] = NULL; | |
2776 | for(i = 0; i < MAX_DISKS; i++) | |
2777 | hd_filename[i] = NULL; | |
2778 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; | |
2779 | vga_ram_size = VGA_RAM_SIZE; | |
2780 | bios_size = BIOS_SIZE; | |
2781 | pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT); | |
2782 | #ifdef CONFIG_GDBSTUB | |
2783 | use_gdbstub = 0; | |
2784 | gdbstub_port = DEFAULT_GDBSTUB_PORT; | |
2785 | #endif | |
2786 | snapshot = 0; | |
2787 | nographic = 0; | |
2788 | kernel_filename = NULL; | |
2789 | kernel_cmdline = ""; | |
2790 | has_cdrom = 1; | |
2791 | cyls = heads = secs = 0; | |
2792 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
2793 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); | |
2794 | ||
2795 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); | |
2796 | for(i = 1; i < MAX_SERIAL_PORTS; i++) | |
2797 | serial_devices[i][0] = '\0'; | |
2798 | serial_device_index = 0; | |
2799 | ||
2800 | nb_tun_fds = 0; | |
2801 | net_if_type = -1; | |
2802 | nb_nics = 1; | |
2803 | /* default mac address of the first network interface */ | |
2804 | macaddr[0] = 0x52; | |
2805 | macaddr[1] = 0x54; | |
2806 | macaddr[2] = 0x00; | |
2807 | macaddr[3] = 0x12; | |
2808 | macaddr[4] = 0x34; | |
2809 | macaddr[5] = 0x56; | |
2810 | ||
2811 | optind = 1; | |
2812 | for(;;) { | |
2813 | if (optind >= argc) | |
2814 | break; | |
2815 | r = argv[optind]; | |
2816 | if (r[0] != '-') { | |
2817 | hd_filename[0] = argv[optind++]; | |
2818 | } else { | |
2819 | const QEMUOption *popt; | |
2820 | ||
2821 | optind++; | |
2822 | popt = qemu_options; | |
2823 | for(;;) { | |
2824 | if (!popt->name) { | |
2825 | fprintf(stderr, "%s: invalid option -- '%s'\n", | |
2826 | argv[0], r); | |
2827 | exit(1); | |
2828 | } | |
2829 | if (!strcmp(popt->name, r + 1)) | |
2830 | break; | |
2831 | popt++; | |
2832 | } | |
2833 | if (popt->flags & HAS_ARG) { | |
2834 | if (optind >= argc) { | |
2835 | fprintf(stderr, "%s: option '%s' requires an argument\n", | |
2836 | argv[0], r); | |
2837 | exit(1); | |
2838 | } | |
2839 | optarg = argv[optind++]; | |
2840 | } else { | |
2841 | optarg = NULL; | |
2842 | } | |
2843 | ||
2844 | switch(popt->index) { | |
2845 | case QEMU_OPTION_initrd: | |
2846 | initrd_filename = optarg; | |
2847 | break; | |
2848 | case QEMU_OPTION_hda: | |
2849 | hd_filename[0] = optarg; | |
2850 | break; | |
2851 | case QEMU_OPTION_hdb: | |
2852 | hd_filename[1] = optarg; | |
2853 | break; | |
2854 | case QEMU_OPTION_snapshot: | |
2855 | snapshot = 1; | |
2856 | break; | |
2857 | case QEMU_OPTION_hdachs: | |
2858 | { | |
2859 | const char *p; | |
2860 | p = optarg; | |
2861 | cyls = strtol(p, (char **)&p, 0); | |
2862 | if (cyls < 1 || cyls > 16383) | |
2863 | goto chs_fail; | |
2864 | if (*p != ',') | |
2865 | goto chs_fail; | |
2866 | p++; | |
2867 | heads = strtol(p, (char **)&p, 0); | |
2868 | if (heads < 1 || heads > 16) | |
2869 | goto chs_fail; | |
2870 | if (*p != ',') | |
2871 | goto chs_fail; | |
2872 | p++; | |
2873 | secs = strtol(p, (char **)&p, 0); | |
2874 | if (secs < 1 || secs > 63) | |
2875 | goto chs_fail; | |
2876 | if (*p == ',') { | |
2877 | p++; | |
2878 | if (!strcmp(p, "none")) | |
2879 | translation = BIOS_ATA_TRANSLATION_NONE; | |
2880 | else if (!strcmp(p, "lba")) | |
2881 | translation = BIOS_ATA_TRANSLATION_LBA; | |
2882 | else if (!strcmp(p, "auto")) | |
2883 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
2884 | else | |
2885 | goto chs_fail; | |
2886 | } else if (*p != '\0') { | |
2887 | chs_fail: | |
2888 | fprintf(stderr, "qemu: invalid physical CHS format\n"); | |
2889 | exit(1); | |
2890 | } | |
2891 | } | |
2892 | break; | |
2893 | case QEMU_OPTION_nographic: | |
2894 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); | |
2895 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); | |
2896 | nographic = 1; | |
2897 | break; | |
2898 | case QEMU_OPTION_kernel: | |
2899 | kernel_filename = optarg; | |
2900 | break; | |
2901 | case QEMU_OPTION_append: | |
2902 | kernel_cmdline = optarg; | |
2903 | break; | |
2904 | case QEMU_OPTION_tun_fd: | |
2905 | { | |
2906 | const char *p; | |
2907 | int fd; | |
2908 | net_if_type = NET_IF_TUN; | |
2909 | if (nb_tun_fds < MAX_NICS) { | |
2910 | fd = strtol(optarg, (char **)&p, 0); | |
2911 | if (*p != '\0') { | |
2912 | fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds); | |
2913 | exit(1); | |
2914 | } | |
2915 | tun_fds[nb_tun_fds++] = fd; | |
2916 | } | |
2917 | } | |
2918 | break; | |
2919 | case QEMU_OPTION_hdc: | |
2920 | hd_filename[2] = optarg; | |
2921 | has_cdrom = 0; | |
2922 | break; | |
2923 | case QEMU_OPTION_hdd: | |
2924 | hd_filename[3] = optarg; | |
2925 | break; | |
2926 | case QEMU_OPTION_cdrom: | |
2927 | hd_filename[2] = optarg; | |
2928 | has_cdrom = 1; | |
2929 | break; | |
2930 | case QEMU_OPTION_boot: | |
2931 | boot_device = optarg[0]; | |
2932 | if (boot_device != 'a' && | |
2933 | boot_device != 'c' && boot_device != 'd') { | |
2934 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); | |
2935 | exit(1); | |
2936 | } | |
2937 | break; | |
2938 | case QEMU_OPTION_fda: | |
2939 | fd_filename[0] = optarg; | |
2940 | break; | |
2941 | case QEMU_OPTION_fdb: | |
2942 | fd_filename[1] = optarg; | |
2943 | break; | |
2944 | case QEMU_OPTION_no_code_copy: | |
2945 | code_copy_enabled = 0; | |
2946 | break; | |
2947 | case QEMU_OPTION_nics: | |
2948 | nb_nics = atoi(optarg); | |
2949 | if (nb_nics < 0 || nb_nics > MAX_NICS) { | |
2950 | fprintf(stderr, "qemu: invalid number of network interfaces\n"); | |
2951 | exit(1); | |
2952 | } | |
2953 | break; | |
2954 | case QEMU_OPTION_macaddr: | |
2955 | { | |
2956 | const char *p; | |
2957 | int i; | |
2958 | p = optarg; | |
2959 | for(i = 0; i < 6; i++) { | |
2960 | macaddr[i] = strtol(p, (char **)&p, 16); | |
2961 | if (i == 5) { | |
2962 | if (*p != '\0') | |
2963 | goto macaddr_error; | |
2964 | } else { | |
2965 | if (*p != ':') { | |
2966 | macaddr_error: | |
2967 | fprintf(stderr, "qemu: invalid syntax for ethernet address\n"); | |
2968 | exit(1); | |
2969 | } | |
2970 | p++; | |
2971 | } | |
2972 | } | |
2973 | } | |
2974 | break; | |
2975 | #ifdef CONFIG_SLIRP | |
2976 | case QEMU_OPTION_tftp: | |
2977 | tftp_prefix = optarg; | |
2978 | break; | |
2979 | #ifndef _WIN32 | |
2980 | case QEMU_OPTION_smb: | |
2981 | net_slirp_smb(optarg); | |
2982 | break; | |
2983 | #endif | |
2984 | case QEMU_OPTION_user_net: | |
2985 | net_if_type = NET_IF_USER; | |
2986 | break; | |
2987 | case QEMU_OPTION_redir: | |
2988 | net_slirp_redir(optarg); | |
2989 | break; | |
2990 | #endif | |
2991 | case QEMU_OPTION_dummy_net: | |
2992 | net_if_type = NET_IF_DUMMY; | |
2993 | break; | |
2994 | case QEMU_OPTION_enable_audio: | |
2995 | audio_enabled = 1; | |
2996 | break; | |
2997 | case QEMU_OPTION_h: | |
2998 | help(); | |
2999 | break; | |
3000 | case QEMU_OPTION_m: | |
3001 | ram_size = atoi(optarg) * 1024 * 1024; | |
3002 | if (ram_size <= 0) | |
3003 | help(); | |
3004 | if (ram_size > PHYS_RAM_MAX_SIZE) { | |
3005 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", | |
3006 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); | |
3007 | exit(1); | |
3008 | } | |
3009 | break; | |
3010 | case QEMU_OPTION_d: | |
3011 | { | |
3012 | int mask; | |
3013 | CPULogItem *item; | |
3014 | ||
3015 | mask = cpu_str_to_log_mask(optarg); | |
3016 | if (!mask) { | |
3017 | printf("Log items (comma separated):\n"); | |
3018 | for(item = cpu_log_items; item->mask != 0; item++) { | |
3019 | printf("%-10s %s\n", item->name, item->help); | |
3020 | } | |
3021 | exit(1); | |
3022 | } | |
3023 | cpu_set_log(mask); | |
3024 | } | |
3025 | break; | |
3026 | case QEMU_OPTION_n: | |
3027 | pstrcpy(network_script, sizeof(network_script), optarg); | |
3028 | break; | |
3029 | #ifdef CONFIG_GDBSTUB | |
3030 | case QEMU_OPTION_s: | |
3031 | use_gdbstub = 1; | |
3032 | break; | |
3033 | case QEMU_OPTION_p: | |
3034 | gdbstub_port = atoi(optarg); | |
3035 | break; | |
3036 | #endif | |
3037 | case QEMU_OPTION_L: | |
3038 | bios_dir = optarg; | |
3039 | break; | |
3040 | case QEMU_OPTION_S: | |
3041 | start_emulation = 0; | |
3042 | break; | |
3043 | case QEMU_OPTION_pci: | |
3044 | pci_enabled = 1; | |
3045 | break; | |
3046 | case QEMU_OPTION_isa: | |
3047 | pci_enabled = 0; | |
3048 | break; | |
3049 | case QEMU_OPTION_prep: | |
3050 | prep_enabled = 1; | |
3051 | break; | |
3052 | case QEMU_OPTION_localtime: | |
3053 | rtc_utc = 0; | |
3054 | break; | |
3055 | case QEMU_OPTION_cirrusvga: | |
3056 | cirrus_vga_enabled = 1; | |
3057 | break; | |
3058 | case QEMU_OPTION_std_vga: | |
3059 | cirrus_vga_enabled = 0; | |
3060 | break; | |
3061 | case QEMU_OPTION_g: | |
3062 | { | |
3063 | const char *p; | |
3064 | int w, h, depth; | |
3065 | p = optarg; | |
3066 | w = strtol(p, (char **)&p, 10); | |
3067 | if (w <= 0) { | |
3068 | graphic_error: | |
3069 | fprintf(stderr, "qemu: invalid resolution or depth\n"); | |
3070 | exit(1); | |
3071 | } | |
3072 | if (*p != 'x') | |
3073 | goto graphic_error; | |
3074 | p++; | |
3075 | h = strtol(p, (char **)&p, 10); | |
3076 | if (h <= 0) | |
3077 | goto graphic_error; | |
3078 | if (*p == 'x') { | |
3079 | p++; | |
3080 | depth = strtol(p, (char **)&p, 10); | |
3081 | if (depth != 8 && depth != 15 && depth != 16 && | |
3082 | depth != 24 && depth != 32) | |
3083 | goto graphic_error; | |
3084 | } else if (*p == '\0') { | |
3085 | depth = graphic_depth; | |
3086 | } else { | |
3087 | goto graphic_error; | |
3088 | } | |
3089 | ||
3090 | graphic_width = w; | |
3091 | graphic_height = h; | |
3092 | graphic_depth = depth; | |
3093 | } | |
3094 | break; | |
3095 | case QEMU_OPTION_monitor: | |
3096 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); | |
3097 | break; | |
3098 | case QEMU_OPTION_serial: | |
3099 | if (serial_device_index >= MAX_SERIAL_PORTS) { | |
3100 | fprintf(stderr, "qemu: too many serial ports\n"); | |
3101 | exit(1); | |
3102 | } | |
3103 | pstrcpy(serial_devices[serial_device_index], | |
3104 | sizeof(serial_devices[0]), optarg); | |
3105 | serial_device_index++; | |
3106 | break; | |
3107 | case QEMU_OPTION_loadvm: | |
3108 | loadvm = optarg; | |
3109 | break; | |
3110 | case QEMU_OPTION_full_screen: | |
3111 | full_screen = 1; | |
3112 | break; | |
3113 | } | |
3114 | } | |
3115 | } | |
3116 | ||
3117 | linux_boot = (kernel_filename != NULL); | |
3118 | ||
3119 | if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' && | |
3120 | fd_filename[0] == '\0') | |
3121 | help(); | |
3122 | ||
3123 | /* boot to cd by default if no hard disk */ | |
3124 | if (hd_filename[0] == '\0' && boot_device == 'c') { | |
3125 | if (fd_filename[0] != '\0') | |
3126 | boot_device = 'a'; | |
3127 | else | |
3128 | boot_device = 'd'; | |
3129 | } | |
3130 | ||
3131 | #if !defined(CONFIG_SOFTMMU) | |
3132 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ | |
3133 | { | |
3134 | static uint8_t stdout_buf[4096]; | |
3135 | setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf)); | |
3136 | } | |
3137 | #else | |
3138 | setvbuf(stdout, NULL, _IOLBF, 0); | |
3139 | #endif | |
3140 | ||
3141 | /* init host network redirectors */ | |
3142 | if (net_if_type == -1) { | |
3143 | net_if_type = NET_IF_TUN; | |
3144 | #if defined(CONFIG_SLIRP) | |
3145 | if (access(network_script, R_OK) < 0) { | |
3146 | net_if_type = NET_IF_USER; | |
3147 | } | |
3148 | #endif | |
3149 | } | |
3150 | ||
3151 | for(i = 0; i < nb_nics; i++) { | |
3152 | NetDriverState *nd = &nd_table[i]; | |
3153 | nd->index = i; | |
3154 | /* init virtual mac address */ | |
3155 | nd->macaddr[0] = macaddr[0]; | |
3156 | nd->macaddr[1] = macaddr[1]; | |
3157 | nd->macaddr[2] = macaddr[2]; | |
3158 | nd->macaddr[3] = macaddr[3]; | |
3159 | nd->macaddr[4] = macaddr[4]; | |
3160 | nd->macaddr[5] = macaddr[5] + i; | |
3161 | switch(net_if_type) { | |
3162 | #if defined(CONFIG_SLIRP) | |
3163 | case NET_IF_USER: | |
3164 | net_slirp_init(nd); | |
3165 | break; | |
3166 | #endif | |
3167 | #if !defined(_WIN32) | |
3168 | case NET_IF_TUN: | |
3169 | if (i < nb_tun_fds) { | |
3170 | net_fd_init(nd, tun_fds[i]); | |
3171 | } else { | |
3172 | if (net_tun_init(nd) < 0) | |
3173 | net_dummy_init(nd); | |
3174 | } | |
3175 | break; | |
3176 | #endif | |
3177 | case NET_IF_DUMMY: | |
3178 | default: | |
3179 | net_dummy_init(nd); | |
3180 | break; | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | /* init the memory */ | |
3185 | phys_ram_size = ram_size + vga_ram_size + bios_size; | |
3186 | ||
3187 | #ifdef CONFIG_SOFTMMU | |
3188 | #ifdef _BSD | |
3189 | /* mallocs are always aligned on BSD. valloc is better for correctness */ | |
3190 | phys_ram_base = valloc(phys_ram_size); | |
3191 | #else | |
3192 | phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size); | |
3193 | #endif | |
3194 | if (!phys_ram_base) { | |
3195 | fprintf(stderr, "Could not allocate physical memory\n"); | |
3196 | exit(1); | |
3197 | } | |
3198 | #else | |
3199 | /* as we must map the same page at several addresses, we must use | |
3200 | a fd */ | |
3201 | { | |
3202 | const char *tmpdir; | |
3203 | ||
3204 | tmpdir = getenv("QEMU_TMPDIR"); | |
3205 | if (!tmpdir) | |
3206 | tmpdir = "/tmp"; | |
3207 | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); | |
3208 | if (mkstemp(phys_ram_file) < 0) { | |
3209 | fprintf(stderr, "Could not create temporary memory file '%s'\n", | |
3210 | phys_ram_file); | |
3211 | exit(1); | |
3212 | } | |
3213 | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600); | |
3214 | if (phys_ram_fd < 0) { | |
3215 | fprintf(stderr, "Could not open temporary memory file '%s'\n", | |
3216 | phys_ram_file); | |
3217 | exit(1); | |
3218 | } | |
3219 | ftruncate(phys_ram_fd, phys_ram_size); | |
3220 | unlink(phys_ram_file); | |
3221 | phys_ram_base = mmap(get_mmap_addr(phys_ram_size), | |
3222 | phys_ram_size, | |
3223 | PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, | |
3224 | phys_ram_fd, 0); | |
3225 | if (phys_ram_base == MAP_FAILED) { | |
3226 | fprintf(stderr, "Could not map physical memory\n"); | |
3227 | exit(1); | |
3228 | } | |
3229 | } | |
3230 | #endif | |
3231 | ||
3232 | /* we always create the cdrom drive, even if no disk is there */ | |
3233 | bdrv_init(); | |
3234 | if (has_cdrom) { | |
3235 | bs_table[2] = bdrv_new("cdrom"); | |
3236 | bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM); | |
3237 | } | |
3238 | ||
3239 | /* open the virtual block devices */ | |
3240 | for(i = 0; i < MAX_DISKS; i++) { | |
3241 | if (hd_filename[i]) { | |
3242 | if (!bs_table[i]) { | |
3243 | char buf[64]; | |
3244 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); | |
3245 | bs_table[i] = bdrv_new(buf); | |
3246 | } | |
3247 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { | |
3248 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", | |
3249 | hd_filename[i]); | |
3250 | exit(1); | |
3251 | } | |
3252 | if (i == 0 && cyls != 0) { | |
3253 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); | |
3254 | bdrv_set_translation_hint(bs_table[i], translation); | |
3255 | } | |
3256 | } | |
3257 | } | |
3258 | ||
3259 | /* we always create at least one floppy disk */ | |
3260 | fd_table[0] = bdrv_new("fda"); | |
3261 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); | |
3262 | ||
3263 | for(i = 0; i < MAX_FD; i++) { | |
3264 | if (fd_filename[i]) { | |
3265 | if (!fd_table[i]) { | |
3266 | char buf[64]; | |
3267 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); | |
3268 | fd_table[i] = bdrv_new(buf); | |
3269 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); | |
3270 | } | |
3271 | if (fd_filename[i] != '\0') { | |
3272 | if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { | |
3273 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", | |
3274 | fd_filename[i]); | |
3275 | exit(1); | |
3276 | } | |
3277 | } | |
3278 | } | |
3279 | } | |
3280 | ||
3281 | /* init CPU state */ | |
3282 | env = cpu_init(); | |
3283 | global_env = env; | |
3284 | cpu_single_env = env; | |
3285 | ||
3286 | register_savevm("timer", 0, 1, timer_save, timer_load, env); | |
3287 | register_savevm("cpu", 0, 2, cpu_save, cpu_load, env); | |
3288 | register_savevm("ram", 0, 1, ram_save, ram_load, NULL); | |
3289 | qemu_register_reset(main_cpu_reset, global_env); | |
3290 | ||
3291 | init_ioports(); | |
3292 | cpu_calibrate_ticks(); | |
3293 | ||
3294 | /* terminal init */ | |
3295 | if (nographic) { | |
3296 | dumb_display_init(ds); | |
3297 | } else { | |
3298 | #ifdef CONFIG_SDL | |
3299 | sdl_display_init(ds, full_screen); | |
3300 | #else | |
3301 | dumb_display_init(ds); | |
3302 | #endif | |
3303 | } | |
3304 | ||
3305 | vga_console = graphic_console_init(ds); | |
3306 | ||
3307 | monitor_hd = qemu_chr_open(monitor_device); | |
3308 | if (!monitor_hd) { | |
3309 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); | |
3310 | exit(1); | |
3311 | } | |
3312 | monitor_init(monitor_hd, !nographic); | |
3313 | ||
3314 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { | |
3315 | if (serial_devices[i][0] != '\0') { | |
3316 | serial_hds[i] = qemu_chr_open(serial_devices[i]); | |
3317 | if (!serial_hds[i]) { | |
3318 | fprintf(stderr, "qemu: could not open serial device '%s'\n", | |
3319 | serial_devices[i]); | |
3320 | exit(1); | |
3321 | } | |
3322 | if (!strcmp(serial_devices[i], "vc")) | |
3323 | qemu_chr_printf(serial_hds[i], "serial%d console\n", i); | |
3324 | } | |
3325 | } | |
3326 | ||
3327 | /* setup cpu signal handlers for MMU / self modifying code handling */ | |
3328 | #if !defined(CONFIG_SOFTMMU) | |
3329 | ||
3330 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) | |
3331 | { | |
3332 | stack_t stk; | |
3333 | signal_stack = memalign(16, SIGNAL_STACK_SIZE); | |
3334 | stk.ss_sp = signal_stack; | |
3335 | stk.ss_size = SIGNAL_STACK_SIZE; | |
3336 | stk.ss_flags = 0; | |
3337 | ||
3338 | if (sigaltstack(&stk, NULL) < 0) { | |
3339 | perror("sigaltstack"); | |
3340 | exit(1); | |
3341 | } | |
3342 | } | |
3343 | #endif | |
3344 | { | |
3345 | struct sigaction act; | |
3346 | ||
3347 | sigfillset(&act.sa_mask); | |
3348 | act.sa_flags = SA_SIGINFO; | |
3349 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) | |
3350 | act.sa_flags |= SA_ONSTACK; | |
3351 | #endif | |
3352 | act.sa_sigaction = host_segv_handler; | |
3353 | sigaction(SIGSEGV, &act, NULL); | |
3354 | sigaction(SIGBUS, &act, NULL); | |
3355 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) | |
3356 | sigaction(SIGFPE, &act, NULL); | |
3357 | #endif | |
3358 | } | |
3359 | #endif | |
3360 | ||
3361 | #ifndef _WIN32 | |
3362 | { | |
3363 | struct sigaction act; | |
3364 | sigfillset(&act.sa_mask); | |
3365 | act.sa_flags = 0; | |
3366 | act.sa_handler = SIG_IGN; | |
3367 | sigaction(SIGPIPE, &act, NULL); | |
3368 | } | |
3369 | #endif | |
3370 | init_timers(); | |
3371 | ||
3372 | #if defined(TARGET_I386) | |
3373 | pc_init(ram_size, vga_ram_size, boot_device, | |
3374 | ds, fd_filename, snapshot, | |
3375 | kernel_filename, kernel_cmdline, initrd_filename); | |
3376 | #elif defined(TARGET_PPC) | |
3377 | ppc_init(ram_size, vga_ram_size, boot_device, | |
3378 | ds, fd_filename, snapshot, | |
3379 | kernel_filename, kernel_cmdline, initrd_filename); | |
3380 | #elif defined(TARGET_SPARC) | |
3381 | sun4m_init(ram_size, vga_ram_size, boot_device, | |
3382 | ds, fd_filename, snapshot, | |
3383 | kernel_filename, kernel_cmdline, initrd_filename); | |
3384 | #endif | |
3385 | ||
3386 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); | |
3387 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); | |
3388 | ||
3389 | #ifdef CONFIG_GDBSTUB | |
3390 | if (use_gdbstub) { | |
3391 | if (gdbserver_start(gdbstub_port) < 0) { | |
3392 | fprintf(stderr, "Could not open gdbserver socket on port %d\n", | |
3393 | gdbstub_port); | |
3394 | exit(1); | |
3395 | } else { | |
3396 | printf("Waiting gdb connection on port %d\n", gdbstub_port); | |
3397 | } | |
3398 | } else | |
3399 | #endif | |
3400 | if (loadvm) | |
3401 | qemu_loadvm(loadvm); | |
3402 | ||
3403 | { | |
3404 | /* XXX: simplify init */ | |
3405 | read_passwords(); | |
3406 | if (start_emulation) { | |
3407 | vm_start(); | |
3408 | } | |
3409 | } | |
3410 | main_loop(); | |
3411 | quit_timers(); | |
3412 | return 0; | |
3413 | } |