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