]> git.proxmox.com Git - qemu.git/blob - vl.c
removed unused code
[qemu.git] / vl.c
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 #ifdef _BSD
42 #include <sys/stat.h>
43 #ifndef __APPLE__
44 #include <libutil.h>
45 #endif
46 #else
47 #include <linux/if.h>
48 #include <linux/if_tun.h>
49 #include <pty.h>
50 #include <malloc.h>
51 #include <linux/rtc.h>
52 #endif
53 #endif
54
55 #if defined(CONFIG_SLIRP)
56 #include "libslirp.h"
57 #endif
58
59 #ifdef _WIN32
60 #include <malloc.h>
61 #include <sys/timeb.h>
62 #include <windows.h>
63 #define getopt_long_only getopt_long
64 #define memalign(align, size) malloc(size)
65 #endif
66
67 #ifdef CONFIG_SDL
68 #include <SDL/SDL.h>
69 #if defined(__linux__)
70 /* SDL use the pthreads and they modify sigaction. We don't
71 want that. */
72 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
73 extern void __libc_sigaction();
74 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
75 #else
76 extern void __sigaction();
77 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
78 #endif
79 #endif /* __linux__ */
80 #endif /* CONFIG_SDL */
81
82 #include "disas.h"
83
84 #include "exec-all.h"
85
86 //#define DO_TB_FLUSH
87
88 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
89
90 //#define DEBUG_UNUSED_IOPORT
91 //#define DEBUG_IOPORT
92
93 #if !defined(CONFIG_SOFTMMU)
94 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
95 #else
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
97 #endif
98
99 #ifdef TARGET_PPC
100 #define DEFAULT_RAM_SIZE 144
101 #else
102 #define DEFAULT_RAM_SIZE 32
103 #endif
104 /* in ms */
105 #define GUI_REFRESH_INTERVAL 30
106
107 /* XXX: use a two level table to limit memory usage */
108 #define MAX_IOPORTS 65536
109
110 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
111 char phys_ram_file[1024];
112 CPUState *global_env;
113 CPUState *cpu_single_env;
114 void *ioport_opaque[MAX_IOPORTS];
115 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
116 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
117 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
118 int vga_ram_size;
119 int bios_size;
120 static DisplayState display_state;
121 int nographic;
122 int64_t ticks_per_sec;
123 int boot_device = 'c';
124 int ram_size;
125 static char network_script[1024];
126 int pit_min_timer_count = 0;
127 int nb_nics;
128 NetDriverState nd_table[MAX_NICS];
129 SerialState *serial_console;
130 QEMUTimer *gui_timer;
131 int vm_running;
132 int audio_enabled = 0;
133 int pci_enabled = 1;
134 int prep_enabled = 0;
135 int rtc_utc = 1;
136 int cirrus_vga_enabled = 0;
137 int graphic_width = 640;
138 int graphic_height = 480;
139 int graphic_depth = 15;
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 void pstrcpy(char *buf, int buf_size, const char *str)
273 {
274 int c;
275 char *q = buf;
276
277 if (buf_size <= 0)
278 return;
279
280 for(;;) {
281 c = *str++;
282 if (c == 0 || q >= buf + buf_size - 1)
283 break;
284 *q++ = c;
285 }
286 *q = '\0';
287 }
288
289 /* strcat and truncate. */
290 char *pstrcat(char *buf, int buf_size, const char *s)
291 {
292 int len;
293 len = strlen(buf);
294 if (len < buf_size)
295 pstrcpy(buf + len, buf_size - len, s);
296 return buf;
297 }
298
299 /* return the size or -1 if error */
300 int get_image_size(const char *filename)
301 {
302 int fd, size;
303 fd = open(filename, O_RDONLY | O_BINARY);
304 if (fd < 0)
305 return -1;
306 size = lseek(fd, 0, SEEK_END);
307 close(fd);
308 return size;
309 }
310
311 /* return the size or -1 if error */
312 int load_image(const char *filename, uint8_t *addr)
313 {
314 int fd, size;
315 fd = open(filename, O_RDONLY | O_BINARY);
316 if (fd < 0)
317 return -1;
318 size = lseek(fd, 0, SEEK_END);
319 lseek(fd, 0, SEEK_SET);
320 if (read(fd, addr, size) != size) {
321 close(fd);
322 return -1;
323 }
324 close(fd);
325 return size;
326 }
327
328 void cpu_outb(CPUState *env, int addr, int val)
329 {
330 #ifdef DEBUG_IOPORT
331 if (loglevel & CPU_LOG_IOPORT)
332 fprintf(logfile, "outb: %04x %02x\n", addr, val);
333 #endif
334 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
335 }
336
337 void cpu_outw(CPUState *env, int addr, int val)
338 {
339 #ifdef DEBUG_IOPORT
340 if (loglevel & CPU_LOG_IOPORT)
341 fprintf(logfile, "outw: %04x %04x\n", addr, val);
342 #endif
343 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
344 }
345
346 void cpu_outl(CPUState *env, int addr, int val)
347 {
348 #ifdef DEBUG_IOPORT
349 if (loglevel & CPU_LOG_IOPORT)
350 fprintf(logfile, "outl: %04x %08x\n", addr, val);
351 #endif
352 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
353 }
354
355 int cpu_inb(CPUState *env, int addr)
356 {
357 int val;
358 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
359 #ifdef DEBUG_IOPORT
360 if (loglevel & CPU_LOG_IOPORT)
361 fprintf(logfile, "inb : %04x %02x\n", addr, val);
362 #endif
363 return val;
364 }
365
366 int cpu_inw(CPUState *env, int addr)
367 {
368 int val;
369 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
370 #ifdef DEBUG_IOPORT
371 if (loglevel & CPU_LOG_IOPORT)
372 fprintf(logfile, "inw : %04x %04x\n", addr, val);
373 #endif
374 return val;
375 }
376
377 int cpu_inl(CPUState *env, int addr)
378 {
379 int val;
380 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
381 #ifdef DEBUG_IOPORT
382 if (loglevel & CPU_LOG_IOPORT)
383 fprintf(logfile, "inl : %04x %08x\n", addr, val);
384 #endif
385 return val;
386 }
387
388 /***********************************************************/
389 void hw_error(const char *fmt, ...)
390 {
391 va_list ap;
392
393 va_start(ap, fmt);
394 fprintf(stderr, "qemu: hardware error: ");
395 vfprintf(stderr, fmt, ap);
396 fprintf(stderr, "\n");
397 #ifdef TARGET_I386
398 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
399 #else
400 cpu_dump_state(global_env, stderr, 0);
401 #endif
402 va_end(ap);
403 abort();
404 }
405
406 /***********************************************************/
407 /* keyboard/mouse */
408
409 static QEMUPutKBDEvent *qemu_put_kbd_event;
410 static void *qemu_put_kbd_event_opaque;
411 static QEMUPutMouseEvent *qemu_put_mouse_event;
412 static void *qemu_put_mouse_event_opaque;
413
414 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
415 {
416 qemu_put_kbd_event_opaque = opaque;
417 qemu_put_kbd_event = func;
418 }
419
420 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
421 {
422 qemu_put_mouse_event_opaque = opaque;
423 qemu_put_mouse_event = func;
424 }
425
426 void kbd_put_keycode(int keycode)
427 {
428 if (qemu_put_kbd_event) {
429 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
430 }
431 }
432
433 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
434 {
435 if (qemu_put_mouse_event) {
436 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
437 dx, dy, dz, buttons_state);
438 }
439 }
440
441 /***********************************************************/
442 /* timers */
443
444 #if defined(__powerpc__)
445
446 static inline uint32_t get_tbl(void)
447 {
448 uint32_t tbl;
449 asm volatile("mftb %0" : "=r" (tbl));
450 return tbl;
451 }
452
453 static inline uint32_t get_tbu(void)
454 {
455 uint32_t tbl;
456 asm volatile("mftbu %0" : "=r" (tbl));
457 return tbl;
458 }
459
460 int64_t cpu_get_real_ticks(void)
461 {
462 uint32_t l, h, h1;
463 /* NOTE: we test if wrapping has occurred */
464 do {
465 h = get_tbu();
466 l = get_tbl();
467 h1 = get_tbu();
468 } while (h != h1);
469 return ((int64_t)h << 32) | l;
470 }
471
472 #elif defined(__i386__)
473
474 int64_t cpu_get_real_ticks(void)
475 {
476 int64_t val;
477 asm volatile ("rdtsc" : "=A" (val));
478 return val;
479 }
480
481 #elif defined(__x86_64__)
482
483 int64_t cpu_get_real_ticks(void)
484 {
485 uint32_t low,high;
486 int64_t val;
487 asm volatile("rdtsc" : "=a" (low), "=d" (high));
488 val = high;
489 val <<= 32;
490 val |= low;
491 return val;
492 }
493
494 #else
495 #error unsupported CPU
496 #endif
497
498 static int64_t cpu_ticks_offset;
499 static int cpu_ticks_enabled;
500
501 static inline int64_t cpu_get_ticks(void)
502 {
503 if (!cpu_ticks_enabled) {
504 return cpu_ticks_offset;
505 } else {
506 return cpu_get_real_ticks() + cpu_ticks_offset;
507 }
508 }
509
510 /* enable cpu_get_ticks() */
511 void cpu_enable_ticks(void)
512 {
513 if (!cpu_ticks_enabled) {
514 cpu_ticks_offset -= cpu_get_real_ticks();
515 cpu_ticks_enabled = 1;
516 }
517 }
518
519 /* disable cpu_get_ticks() : the clock is stopped. You must not call
520 cpu_get_ticks() after that. */
521 void cpu_disable_ticks(void)
522 {
523 if (cpu_ticks_enabled) {
524 cpu_ticks_offset = cpu_get_ticks();
525 cpu_ticks_enabled = 0;
526 }
527 }
528
529 static int64_t get_clock(void)
530 {
531 #ifdef _WIN32
532 struct _timeb tb;
533 _ftime(&tb);
534 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
535 #else
536 struct timeval tv;
537 gettimeofday(&tv, NULL);
538 return tv.tv_sec * 1000000LL + tv.tv_usec;
539 #endif
540 }
541
542 void cpu_calibrate_ticks(void)
543 {
544 int64_t usec, ticks;
545
546 usec = get_clock();
547 ticks = cpu_get_real_ticks();
548 #ifdef _WIN32
549 Sleep(50);
550 #else
551 usleep(50 * 1000);
552 #endif
553 usec = get_clock() - usec;
554 ticks = cpu_get_real_ticks() - ticks;
555 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
556 }
557
558 /* compute with 96 bit intermediate result: (a*b)/c */
559 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
560 {
561 union {
562 uint64_t ll;
563 struct {
564 #ifdef WORDS_BIGENDIAN
565 uint32_t high, low;
566 #else
567 uint32_t low, high;
568 #endif
569 } l;
570 } u, res;
571 uint64_t rl, rh;
572
573 u.ll = a;
574 rl = (uint64_t)u.l.low * (uint64_t)b;
575 rh = (uint64_t)u.l.high * (uint64_t)b;
576 rh += (rl >> 32);
577 res.l.high = rh / c;
578 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
579 return res.ll;
580 }
581
582 #define QEMU_TIMER_REALTIME 0
583 #define QEMU_TIMER_VIRTUAL 1
584
585 struct QEMUClock {
586 int type;
587 /* XXX: add frequency */
588 };
589
590 struct QEMUTimer {
591 QEMUClock *clock;
592 int64_t expire_time;
593 QEMUTimerCB *cb;
594 void *opaque;
595 struct QEMUTimer *next;
596 };
597
598 QEMUClock *rt_clock;
599 QEMUClock *vm_clock;
600
601 static QEMUTimer *active_timers[2];
602 #ifdef _WIN32
603 static MMRESULT timerID;
604 #else
605 /* frequency of the times() clock tick */
606 static int timer_freq;
607 #endif
608
609 QEMUClock *qemu_new_clock(int type)
610 {
611 QEMUClock *clock;
612 clock = qemu_mallocz(sizeof(QEMUClock));
613 if (!clock)
614 return NULL;
615 clock->type = type;
616 return clock;
617 }
618
619 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
620 {
621 QEMUTimer *ts;
622
623 ts = qemu_mallocz(sizeof(QEMUTimer));
624 ts->clock = clock;
625 ts->cb = cb;
626 ts->opaque = opaque;
627 return ts;
628 }
629
630 void qemu_free_timer(QEMUTimer *ts)
631 {
632 qemu_free(ts);
633 }
634
635 /* stop a timer, but do not dealloc it */
636 void qemu_del_timer(QEMUTimer *ts)
637 {
638 QEMUTimer **pt, *t;
639
640 /* NOTE: this code must be signal safe because
641 qemu_timer_expired() can be called from a signal. */
642 pt = &active_timers[ts->clock->type];
643 for(;;) {
644 t = *pt;
645 if (!t)
646 break;
647 if (t == ts) {
648 *pt = t->next;
649 break;
650 }
651 pt = &t->next;
652 }
653 }
654
655 /* modify the current timer so that it will be fired when current_time
656 >= expire_time. The corresponding callback will be called. */
657 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
658 {
659 QEMUTimer **pt, *t;
660
661 qemu_del_timer(ts);
662
663 /* add the timer in the sorted list */
664 /* NOTE: this code must be signal safe because
665 qemu_timer_expired() can be called from a signal. */
666 pt = &active_timers[ts->clock->type];
667 for(;;) {
668 t = *pt;
669 if (!t)
670 break;
671 if (t->expire_time > expire_time)
672 break;
673 pt = &t->next;
674 }
675 ts->expire_time = expire_time;
676 ts->next = *pt;
677 *pt = ts;
678 }
679
680 int qemu_timer_pending(QEMUTimer *ts)
681 {
682 QEMUTimer *t;
683 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
684 if (t == ts)
685 return 1;
686 }
687 return 0;
688 }
689
690 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
691 {
692 if (!timer_head)
693 return 0;
694 return (timer_head->expire_time <= current_time);
695 }
696
697 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
698 {
699 QEMUTimer *ts;
700
701 for(;;) {
702 ts = *ptimer_head;
703 if (ts->expire_time > current_time)
704 break;
705 /* remove timer from the list before calling the callback */
706 *ptimer_head = ts->next;
707 ts->next = NULL;
708
709 /* run the callback (the timer list can be modified) */
710 ts->cb(ts->opaque);
711 }
712 }
713
714 int64_t qemu_get_clock(QEMUClock *clock)
715 {
716 switch(clock->type) {
717 case QEMU_TIMER_REALTIME:
718 #ifdef _WIN32
719 return GetTickCount();
720 #else
721 {
722 struct tms tp;
723
724 /* Note that using gettimeofday() is not a good solution
725 for timers because its value change when the date is
726 modified. */
727 if (timer_freq == 100) {
728 return times(&tp) * 10;
729 } else {
730 return ((int64_t)times(&tp) * 1000) / timer_freq;
731 }
732 }
733 #endif
734 default:
735 case QEMU_TIMER_VIRTUAL:
736 return cpu_get_ticks();
737 }
738 }
739
740 /* save a timer */
741 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
742 {
743 uint64_t expire_time;
744
745 if (qemu_timer_pending(ts)) {
746 expire_time = ts->expire_time;
747 } else {
748 expire_time = -1;
749 }
750 qemu_put_be64(f, expire_time);
751 }
752
753 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
754 {
755 uint64_t expire_time;
756
757 expire_time = qemu_get_be64(f);
758 if (expire_time != -1) {
759 qemu_mod_timer(ts, expire_time);
760 } else {
761 qemu_del_timer(ts);
762 }
763 }
764
765 static void timer_save(QEMUFile *f, void *opaque)
766 {
767 if (cpu_ticks_enabled) {
768 hw_error("cannot save state if virtual timers are running");
769 }
770 qemu_put_be64s(f, &cpu_ticks_offset);
771 qemu_put_be64s(f, &ticks_per_sec);
772 }
773
774 static int timer_load(QEMUFile *f, void *opaque, int version_id)
775 {
776 if (version_id != 1)
777 return -EINVAL;
778 if (cpu_ticks_enabled) {
779 return -EINVAL;
780 }
781 qemu_get_be64s(f, &cpu_ticks_offset);
782 qemu_get_be64s(f, &ticks_per_sec);
783 return 0;
784 }
785
786 #ifdef _WIN32
787 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
788 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
789 #else
790 static void host_alarm_handler(int host_signum)
791 #endif
792 {
793 #if 0
794 #define DISP_FREQ 1000
795 {
796 static int64_t delta_min = INT64_MAX;
797 static int64_t delta_max, delta_cum, last_clock, delta, ti;
798 static int count;
799 ti = qemu_get_clock(vm_clock);
800 if (last_clock != 0) {
801 delta = ti - last_clock;
802 if (delta < delta_min)
803 delta_min = delta;
804 if (delta > delta_max)
805 delta_max = delta;
806 delta_cum += delta;
807 if (++count == DISP_FREQ) {
808 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
809 muldiv64(delta_min, 1000000, ticks_per_sec),
810 muldiv64(delta_max, 1000000, ticks_per_sec),
811 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
812 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
813 count = 0;
814 delta_min = INT64_MAX;
815 delta_max = 0;
816 delta_cum = 0;
817 }
818 }
819 last_clock = ti;
820 }
821 #endif
822 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
823 qemu_get_clock(vm_clock)) ||
824 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
825 qemu_get_clock(rt_clock))) {
826 /* stop the cpu because a timer occured */
827 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
828 }
829 }
830
831 #ifndef _WIN32
832
833 #if defined(__linux__)
834
835 #define RTC_FREQ 1024
836
837 static int rtc_fd;
838
839 static int start_rtc_timer(void)
840 {
841 rtc_fd = open("/dev/rtc", O_RDONLY);
842 if (rtc_fd < 0)
843 return -1;
844 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
845 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
846 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
847 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
848 goto fail;
849 }
850 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
851 fail:
852 close(rtc_fd);
853 return -1;
854 }
855 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
856 return 0;
857 }
858
859 #else
860
861 static int start_rtc_timer(void)
862 {
863 return -1;
864 }
865
866 #endif /* !defined(__linux__) */
867
868 #endif /* !defined(_WIN32) */
869
870 static void init_timers(void)
871 {
872 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
873 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
874
875 #ifdef _WIN32
876 {
877 int count=0;
878 timerID = timeSetEvent(10, // interval (ms)
879 0, // resolution
880 host_alarm_handler, // function
881 (DWORD)&count, // user parameter
882 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
883 if( !timerID ) {
884 perror("failed timer alarm");
885 exit(1);
886 }
887 }
888 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
889 #else
890 {
891 struct sigaction act;
892 struct itimerval itv;
893
894 /* get times() syscall frequency */
895 timer_freq = sysconf(_SC_CLK_TCK);
896
897 /* timer signal */
898 sigfillset(&act.sa_mask);
899 act.sa_flags = 0;
900 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
901 act.sa_flags |= SA_ONSTACK;
902 #endif
903 act.sa_handler = host_alarm_handler;
904 sigaction(SIGALRM, &act, NULL);
905
906 itv.it_interval.tv_sec = 0;
907 itv.it_interval.tv_usec = 1000;
908 itv.it_value.tv_sec = 0;
909 itv.it_value.tv_usec = 10 * 1000;
910 setitimer(ITIMER_REAL, &itv, NULL);
911 /* we probe the tick duration of the kernel to inform the user if
912 the emulated kernel requested a too high timer frequency */
913 getitimer(ITIMER_REAL, &itv);
914
915 #if defined(__linux__)
916 if (itv.it_interval.tv_usec > 1000) {
917 /* try to use /dev/rtc to have a faster timer */
918 if (start_rtc_timer() < 0)
919 goto use_itimer;
920 /* disable itimer */
921 itv.it_interval.tv_sec = 0;
922 itv.it_interval.tv_usec = 0;
923 itv.it_value.tv_sec = 0;
924 itv.it_value.tv_usec = 0;
925 setitimer(ITIMER_REAL, &itv, NULL);
926
927 /* use the RTC */
928 sigaction(SIGIO, &act, NULL);
929 fcntl(rtc_fd, F_SETFL, O_ASYNC);
930 fcntl(rtc_fd, F_SETOWN, getpid());
931 } else
932 #endif /* defined(__linux__) */
933 {
934 use_itimer:
935 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
936 PIT_FREQ) / 1000000;
937 }
938 }
939 #endif
940 }
941
942 void quit_timers(void)
943 {
944 #ifdef _WIN32
945 timeKillEvent(timerID);
946 #endif
947 }
948
949 /***********************************************************/
950 /* serial device */
951
952 #ifdef _WIN32
953
954 int serial_open_device(void)
955 {
956 return -1;
957 }
958
959 #else
960
961 int serial_open_device(void)
962 {
963 char slave_name[1024];
964 int master_fd, slave_fd;
965
966 if (serial_console == NULL && nographic) {
967 /* use console for serial port */
968 return 0;
969 } else {
970 #if 0
971 /* Not satisfying */
972 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
973 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
974 return -1;
975 }
976 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
977 return master_fd;
978 #else
979 return -1;
980 #endif
981 }
982 }
983
984 #endif
985
986 /***********************************************************/
987 /* Linux network device redirectors */
988
989 void hex_dump(FILE *f, const uint8_t *buf, int size)
990 {
991 int len, i, j, c;
992
993 for(i=0;i<size;i+=16) {
994 len = size - i;
995 if (len > 16)
996 len = 16;
997 fprintf(f, "%08x ", i);
998 for(j=0;j<16;j++) {
999 if (j < len)
1000 fprintf(f, " %02x", buf[i+j]);
1001 else
1002 fprintf(f, " ");
1003 }
1004 fprintf(f, " ");
1005 for(j=0;j<len;j++) {
1006 c = buf[i+j];
1007 if (c < ' ' || c > '~')
1008 c = '.';
1009 fprintf(f, "%c", c);
1010 }
1011 fprintf(f, "\n");
1012 }
1013 }
1014
1015 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1016 {
1017 nd->send_packet(nd, buf, size);
1018 }
1019
1020 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1021 IOReadHandler *fd_read, void *opaque)
1022 {
1023 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1024 }
1025
1026 /* dummy network adapter */
1027
1028 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1029 {
1030 }
1031
1032 static void dummy_add_read_packet(NetDriverState *nd,
1033 IOCanRWHandler *fd_can_read,
1034 IOReadHandler *fd_read, void *opaque)
1035 {
1036 }
1037
1038 static int net_dummy_init(NetDriverState *nd)
1039 {
1040 nd->send_packet = dummy_send_packet;
1041 nd->add_read_packet = dummy_add_read_packet;
1042 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1043 return 0;
1044 }
1045
1046 #if defined(CONFIG_SLIRP)
1047
1048 /* slirp network adapter */
1049
1050 static void *slirp_fd_opaque;
1051 static IOCanRWHandler *slirp_fd_can_read;
1052 static IOReadHandler *slirp_fd_read;
1053 static int slirp_inited;
1054
1055 int slirp_can_output(void)
1056 {
1057 return slirp_fd_can_read(slirp_fd_opaque);
1058 }
1059
1060 void slirp_output(const uint8_t *pkt, int pkt_len)
1061 {
1062 #if 0
1063 printf("output:\n");
1064 hex_dump(stdout, pkt, pkt_len);
1065 #endif
1066 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1067 }
1068
1069 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1070 {
1071 #if 0
1072 printf("input:\n");
1073 hex_dump(stdout, buf, size);
1074 #endif
1075 slirp_input(buf, size);
1076 }
1077
1078 static void slirp_add_read_packet(NetDriverState *nd,
1079 IOCanRWHandler *fd_can_read,
1080 IOReadHandler *fd_read, void *opaque)
1081 {
1082 slirp_fd_opaque = opaque;
1083 slirp_fd_can_read = fd_can_read;
1084 slirp_fd_read = fd_read;
1085 }
1086
1087 static int net_slirp_init(NetDriverState *nd)
1088 {
1089 if (!slirp_inited) {
1090 slirp_inited = 1;
1091 slirp_init();
1092 }
1093 nd->send_packet = slirp_send_packet;
1094 nd->add_read_packet = slirp_add_read_packet;
1095 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1096 return 0;
1097 }
1098
1099 #endif /* CONFIG_SLIRP */
1100
1101 #if !defined(_WIN32)
1102 #ifdef _BSD
1103 static int tun_open(char *ifname, int ifname_size)
1104 {
1105 int fd;
1106 char *dev;
1107 struct stat s;
1108
1109 fd = open("/dev/tap", O_RDWR);
1110 if (fd < 0) {
1111 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1112 return -1;
1113 }
1114
1115 fstat(fd, &s);
1116 dev = devname(s.st_rdev, S_IFCHR);
1117 pstrcpy(ifname, ifname_size, dev);
1118
1119 fcntl(fd, F_SETFL, O_NONBLOCK);
1120 return fd;
1121 }
1122 #else
1123 static int tun_open(char *ifname, int ifname_size)
1124 {
1125 struct ifreq ifr;
1126 int fd, ret;
1127
1128 fd = open("/dev/net/tun", O_RDWR);
1129 if (fd < 0) {
1130 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1131 return -1;
1132 }
1133 memset(&ifr, 0, sizeof(ifr));
1134 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1135 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1136 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1137 if (ret != 0) {
1138 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1139 close(fd);
1140 return -1;
1141 }
1142 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1143 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1144 fcntl(fd, F_SETFL, O_NONBLOCK);
1145 return fd;
1146 }
1147 #endif
1148
1149 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1150 {
1151 write(nd->fd, buf, size);
1152 }
1153
1154 static void tun_add_read_packet(NetDriverState *nd,
1155 IOCanRWHandler *fd_can_read,
1156 IOReadHandler *fd_read, void *opaque)
1157 {
1158 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1159 }
1160
1161 static int net_tun_init(NetDriverState *nd)
1162 {
1163 int pid, status;
1164 char *args[3];
1165 char **parg;
1166
1167 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1168 if (nd->fd < 0)
1169 return -1;
1170
1171 /* try to launch network init script */
1172 pid = fork();
1173 if (pid >= 0) {
1174 if (pid == 0) {
1175 parg = args;
1176 *parg++ = network_script;
1177 *parg++ = nd->ifname;
1178 *parg++ = NULL;
1179 execv(network_script, args);
1180 exit(1);
1181 }
1182 while (waitpid(pid, &status, 0) != pid);
1183 if (!WIFEXITED(status) ||
1184 WEXITSTATUS(status) != 0) {
1185 fprintf(stderr, "%s: could not launch network script\n",
1186 network_script);
1187 }
1188 }
1189 nd->send_packet = tun_send_packet;
1190 nd->add_read_packet = tun_add_read_packet;
1191 return 0;
1192 }
1193
1194 static int net_fd_init(NetDriverState *nd, int fd)
1195 {
1196 nd->fd = fd;
1197 nd->send_packet = tun_send_packet;
1198 nd->add_read_packet = tun_add_read_packet;
1199 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1200 return 0;
1201 }
1202
1203 #endif /* !_WIN32 */
1204
1205 /***********************************************************/
1206 /* dumb display */
1207
1208 #ifdef _WIN32
1209
1210 static void term_exit(void)
1211 {
1212 }
1213
1214 static void term_init(void)
1215 {
1216 }
1217
1218 #else
1219
1220 /* init terminal so that we can grab keys */
1221 static struct termios oldtty;
1222 static int old_fd0_flags;
1223
1224 static void term_exit(void)
1225 {
1226 tcsetattr (0, TCSANOW, &oldtty);
1227 fcntl(0, F_SETFL, old_fd0_flags);
1228 }
1229
1230 static void term_init(void)
1231 {
1232 struct termios tty;
1233
1234 tcgetattr (0, &tty);
1235 oldtty = tty;
1236 old_fd0_flags = fcntl(0, F_GETFL);
1237
1238 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1239 |INLCR|IGNCR|ICRNL|IXON);
1240 tty.c_oflag |= OPOST;
1241 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1242 /* if graphical mode, we allow Ctrl-C handling */
1243 if (nographic)
1244 tty.c_lflag &= ~ISIG;
1245 tty.c_cflag &= ~(CSIZE|PARENB);
1246 tty.c_cflag |= CS8;
1247 tty.c_cc[VMIN] = 1;
1248 tty.c_cc[VTIME] = 0;
1249
1250 tcsetattr (0, TCSANOW, &tty);
1251
1252 atexit(term_exit);
1253
1254 fcntl(0, F_SETFL, O_NONBLOCK);
1255 }
1256
1257 #endif
1258
1259 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1260 {
1261 }
1262
1263 static void dumb_resize(DisplayState *ds, int w, int h)
1264 {
1265 }
1266
1267 static void dumb_refresh(DisplayState *ds)
1268 {
1269 vga_update_display();
1270 }
1271
1272 void dumb_display_init(DisplayState *ds)
1273 {
1274 ds->data = NULL;
1275 ds->linesize = 0;
1276 ds->depth = 0;
1277 ds->dpy_update = dumb_update;
1278 ds->dpy_resize = dumb_resize;
1279 ds->dpy_refresh = dumb_refresh;
1280 }
1281
1282 #if !defined(CONFIG_SOFTMMU)
1283 /***********************************************************/
1284 /* cpu signal handler */
1285 static void host_segv_handler(int host_signum, siginfo_t *info,
1286 void *puc)
1287 {
1288 if (cpu_signal_handler(host_signum, info, puc))
1289 return;
1290 term_exit();
1291 abort();
1292 }
1293 #endif
1294
1295 /***********************************************************/
1296 /* I/O handling */
1297
1298 #define MAX_IO_HANDLERS 64
1299
1300 typedef struct IOHandlerRecord {
1301 int fd;
1302 IOCanRWHandler *fd_can_read;
1303 IOReadHandler *fd_read;
1304 void *opaque;
1305 /* temporary data */
1306 struct pollfd *ufd;
1307 int max_size;
1308 struct IOHandlerRecord *next;
1309 } IOHandlerRecord;
1310
1311 static IOHandlerRecord *first_io_handler;
1312
1313 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1314 IOReadHandler *fd_read, void *opaque)
1315 {
1316 IOHandlerRecord *ioh;
1317
1318 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1319 if (!ioh)
1320 return -1;
1321 ioh->fd = fd;
1322 ioh->fd_can_read = fd_can_read;
1323 ioh->fd_read = fd_read;
1324 ioh->opaque = opaque;
1325 ioh->next = first_io_handler;
1326 first_io_handler = ioh;
1327 return 0;
1328 }
1329
1330 void qemu_del_fd_read_handler(int fd)
1331 {
1332 IOHandlerRecord **pioh, *ioh;
1333
1334 pioh = &first_io_handler;
1335 for(;;) {
1336 ioh = *pioh;
1337 if (ioh == NULL)
1338 break;
1339 if (ioh->fd == fd) {
1340 *pioh = ioh->next;
1341 break;
1342 }
1343 pioh = &ioh->next;
1344 }
1345 }
1346
1347 /***********************************************************/
1348 /* savevm/loadvm support */
1349
1350 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1351 {
1352 fwrite(buf, 1, size, f);
1353 }
1354
1355 void qemu_put_byte(QEMUFile *f, int v)
1356 {
1357 fputc(v, f);
1358 }
1359
1360 void qemu_put_be16(QEMUFile *f, unsigned int v)
1361 {
1362 qemu_put_byte(f, v >> 8);
1363 qemu_put_byte(f, v);
1364 }
1365
1366 void qemu_put_be32(QEMUFile *f, unsigned int v)
1367 {
1368 qemu_put_byte(f, v >> 24);
1369 qemu_put_byte(f, v >> 16);
1370 qemu_put_byte(f, v >> 8);
1371 qemu_put_byte(f, v);
1372 }
1373
1374 void qemu_put_be64(QEMUFile *f, uint64_t v)
1375 {
1376 qemu_put_be32(f, v >> 32);
1377 qemu_put_be32(f, v);
1378 }
1379
1380 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1381 {
1382 return fread(buf, 1, size, f);
1383 }
1384
1385 int qemu_get_byte(QEMUFile *f)
1386 {
1387 int v;
1388 v = fgetc(f);
1389 if (v == EOF)
1390 return 0;
1391 else
1392 return v;
1393 }
1394
1395 unsigned int qemu_get_be16(QEMUFile *f)
1396 {
1397 unsigned int v;
1398 v = qemu_get_byte(f) << 8;
1399 v |= qemu_get_byte(f);
1400 return v;
1401 }
1402
1403 unsigned int qemu_get_be32(QEMUFile *f)
1404 {
1405 unsigned int v;
1406 v = qemu_get_byte(f) << 24;
1407 v |= qemu_get_byte(f) << 16;
1408 v |= qemu_get_byte(f) << 8;
1409 v |= qemu_get_byte(f);
1410 return v;
1411 }
1412
1413 uint64_t qemu_get_be64(QEMUFile *f)
1414 {
1415 uint64_t v;
1416 v = (uint64_t)qemu_get_be32(f) << 32;
1417 v |= qemu_get_be32(f);
1418 return v;
1419 }
1420
1421 int64_t qemu_ftell(QEMUFile *f)
1422 {
1423 return ftell(f);
1424 }
1425
1426 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1427 {
1428 if (fseek(f, pos, whence) < 0)
1429 return -1;
1430 return ftell(f);
1431 }
1432
1433 typedef struct SaveStateEntry {
1434 char idstr[256];
1435 int instance_id;
1436 int version_id;
1437 SaveStateHandler *save_state;
1438 LoadStateHandler *load_state;
1439 void *opaque;
1440 struct SaveStateEntry *next;
1441 } SaveStateEntry;
1442
1443 static SaveStateEntry *first_se;
1444
1445 int register_savevm(const char *idstr,
1446 int instance_id,
1447 int version_id,
1448 SaveStateHandler *save_state,
1449 LoadStateHandler *load_state,
1450 void *opaque)
1451 {
1452 SaveStateEntry *se, **pse;
1453
1454 se = qemu_malloc(sizeof(SaveStateEntry));
1455 if (!se)
1456 return -1;
1457 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1458 se->instance_id = instance_id;
1459 se->version_id = version_id;
1460 se->save_state = save_state;
1461 se->load_state = load_state;
1462 se->opaque = opaque;
1463 se->next = NULL;
1464
1465 /* add at the end of list */
1466 pse = &first_se;
1467 while (*pse != NULL)
1468 pse = &(*pse)->next;
1469 *pse = se;
1470 return 0;
1471 }
1472
1473 #define QEMU_VM_FILE_MAGIC 0x5145564d
1474 #define QEMU_VM_FILE_VERSION 0x00000001
1475
1476 int qemu_savevm(const char *filename)
1477 {
1478 SaveStateEntry *se;
1479 QEMUFile *f;
1480 int len, len_pos, cur_pos, saved_vm_running, ret;
1481
1482 saved_vm_running = vm_running;
1483 vm_stop(0);
1484
1485 f = fopen(filename, "wb");
1486 if (!f) {
1487 ret = -1;
1488 goto the_end;
1489 }
1490
1491 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1492 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1493
1494 for(se = first_se; se != NULL; se = se->next) {
1495 /* ID string */
1496 len = strlen(se->idstr);
1497 qemu_put_byte(f, len);
1498 qemu_put_buffer(f, se->idstr, len);
1499
1500 qemu_put_be32(f, se->instance_id);
1501 qemu_put_be32(f, se->version_id);
1502
1503 /* record size: filled later */
1504 len_pos = ftell(f);
1505 qemu_put_be32(f, 0);
1506
1507 se->save_state(f, se->opaque);
1508
1509 /* fill record size */
1510 cur_pos = ftell(f);
1511 len = ftell(f) - len_pos - 4;
1512 fseek(f, len_pos, SEEK_SET);
1513 qemu_put_be32(f, len);
1514 fseek(f, cur_pos, SEEK_SET);
1515 }
1516
1517 fclose(f);
1518 ret = 0;
1519 the_end:
1520 if (saved_vm_running)
1521 vm_start();
1522 return ret;
1523 }
1524
1525 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1526 {
1527 SaveStateEntry *se;
1528
1529 for(se = first_se; se != NULL; se = se->next) {
1530 if (!strcmp(se->idstr, idstr) &&
1531 instance_id == se->instance_id)
1532 return se;
1533 }
1534 return NULL;
1535 }
1536
1537 int qemu_loadvm(const char *filename)
1538 {
1539 SaveStateEntry *se;
1540 QEMUFile *f;
1541 int len, cur_pos, ret, instance_id, record_len, version_id;
1542 int saved_vm_running;
1543 unsigned int v;
1544 char idstr[256];
1545
1546 saved_vm_running = vm_running;
1547 vm_stop(0);
1548
1549 f = fopen(filename, "rb");
1550 if (!f) {
1551 ret = -1;
1552 goto the_end;
1553 }
1554
1555 v = qemu_get_be32(f);
1556 if (v != QEMU_VM_FILE_MAGIC)
1557 goto fail;
1558 v = qemu_get_be32(f);
1559 if (v != QEMU_VM_FILE_VERSION) {
1560 fail:
1561 fclose(f);
1562 ret = -1;
1563 goto the_end;
1564 }
1565 for(;;) {
1566 #if defined (DO_TB_FLUSH)
1567 tb_flush(global_env);
1568 #endif
1569 len = qemu_get_byte(f);
1570 if (feof(f))
1571 break;
1572 qemu_get_buffer(f, idstr, len);
1573 idstr[len] = '\0';
1574 instance_id = qemu_get_be32(f);
1575 version_id = qemu_get_be32(f);
1576 record_len = qemu_get_be32(f);
1577 #if 0
1578 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1579 idstr, instance_id, version_id, record_len);
1580 #endif
1581 cur_pos = ftell(f);
1582 se = find_se(idstr, instance_id);
1583 if (!se) {
1584 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1585 instance_id, idstr);
1586 } else {
1587 ret = se->load_state(f, se->opaque, version_id);
1588 if (ret < 0) {
1589 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1590 instance_id, idstr);
1591 }
1592 }
1593 /* always seek to exact end of record */
1594 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1595 }
1596 fclose(f);
1597 ret = 0;
1598 the_end:
1599 if (saved_vm_running)
1600 vm_start();
1601 return ret;
1602 }
1603
1604 /***********************************************************/
1605 /* cpu save/restore */
1606
1607 #if defined(TARGET_I386)
1608
1609 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1610 {
1611 qemu_put_be32(f, dt->selector);
1612 qemu_put_be32(f, (uint32_t)dt->base);
1613 qemu_put_be32(f, dt->limit);
1614 qemu_put_be32(f, dt->flags);
1615 }
1616
1617 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1618 {
1619 dt->selector = qemu_get_be32(f);
1620 dt->base = (uint8_t *)qemu_get_be32(f);
1621 dt->limit = qemu_get_be32(f);
1622 dt->flags = qemu_get_be32(f);
1623 }
1624
1625 void cpu_save(QEMUFile *f, void *opaque)
1626 {
1627 CPUState *env = opaque;
1628 uint16_t fptag, fpus, fpuc;
1629 uint32_t hflags;
1630 int i;
1631
1632 for(i = 0; i < 8; i++)
1633 qemu_put_be32s(f, &env->regs[i]);
1634 qemu_put_be32s(f, &env->eip);
1635 qemu_put_be32s(f, &env->eflags);
1636 qemu_put_be32s(f, &env->eflags);
1637 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1638 qemu_put_be32s(f, &hflags);
1639
1640 /* FPU */
1641 fpuc = env->fpuc;
1642 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1643 fptag = 0;
1644 for (i=7; i>=0; i--) {
1645 fptag <<= 2;
1646 if (env->fptags[i]) {
1647 fptag |= 3;
1648 }
1649 }
1650
1651 qemu_put_be16s(f, &fpuc);
1652 qemu_put_be16s(f, &fpus);
1653 qemu_put_be16s(f, &fptag);
1654
1655 for(i = 0; i < 8; i++) {
1656 uint64_t mant;
1657 uint16_t exp;
1658 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1659 qemu_put_be64(f, mant);
1660 qemu_put_be16(f, exp);
1661 }
1662
1663 for(i = 0; i < 6; i++)
1664 cpu_put_seg(f, &env->segs[i]);
1665 cpu_put_seg(f, &env->ldt);
1666 cpu_put_seg(f, &env->tr);
1667 cpu_put_seg(f, &env->gdt);
1668 cpu_put_seg(f, &env->idt);
1669
1670 qemu_put_be32s(f, &env->sysenter_cs);
1671 qemu_put_be32s(f, &env->sysenter_esp);
1672 qemu_put_be32s(f, &env->sysenter_eip);
1673
1674 qemu_put_be32s(f, &env->cr[0]);
1675 qemu_put_be32s(f, &env->cr[2]);
1676 qemu_put_be32s(f, &env->cr[3]);
1677 qemu_put_be32s(f, &env->cr[4]);
1678
1679 for(i = 0; i < 8; i++)
1680 qemu_put_be32s(f, &env->dr[i]);
1681
1682 /* MMU */
1683 qemu_put_be32s(f, &env->a20_mask);
1684 }
1685
1686 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1687 {
1688 CPUState *env = opaque;
1689 int i;
1690 uint32_t hflags;
1691 uint16_t fpus, fpuc, fptag;
1692
1693 if (version_id != 2)
1694 return -EINVAL;
1695 for(i = 0; i < 8; i++)
1696 qemu_get_be32s(f, &env->regs[i]);
1697 qemu_get_be32s(f, &env->eip);
1698 qemu_get_be32s(f, &env->eflags);
1699 qemu_get_be32s(f, &env->eflags);
1700 qemu_get_be32s(f, &hflags);
1701
1702 qemu_get_be16s(f, &fpuc);
1703 qemu_get_be16s(f, &fpus);
1704 qemu_get_be16s(f, &fptag);
1705
1706 for(i = 0; i < 8; i++) {
1707 uint64_t mant;
1708 uint16_t exp;
1709 mant = qemu_get_be64(f);
1710 exp = qemu_get_be16(f);
1711 env->fpregs[i] = cpu_set_fp80(mant, exp);
1712 }
1713
1714 env->fpuc = fpuc;
1715 env->fpstt = (fpus >> 11) & 7;
1716 env->fpus = fpus & ~0x3800;
1717 for(i = 0; i < 8; i++) {
1718 env->fptags[i] = ((fptag & 3) == 3);
1719 fptag >>= 2;
1720 }
1721
1722 for(i = 0; i < 6; i++)
1723 cpu_get_seg(f, &env->segs[i]);
1724 cpu_get_seg(f, &env->ldt);
1725 cpu_get_seg(f, &env->tr);
1726 cpu_get_seg(f, &env->gdt);
1727 cpu_get_seg(f, &env->idt);
1728
1729 qemu_get_be32s(f, &env->sysenter_cs);
1730 qemu_get_be32s(f, &env->sysenter_esp);
1731 qemu_get_be32s(f, &env->sysenter_eip);
1732
1733 qemu_get_be32s(f, &env->cr[0]);
1734 qemu_get_be32s(f, &env->cr[2]);
1735 qemu_get_be32s(f, &env->cr[3]);
1736 qemu_get_be32s(f, &env->cr[4]);
1737
1738 for(i = 0; i < 8; i++)
1739 qemu_get_be32s(f, &env->dr[i]);
1740
1741 /* MMU */
1742 qemu_get_be32s(f, &env->a20_mask);
1743
1744 /* XXX: compute hflags from scratch, except for CPL and IIF */
1745 env->hflags = hflags;
1746 tlb_flush(env, 1);
1747 return 0;
1748 }
1749
1750 #elif defined(TARGET_PPC)
1751 void cpu_save(QEMUFile *f, void *opaque)
1752 {
1753 }
1754
1755 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1756 {
1757 return 0;
1758 }
1759 #else
1760
1761 #warning No CPU save/restore functions
1762
1763 #endif
1764
1765 /***********************************************************/
1766 /* ram save/restore */
1767
1768 /* we just avoid storing empty pages */
1769 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1770 {
1771 int i, v;
1772
1773 v = buf[0];
1774 for(i = 1; i < len; i++) {
1775 if (buf[i] != v)
1776 goto normal_save;
1777 }
1778 qemu_put_byte(f, 1);
1779 qemu_put_byte(f, v);
1780 return;
1781 normal_save:
1782 qemu_put_byte(f, 0);
1783 qemu_put_buffer(f, buf, len);
1784 }
1785
1786 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1787 {
1788 int v;
1789
1790 v = qemu_get_byte(f);
1791 switch(v) {
1792 case 0:
1793 if (qemu_get_buffer(f, buf, len) != len)
1794 return -EIO;
1795 break;
1796 case 1:
1797 v = qemu_get_byte(f);
1798 memset(buf, v, len);
1799 break;
1800 default:
1801 return -EINVAL;
1802 }
1803 return 0;
1804 }
1805
1806 static void ram_save(QEMUFile *f, void *opaque)
1807 {
1808 int i;
1809 qemu_put_be32(f, phys_ram_size);
1810 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1811 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1812 }
1813 }
1814
1815 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1816 {
1817 int i, ret;
1818
1819 if (version_id != 1)
1820 return -EINVAL;
1821 if (qemu_get_be32(f) != phys_ram_size)
1822 return -EINVAL;
1823 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1824 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1825 if (ret)
1826 return ret;
1827 }
1828 return 0;
1829 }
1830
1831 /***********************************************************/
1832 /* main execution loop */
1833
1834 void gui_update(void *opaque)
1835 {
1836 display_state.dpy_refresh(&display_state);
1837 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1838 }
1839
1840 /* XXX: support several handlers */
1841 VMStopHandler *vm_stop_cb;
1842 VMStopHandler *vm_stop_opaque;
1843
1844 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1845 {
1846 vm_stop_cb = cb;
1847 vm_stop_opaque = opaque;
1848 return 0;
1849 }
1850
1851 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1852 {
1853 vm_stop_cb = NULL;
1854 }
1855
1856 void vm_start(void)
1857 {
1858 if (!vm_running) {
1859 cpu_enable_ticks();
1860 vm_running = 1;
1861 }
1862 }
1863
1864 void vm_stop(int reason)
1865 {
1866 if (vm_running) {
1867 cpu_disable_ticks();
1868 vm_running = 0;
1869 if (reason != 0) {
1870 if (vm_stop_cb) {
1871 vm_stop_cb(vm_stop_opaque, reason);
1872 }
1873 }
1874 }
1875 }
1876
1877 /* reset/shutdown handler */
1878
1879 typedef struct QEMUResetEntry {
1880 QEMUResetHandler *func;
1881 void *opaque;
1882 struct QEMUResetEntry *next;
1883 } QEMUResetEntry;
1884
1885 static QEMUResetEntry *first_reset_entry;
1886 static int reset_requested;
1887 static int shutdown_requested;
1888
1889 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1890 {
1891 QEMUResetEntry **pre, *re;
1892
1893 pre = &first_reset_entry;
1894 while (*pre != NULL)
1895 pre = &(*pre)->next;
1896 re = qemu_mallocz(sizeof(QEMUResetEntry));
1897 re->func = func;
1898 re->opaque = opaque;
1899 re->next = NULL;
1900 *pre = re;
1901 }
1902
1903 void qemu_system_reset(void)
1904 {
1905 QEMUResetEntry *re;
1906
1907 /* reset all devices */
1908 for(re = first_reset_entry; re != NULL; re = re->next) {
1909 re->func(re->opaque);
1910 }
1911 }
1912
1913 void qemu_system_reset_request(void)
1914 {
1915 reset_requested = 1;
1916 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1917 }
1918
1919 void qemu_system_shutdown_request(void)
1920 {
1921 shutdown_requested = 1;
1922 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1923 }
1924
1925 static void main_cpu_reset(void *opaque)
1926 {
1927 #ifdef TARGET_I386
1928 CPUState *env = opaque;
1929 cpu_reset(env);
1930 #endif
1931 }
1932
1933 int main_loop(void)
1934 {
1935 #ifndef _WIN32
1936 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1937 IOHandlerRecord *ioh, *ioh_next;
1938 uint8_t buf[4096];
1939 int n, max_size;
1940 #endif
1941 int ret, timeout;
1942 CPUState *env = global_env;
1943
1944 for(;;) {
1945 if (vm_running) {
1946 ret = cpu_exec(env);
1947 if (shutdown_requested) {
1948 ret = EXCP_INTERRUPT;
1949 break;
1950 }
1951 if (reset_requested) {
1952 reset_requested = 0;
1953 qemu_system_reset();
1954 ret = EXCP_INTERRUPT;
1955 }
1956 if (ret == EXCP_DEBUG) {
1957 vm_stop(EXCP_DEBUG);
1958 }
1959 /* if hlt instruction, we wait until the next IRQ */
1960 /* XXX: use timeout computed from timers */
1961 if (ret == EXCP_HLT)
1962 timeout = 10;
1963 else
1964 timeout = 0;
1965 } else {
1966 timeout = 10;
1967 }
1968
1969 #ifdef _WIN32
1970 if (timeout > 0)
1971 Sleep(timeout);
1972 #else
1973
1974 /* poll any events */
1975 /* XXX: separate device handlers from system ones */
1976 pf = ufds;
1977 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1978 if (!ioh->fd_can_read) {
1979 max_size = 0;
1980 pf->fd = ioh->fd;
1981 pf->events = POLLIN;
1982 ioh->ufd = pf;
1983 pf++;
1984 } else {
1985 max_size = ioh->fd_can_read(ioh->opaque);
1986 if (max_size > 0) {
1987 if (max_size > sizeof(buf))
1988 max_size = sizeof(buf);
1989 pf->fd = ioh->fd;
1990 pf->events = POLLIN;
1991 ioh->ufd = pf;
1992 pf++;
1993 } else {
1994 ioh->ufd = NULL;
1995 }
1996 }
1997 ioh->max_size = max_size;
1998 }
1999
2000 ret = poll(ufds, pf - ufds, timeout);
2001 if (ret > 0) {
2002 /* XXX: better handling of removal */
2003 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2004 ioh_next = ioh->next;
2005 pf = ioh->ufd;
2006 if (pf) {
2007 if (pf->revents & POLLIN) {
2008 if (ioh->max_size == 0) {
2009 /* just a read event */
2010 ioh->fd_read(ioh->opaque, NULL, 0);
2011 } else {
2012 n = read(ioh->fd, buf, ioh->max_size);
2013 if (n >= 0) {
2014 ioh->fd_read(ioh->opaque, buf, n);
2015 } else if (errno != EAGAIN) {
2016 ioh->fd_read(ioh->opaque, NULL, -errno);
2017 }
2018 }
2019 }
2020 }
2021 }
2022 }
2023
2024 #if defined(CONFIG_SLIRP)
2025 /* XXX: merge with poll() */
2026 if (slirp_inited) {
2027 fd_set rfds, wfds, xfds;
2028 int nfds;
2029 struct timeval tv;
2030
2031 nfds = -1;
2032 FD_ZERO(&rfds);
2033 FD_ZERO(&wfds);
2034 FD_ZERO(&xfds);
2035 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2036 tv.tv_sec = 0;
2037 tv.tv_usec = 0;
2038 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2039 if (ret >= 0) {
2040 slirp_select_poll(&rfds, &wfds, &xfds);
2041 }
2042 }
2043 #endif
2044
2045 #endif
2046
2047 if (vm_running) {
2048 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2049 qemu_get_clock(vm_clock));
2050
2051 if (audio_enabled) {
2052 /* XXX: add explicit timer */
2053 SB16_run();
2054 }
2055
2056 /* run dma transfers, if any */
2057 DMA_run();
2058 }
2059
2060 /* real time timers */
2061 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2062 qemu_get_clock(rt_clock));
2063 }
2064 cpu_disable_ticks();
2065 return ret;
2066 }
2067
2068 void help(void)
2069 {
2070 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2071 "usage: %s [options] [disk_image]\n"
2072 "\n"
2073 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2074 "\n"
2075 "Standard options:\n"
2076 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2077 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2078 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2079 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2080 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2081 "-snapshot write to temporary files instead of disk image files\n"
2082 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2083 "-nographic disable graphical output and redirect serial I/Os to console\n"
2084 "-enable-audio enable audio support\n"
2085 "-localtime set the real time clock to local time [default=utc]\n"
2086 #ifdef TARGET_PPC
2087 "-prep Simulate a PREP system (default is PowerMAC)\n"
2088 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2089 #endif
2090 "\n"
2091 "Network options:\n"
2092 "-nics n simulate 'n' network cards [default=1]\n"
2093 "-macaddr addr set the mac address of the first interface\n"
2094 "-n script set tap/tun network init script [default=%s]\n"
2095 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2096 #ifdef CONFIG_SLIRP
2097 "-user-net use user mode network stack [default if no tap/tun script]\n"
2098 #endif
2099 "-dummy-net use dummy network stack\n"
2100 "\n"
2101 "Linux boot specific:\n"
2102 "-kernel bzImage use 'bzImage' as kernel image\n"
2103 "-append cmdline use 'cmdline' as kernel command line\n"
2104 "-initrd file use 'file' as initial ram disk\n"
2105 "\n"
2106 "Debug/Expert options:\n"
2107 "-S freeze CPU at startup (use 'c' to start execution)\n"
2108 "-s wait gdb connection to port %d\n"
2109 "-p port change gdb connection port\n"
2110 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2111 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2112 "-L path set the directory for the BIOS and VGA BIOS\n"
2113 #ifdef USE_CODE_COPY
2114 "-no-code-copy disable code copy acceleration\n"
2115 #endif
2116 #ifdef TARGET_I386
2117 "-isa simulate an ISA-only system (default is PCI system)\n"
2118 #endif
2119 "\n"
2120 "During emulation, use C-a h to get terminal commands:\n",
2121 #ifdef CONFIG_SOFTMMU
2122 "qemu",
2123 #else
2124 "qemu-fast",
2125 #endif
2126 DEFAULT_RAM_SIZE,
2127 DEFAULT_NETWORK_SCRIPT,
2128 DEFAULT_GDBSTUB_PORT,
2129 "/tmp/qemu.log");
2130 term_print_help();
2131 #ifndef CONFIG_SOFTMMU
2132 printf("\n"
2133 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2134 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2135 "PC emulation.\n");
2136 #endif
2137 exit(1);
2138 }
2139
2140 #define HAS_ARG 0x0001
2141
2142 enum {
2143 QEMU_OPTION_h,
2144
2145 QEMU_OPTION_fda,
2146 QEMU_OPTION_fdb,
2147 QEMU_OPTION_hda,
2148 QEMU_OPTION_hdb,
2149 QEMU_OPTION_hdc,
2150 QEMU_OPTION_hdd,
2151 QEMU_OPTION_cdrom,
2152 QEMU_OPTION_boot,
2153 QEMU_OPTION_snapshot,
2154 QEMU_OPTION_m,
2155 QEMU_OPTION_nographic,
2156 QEMU_OPTION_enable_audio,
2157
2158 QEMU_OPTION_nics,
2159 QEMU_OPTION_macaddr,
2160 QEMU_OPTION_n,
2161 QEMU_OPTION_tun_fd,
2162 QEMU_OPTION_user_net,
2163 QEMU_OPTION_dummy_net,
2164
2165 QEMU_OPTION_kernel,
2166 QEMU_OPTION_append,
2167 QEMU_OPTION_initrd,
2168
2169 QEMU_OPTION_S,
2170 QEMU_OPTION_s,
2171 QEMU_OPTION_p,
2172 QEMU_OPTION_d,
2173 QEMU_OPTION_hdachs,
2174 QEMU_OPTION_L,
2175 QEMU_OPTION_no_code_copy,
2176 QEMU_OPTION_pci,
2177 QEMU_OPTION_isa,
2178 QEMU_OPTION_prep,
2179 QEMU_OPTION_localtime,
2180 QEMU_OPTION_cirrusvga,
2181 QEMU_OPTION_g,
2182 };
2183
2184 typedef struct QEMUOption {
2185 const char *name;
2186 int flags;
2187 int index;
2188 } QEMUOption;
2189
2190 const QEMUOption qemu_options[] = {
2191 { "h", 0, QEMU_OPTION_h },
2192
2193 { "fda", HAS_ARG, QEMU_OPTION_fda },
2194 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2195 { "hda", HAS_ARG, QEMU_OPTION_hda },
2196 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2197 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2198 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2199 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2200 { "boot", HAS_ARG, QEMU_OPTION_boot },
2201 { "snapshot", 0, QEMU_OPTION_snapshot },
2202 { "m", HAS_ARG, QEMU_OPTION_m },
2203 { "nographic", 0, QEMU_OPTION_nographic },
2204 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2205
2206 { "nics", HAS_ARG, QEMU_OPTION_nics},
2207 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2208 { "n", HAS_ARG, QEMU_OPTION_n },
2209 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2210 #ifdef CONFIG_SLIRP
2211 { "user-net", 0, QEMU_OPTION_user_net },
2212 #endif
2213 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2214
2215 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2216 { "append", HAS_ARG, QEMU_OPTION_append },
2217 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2218
2219 { "S", 0, QEMU_OPTION_S },
2220 { "s", 0, QEMU_OPTION_s },
2221 { "p", HAS_ARG, QEMU_OPTION_p },
2222 { "d", HAS_ARG, QEMU_OPTION_d },
2223 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2224 { "L", HAS_ARG, QEMU_OPTION_L },
2225 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2226 #ifdef TARGET_PPC
2227 { "prep", 0, QEMU_OPTION_prep },
2228 { "g", 1, QEMU_OPTION_g },
2229 #endif
2230 { "localtime", 0, QEMU_OPTION_localtime },
2231 { "isa", 0, QEMU_OPTION_isa },
2232
2233 /* temporary options */
2234 { "pci", 0, QEMU_OPTION_pci },
2235 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2236 { NULL },
2237 };
2238
2239 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2240
2241 /* this stack is only used during signal handling */
2242 #define SIGNAL_STACK_SIZE 32768
2243
2244 static uint8_t *signal_stack;
2245
2246 #endif
2247
2248 #define NET_IF_TUN 0
2249 #define NET_IF_USER 1
2250 #define NET_IF_DUMMY 2
2251
2252 int main(int argc, char **argv)
2253 {
2254 #ifdef CONFIG_GDBSTUB
2255 int use_gdbstub, gdbstub_port;
2256 #endif
2257 int i, has_cdrom;
2258 int snapshot, linux_boot;
2259 CPUState *env;
2260 const char *initrd_filename;
2261 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2262 const char *kernel_filename, *kernel_cmdline;
2263 DisplayState *ds = &display_state;
2264 int cyls, heads, secs;
2265 int start_emulation = 1;
2266 uint8_t macaddr[6];
2267 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2268 int optind;
2269 const char *r, *optarg;
2270
2271 #if !defined(CONFIG_SOFTMMU)
2272 /* we never want that malloc() uses mmap() */
2273 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2274 #endif
2275 initrd_filename = NULL;
2276 for(i = 0; i < MAX_FD; i++)
2277 fd_filename[i] = NULL;
2278 for(i = 0; i < MAX_DISKS; i++)
2279 hd_filename[i] = NULL;
2280 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2281 vga_ram_size = VGA_RAM_SIZE;
2282 bios_size = BIOS_SIZE;
2283 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2284 #ifdef CONFIG_GDBSTUB
2285 use_gdbstub = 0;
2286 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2287 #endif
2288 snapshot = 0;
2289 nographic = 0;
2290 kernel_filename = NULL;
2291 kernel_cmdline = "";
2292 has_cdrom = 1;
2293 cyls = heads = secs = 0;
2294
2295 nb_tun_fds = 0;
2296 net_if_type = -1;
2297 nb_nics = 1;
2298 /* default mac address of the first network interface */
2299 macaddr[0] = 0x52;
2300 macaddr[1] = 0x54;
2301 macaddr[2] = 0x00;
2302 macaddr[3] = 0x12;
2303 macaddr[4] = 0x34;
2304 macaddr[5] = 0x56;
2305
2306 optind = 1;
2307 for(;;) {
2308 if (optind >= argc)
2309 break;
2310 r = argv[optind];
2311 if (r[0] != '-') {
2312 hd_filename[0] = argv[optind++];
2313 } else {
2314 const QEMUOption *popt;
2315
2316 optind++;
2317 popt = qemu_options;
2318 for(;;) {
2319 if (!popt->name) {
2320 fprintf(stderr, "%s: invalid option -- '%s'\n",
2321 argv[0], r);
2322 exit(1);
2323 }
2324 if (!strcmp(popt->name, r + 1))
2325 break;
2326 popt++;
2327 }
2328 if (popt->flags & HAS_ARG) {
2329 if (optind >= argc) {
2330 fprintf(stderr, "%s: option '%s' requires an argument\n",
2331 argv[0], r);
2332 exit(1);
2333 }
2334 optarg = argv[optind++];
2335 } else {
2336 optarg = NULL;
2337 }
2338
2339 switch(popt->index) {
2340 case QEMU_OPTION_initrd:
2341 initrd_filename = optarg;
2342 break;
2343 case QEMU_OPTION_hda:
2344 hd_filename[0] = optarg;
2345 break;
2346 case QEMU_OPTION_hdb:
2347 hd_filename[1] = optarg;
2348 break;
2349 case QEMU_OPTION_snapshot:
2350 snapshot = 1;
2351 break;
2352 case QEMU_OPTION_hdachs:
2353 {
2354 const char *p;
2355 p = optarg;
2356 cyls = strtol(p, (char **)&p, 0);
2357 if (*p != ',')
2358 goto chs_fail;
2359 p++;
2360 heads = strtol(p, (char **)&p, 0);
2361 if (*p != ',')
2362 goto chs_fail;
2363 p++;
2364 secs = strtol(p, (char **)&p, 0);
2365 if (*p != '\0') {
2366 chs_fail:
2367 cyls = 0;
2368 }
2369 }
2370 break;
2371 case QEMU_OPTION_nographic:
2372 nographic = 1;
2373 break;
2374 case QEMU_OPTION_kernel:
2375 kernel_filename = optarg;
2376 break;
2377 case QEMU_OPTION_append:
2378 kernel_cmdline = optarg;
2379 break;
2380 case QEMU_OPTION_tun_fd:
2381 {
2382 const char *p;
2383 int fd;
2384 net_if_type = NET_IF_TUN;
2385 if (nb_tun_fds < MAX_NICS) {
2386 fd = strtol(optarg, (char **)&p, 0);
2387 if (*p != '\0') {
2388 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2389 exit(1);
2390 }
2391 tun_fds[nb_tun_fds++] = fd;
2392 }
2393 }
2394 break;
2395 case QEMU_OPTION_hdc:
2396 hd_filename[2] = optarg;
2397 has_cdrom = 0;
2398 break;
2399 case QEMU_OPTION_hdd:
2400 hd_filename[3] = optarg;
2401 break;
2402 case QEMU_OPTION_cdrom:
2403 hd_filename[2] = optarg;
2404 has_cdrom = 1;
2405 break;
2406 case QEMU_OPTION_boot:
2407 boot_device = optarg[0];
2408 if (boot_device != 'a' && boot_device != 'b' &&
2409 boot_device != 'c' && boot_device != 'd') {
2410 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2411 exit(1);
2412 }
2413 break;
2414 case QEMU_OPTION_fda:
2415 fd_filename[0] = optarg;
2416 break;
2417 case QEMU_OPTION_fdb:
2418 fd_filename[1] = optarg;
2419 break;
2420 case QEMU_OPTION_no_code_copy:
2421 code_copy_enabled = 0;
2422 break;
2423 case QEMU_OPTION_nics:
2424 nb_nics = atoi(optarg);
2425 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2426 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2427 exit(1);
2428 }
2429 break;
2430 case QEMU_OPTION_macaddr:
2431 {
2432 const char *p;
2433 int i;
2434 p = optarg;
2435 for(i = 0; i < 6; i++) {
2436 macaddr[i] = strtol(p, (char **)&p, 16);
2437 if (i == 5) {
2438 if (*p != '\0')
2439 goto macaddr_error;
2440 } else {
2441 if (*p != ':') {
2442 macaddr_error:
2443 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2444 exit(1);
2445 }
2446 p++;
2447 }
2448 }
2449 }
2450 break;
2451 case QEMU_OPTION_user_net:
2452 net_if_type = NET_IF_USER;
2453 break;
2454 case QEMU_OPTION_dummy_net:
2455 net_if_type = NET_IF_DUMMY;
2456 break;
2457 case QEMU_OPTION_enable_audio:
2458 audio_enabled = 1;
2459 break;
2460 case QEMU_OPTION_h:
2461 help();
2462 break;
2463 case QEMU_OPTION_m:
2464 ram_size = atoi(optarg) * 1024 * 1024;
2465 if (ram_size <= 0)
2466 help();
2467 if (ram_size > PHYS_RAM_MAX_SIZE) {
2468 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2469 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2470 exit(1);
2471 }
2472 break;
2473 case QEMU_OPTION_d:
2474 {
2475 int mask;
2476 CPULogItem *item;
2477
2478 mask = cpu_str_to_log_mask(optarg);
2479 if (!mask) {
2480 printf("Log items (comma separated):\n");
2481 for(item = cpu_log_items; item->mask != 0; item++) {
2482 printf("%-10s %s\n", item->name, item->help);
2483 }
2484 exit(1);
2485 }
2486 cpu_set_log(mask);
2487 }
2488 break;
2489 case QEMU_OPTION_n:
2490 pstrcpy(network_script, sizeof(network_script), optarg);
2491 break;
2492 #ifdef CONFIG_GDBSTUB
2493 case QEMU_OPTION_s:
2494 use_gdbstub = 1;
2495 break;
2496 case QEMU_OPTION_p:
2497 gdbstub_port = atoi(optarg);
2498 break;
2499 #endif
2500 case QEMU_OPTION_L:
2501 bios_dir = optarg;
2502 break;
2503 case QEMU_OPTION_S:
2504 start_emulation = 0;
2505 break;
2506 case QEMU_OPTION_pci:
2507 pci_enabled = 1;
2508 break;
2509 case QEMU_OPTION_isa:
2510 pci_enabled = 0;
2511 break;
2512 case QEMU_OPTION_prep:
2513 prep_enabled = 1;
2514 break;
2515 case QEMU_OPTION_localtime:
2516 rtc_utc = 0;
2517 break;
2518 case QEMU_OPTION_cirrusvga:
2519 cirrus_vga_enabled = 1;
2520 break;
2521 case QEMU_OPTION_g:
2522 {
2523 const char *p;
2524 int w, h, depth;
2525 p = optarg;
2526 w = strtol(p, (char **)&p, 10);
2527 if (w <= 0) {
2528 graphic_error:
2529 fprintf(stderr, "qemu: invalid resolution or depth\n");
2530 exit(1);
2531 }
2532 if (*p != 'x')
2533 goto graphic_error;
2534 p++;
2535 h = strtol(p, (char **)&p, 10);
2536 if (h <= 0)
2537 goto graphic_error;
2538 if (*p == 'x') {
2539 p++;
2540 depth = strtol(p, (char **)&p, 10);
2541 if (depth != 8 && depth != 15 && depth != 16 &&
2542 depth != 24 && depth != 32)
2543 goto graphic_error;
2544 } else if (*p == '\0') {
2545 depth = graphic_depth;
2546 } else {
2547 goto graphic_error;
2548 }
2549
2550 graphic_width = w;
2551 graphic_height = h;
2552 graphic_depth = depth;
2553 }
2554 break;
2555 }
2556 }
2557 }
2558
2559 linux_boot = (kernel_filename != NULL);
2560
2561 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2562 fd_filename[0] == '\0')
2563 help();
2564
2565 /* boot to cd by default if no hard disk */
2566 if (hd_filename[0] == '\0' && boot_device == 'c') {
2567 if (fd_filename[0] != '\0')
2568 boot_device = 'a';
2569 else
2570 boot_device = 'd';
2571 }
2572
2573 #if !defined(CONFIG_SOFTMMU)
2574 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2575 {
2576 static uint8_t stdout_buf[4096];
2577 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2578 }
2579 #else
2580 setvbuf(stdout, NULL, _IOLBF, 0);
2581 #endif
2582
2583 /* init host network redirectors */
2584 if (net_if_type == -1) {
2585 net_if_type = NET_IF_TUN;
2586 #if defined(CONFIG_SLIRP)
2587 if (access(network_script, R_OK) < 0) {
2588 net_if_type = NET_IF_USER;
2589 }
2590 #endif
2591 }
2592
2593 for(i = 0; i < nb_nics; i++) {
2594 NetDriverState *nd = &nd_table[i];
2595 nd->index = i;
2596 /* init virtual mac address */
2597 nd->macaddr[0] = macaddr[0];
2598 nd->macaddr[1] = macaddr[1];
2599 nd->macaddr[2] = macaddr[2];
2600 nd->macaddr[3] = macaddr[3];
2601 nd->macaddr[4] = macaddr[4];
2602 nd->macaddr[5] = macaddr[5] + i;
2603 switch(net_if_type) {
2604 #if defined(CONFIG_SLIRP)
2605 case NET_IF_USER:
2606 net_slirp_init(nd);
2607 break;
2608 #endif
2609 #if !defined(_WIN32)
2610 case NET_IF_TUN:
2611 if (i < nb_tun_fds) {
2612 net_fd_init(nd, tun_fds[i]);
2613 } else {
2614 if (net_tun_init(nd) < 0)
2615 net_dummy_init(nd);
2616 }
2617 break;
2618 #endif
2619 case NET_IF_DUMMY:
2620 default:
2621 net_dummy_init(nd);
2622 break;
2623 }
2624 }
2625
2626 /* init the memory */
2627 phys_ram_size = ram_size + vga_ram_size + bios_size;
2628
2629 #ifdef CONFIG_SOFTMMU
2630 #ifdef _BSD
2631 /* mallocs are always aligned on BSD. valloc is better for correctness */
2632 phys_ram_base = valloc(phys_ram_size);
2633 #else
2634 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2635 #endif
2636 if (!phys_ram_base) {
2637 fprintf(stderr, "Could not allocate physical memory\n");
2638 exit(1);
2639 }
2640 #else
2641 /* as we must map the same page at several addresses, we must use
2642 a fd */
2643 {
2644 const char *tmpdir;
2645
2646 tmpdir = getenv("QEMU_TMPDIR");
2647 if (!tmpdir)
2648 tmpdir = "/tmp";
2649 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2650 if (mkstemp(phys_ram_file) < 0) {
2651 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2652 phys_ram_file);
2653 exit(1);
2654 }
2655 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2656 if (phys_ram_fd < 0) {
2657 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2658 phys_ram_file);
2659 exit(1);
2660 }
2661 ftruncate(phys_ram_fd, phys_ram_size);
2662 unlink(phys_ram_file);
2663 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2664 phys_ram_size,
2665 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2666 phys_ram_fd, 0);
2667 if (phys_ram_base == MAP_FAILED) {
2668 fprintf(stderr, "Could not map physical memory\n");
2669 exit(1);
2670 }
2671 }
2672 #endif
2673
2674 /* we always create the cdrom drive, even if no disk is there */
2675 if (has_cdrom) {
2676 bs_table[2] = bdrv_new("cdrom");
2677 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2678 }
2679
2680 /* open the virtual block devices */
2681 for(i = 0; i < MAX_DISKS; i++) {
2682 if (hd_filename[i]) {
2683 if (!bs_table[i]) {
2684 char buf[64];
2685 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2686 bs_table[i] = bdrv_new(buf);
2687 }
2688 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2689 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2690 hd_filename[i]);
2691 exit(1);
2692 }
2693 if (i == 0 && cyls != 0)
2694 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2695 }
2696 }
2697
2698 /* we always create at least one floppy disk */
2699 fd_table[0] = bdrv_new("fda");
2700 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2701
2702 for(i = 0; i < MAX_FD; i++) {
2703 if (fd_filename[i]) {
2704 if (!fd_table[i]) {
2705 char buf[64];
2706 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2707 fd_table[i] = bdrv_new(buf);
2708 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2709 }
2710 if (fd_filename[i] != '\0') {
2711 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2712 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2713 fd_filename[i]);
2714 exit(1);
2715 }
2716 }
2717 }
2718 }
2719
2720 /* init CPU state */
2721 env = cpu_init();
2722 global_env = env;
2723 cpu_single_env = env;
2724
2725 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2726 register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
2727 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2728 qemu_register_reset(main_cpu_reset, global_env);
2729
2730 init_ioports();
2731 cpu_calibrate_ticks();
2732
2733 /* terminal init */
2734 if (nographic) {
2735 dumb_display_init(ds);
2736 } else {
2737 #ifdef CONFIG_SDL
2738 sdl_display_init(ds);
2739 #else
2740 dumb_display_init(ds);
2741 #endif
2742 }
2743
2744 /* setup cpu signal handlers for MMU / self modifying code handling */
2745 #if !defined(CONFIG_SOFTMMU)
2746
2747 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2748 {
2749 stack_t stk;
2750 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2751 stk.ss_sp = signal_stack;
2752 stk.ss_size = SIGNAL_STACK_SIZE;
2753 stk.ss_flags = 0;
2754
2755 if (sigaltstack(&stk, NULL) < 0) {
2756 perror("sigaltstack");
2757 exit(1);
2758 }
2759 }
2760 #endif
2761 {
2762 struct sigaction act;
2763
2764 sigfillset(&act.sa_mask);
2765 act.sa_flags = SA_SIGINFO;
2766 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2767 act.sa_flags |= SA_ONSTACK;
2768 #endif
2769 act.sa_sigaction = host_segv_handler;
2770 sigaction(SIGSEGV, &act, NULL);
2771 sigaction(SIGBUS, &act, NULL);
2772 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2773 sigaction(SIGFPE, &act, NULL);
2774 #endif
2775 }
2776 #endif
2777
2778 #ifndef _WIN32
2779 {
2780 struct sigaction act;
2781 sigfillset(&act.sa_mask);
2782 act.sa_flags = 0;
2783 act.sa_handler = SIG_IGN;
2784 sigaction(SIGPIPE, &act, NULL);
2785 }
2786 #endif
2787 init_timers();
2788
2789 #if defined(TARGET_I386)
2790 pc_init(ram_size, vga_ram_size, boot_device,
2791 ds, fd_filename, snapshot,
2792 kernel_filename, kernel_cmdline, initrd_filename);
2793 #elif defined(TARGET_PPC)
2794 ppc_init(ram_size, vga_ram_size, boot_device,
2795 ds, fd_filename, snapshot,
2796 kernel_filename, kernel_cmdline, initrd_filename);
2797 #endif
2798
2799 /* launched after the device init so that it can display or not a
2800 banner */
2801 monitor_init();
2802
2803 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2804 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2805
2806 #ifdef CONFIG_GDBSTUB
2807 if (use_gdbstub) {
2808 if (gdbserver_start(gdbstub_port) < 0) {
2809 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2810 gdbstub_port);
2811 exit(1);
2812 } else {
2813 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2814 }
2815 } else
2816 #endif
2817 if (start_emulation)
2818 {
2819 vm_start();
2820 }
2821 term_init();
2822 main_loop();
2823 quit_timers();
2824 return 0;
2825 }