]> git.proxmox.com Git - qemu.git/blob - vl.c
init dummy net if tun/tap network error
[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 <getopt.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <time.h>
31 #include <malloc.h>
32 #include <errno.h>
33 #include <sys/time.h>
34
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <pty.h>
39 #include <termios.h>
40 #include <sys/poll.h>
41 #include <sys/mman.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44 #include <linux/if.h>
45 #include <linux/if_tun.h>
46 #endif
47
48 #if defined(CONFIG_SLIRP)
49 #include "libslirp.h"
50 #endif
51
52 #ifdef _WIN32
53 #include <sys/timeb.h>
54 #include <windows.h>
55 #define getopt_long_only getopt_long
56 #define memalign(align, size) malloc(size)
57 #endif
58
59 #ifdef CONFIG_SDL
60 /* SDL use the pthreads and they modify sigaction. We don't
61 want that. */
62 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
63 extern void __libc_sigaction();
64 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
65 #else
66 extern void __sigaction();
67 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
68 #endif
69 #endif /* CONFIG_SDL */
70
71 #include "disas.h"
72
73 #include "exec-all.h"
74
75 //#define DO_TB_FLUSH
76
77 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
78
79 //#define DEBUG_UNUSED_IOPORT
80
81 #if !defined(CONFIG_SOFTMMU)
82 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
83 #else
84 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
85 #endif
86
87 /* in ms */
88 #define GUI_REFRESH_INTERVAL 30
89
90 /* XXX: use a two level table to limit memory usage */
91 #define MAX_IOPORTS 65536
92
93 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
94 char phys_ram_file[1024];
95 CPUState *global_env;
96 CPUState *cpu_single_env;
97 void *ioport_opaque[MAX_IOPORTS];
98 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
99 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
100 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
101 int vga_ram_size;
102 static DisplayState display_state;
103 int nographic;
104 int64_t ticks_per_sec;
105 int boot_device = 'c';
106 static int ram_size;
107 static char network_script[1024];
108 int pit_min_timer_count = 0;
109 int nb_nics;
110 NetDriverState nd_table[MAX_NICS];
111 SerialState *serial_console;
112 QEMUTimer *gui_timer;
113 int vm_running;
114
115 /***********************************************************/
116 /* x86 io ports */
117
118 uint32_t default_ioport_readb(void *opaque, uint32_t address)
119 {
120 #ifdef DEBUG_UNUSED_IOPORT
121 fprintf(stderr, "inb: port=0x%04x\n", address);
122 #endif
123 return 0xff;
124 }
125
126 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
127 {
128 #ifdef DEBUG_UNUSED_IOPORT
129 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
130 #endif
131 }
132
133 /* default is to make two byte accesses */
134 uint32_t default_ioport_readw(void *opaque, uint32_t address)
135 {
136 uint32_t data;
137 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
138 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
139 return data;
140 }
141
142 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
143 {
144 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
145 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
146 }
147
148 uint32_t default_ioport_readl(void *opaque, uint32_t address)
149 {
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr, "inl: port=0x%04x\n", address);
152 #endif
153 return 0xffffffff;
154 }
155
156 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
157 {
158 #ifdef DEBUG_UNUSED_IOPORT
159 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
160 #endif
161 }
162
163 void init_ioports(void)
164 {
165 int i;
166
167 for(i = 0; i < MAX_IOPORTS; i++) {
168 ioport_read_table[0][i] = default_ioport_readb;
169 ioport_write_table[0][i] = default_ioport_writeb;
170 ioport_read_table[1][i] = default_ioport_readw;
171 ioport_write_table[1][i] = default_ioport_writew;
172 ioport_read_table[2][i] = default_ioport_readl;
173 ioport_write_table[2][i] = default_ioport_writel;
174 }
175 }
176
177 /* size is the word size in byte */
178 int register_ioport_read(int start, int length, int size,
179 IOPortReadFunc *func, void *opaque)
180 {
181 int i, bsize;
182
183 if (size == 1) {
184 bsize = 0;
185 } else if (size == 2) {
186 bsize = 1;
187 } else if (size == 4) {
188 bsize = 2;
189 } else {
190 hw_error("register_ioport_read: invalid size");
191 return -1;
192 }
193 for(i = start; i < start + length; i += size) {
194 ioport_read_table[bsize][i] = func;
195 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
196 hw_error("register_ioport_read: invalid opaque");
197 ioport_opaque[i] = opaque;
198 }
199 return 0;
200 }
201
202 /* size is the word size in byte */
203 int register_ioport_write(int start, int length, int size,
204 IOPortWriteFunc *func, void *opaque)
205 {
206 int i, bsize;
207
208 if (size == 1) {
209 bsize = 0;
210 } else if (size == 2) {
211 bsize = 1;
212 } else if (size == 4) {
213 bsize = 2;
214 } else {
215 hw_error("register_ioport_write: invalid size");
216 return -1;
217 }
218 for(i = start; i < start + length; i += size) {
219 ioport_write_table[bsize][i] = func;
220 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
221 hw_error("register_ioport_read: invalid opaque");
222 ioport_opaque[i] = opaque;
223 }
224 return 0;
225 }
226
227 void pstrcpy(char *buf, int buf_size, const char *str)
228 {
229 int c;
230 char *q = buf;
231
232 if (buf_size <= 0)
233 return;
234
235 for(;;) {
236 c = *str++;
237 if (c == 0 || q >= buf + buf_size - 1)
238 break;
239 *q++ = c;
240 }
241 *q = '\0';
242 }
243
244 /* strcat and truncate. */
245 char *pstrcat(char *buf, int buf_size, const char *s)
246 {
247 int len;
248 len = strlen(buf);
249 if (len < buf_size)
250 pstrcpy(buf + len, buf_size - len, s);
251 return buf;
252 }
253
254 /* return the size or -1 if error */
255 int load_image(const char *filename, uint8_t *addr)
256 {
257 int fd, size;
258 fd = open(filename, O_RDONLY | O_BINARY);
259 if (fd < 0)
260 return -1;
261 size = lseek(fd, 0, SEEK_END);
262 lseek(fd, 0, SEEK_SET);
263 if (read(fd, addr, size) != size) {
264 close(fd);
265 return -1;
266 }
267 close(fd);
268 return size;
269 }
270
271 void cpu_outb(CPUState *env, int addr, int val)
272 {
273 addr &= (MAX_IOPORTS - 1);
274 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
275 }
276
277 void cpu_outw(CPUState *env, int addr, int val)
278 {
279 addr &= (MAX_IOPORTS - 1);
280 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
281 }
282
283 void cpu_outl(CPUState *env, int addr, int val)
284 {
285 addr &= (MAX_IOPORTS - 1);
286 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
287 }
288
289 int cpu_inb(CPUState *env, int addr)
290 {
291 addr &= (MAX_IOPORTS - 1);
292 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
293 }
294
295 int cpu_inw(CPUState *env, int addr)
296 {
297 addr &= (MAX_IOPORTS - 1);
298 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
299 }
300
301 int cpu_inl(CPUState *env, int addr)
302 {
303 addr &= (MAX_IOPORTS - 1);
304 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
305 }
306
307 /***********************************************************/
308 void hw_error(const char *fmt, ...)
309 {
310 va_list ap;
311
312 va_start(ap, fmt);
313 fprintf(stderr, "qemu: hardware error: ");
314 vfprintf(stderr, fmt, ap);
315 fprintf(stderr, "\n");
316 #ifdef TARGET_I386
317 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
318 #else
319 cpu_dump_state(global_env, stderr, 0);
320 #endif
321 va_end(ap);
322 abort();
323 }
324
325 /***********************************************************/
326 /* timers */
327
328 #if defined(__powerpc__)
329
330 static inline uint32_t get_tbl(void)
331 {
332 uint32_t tbl;
333 asm volatile("mftb %0" : "=r" (tbl));
334 return tbl;
335 }
336
337 static inline uint32_t get_tbu(void)
338 {
339 uint32_t tbl;
340 asm volatile("mftbu %0" : "=r" (tbl));
341 return tbl;
342 }
343
344 int64_t cpu_get_real_ticks(void)
345 {
346 uint32_t l, h, h1;
347 /* NOTE: we test if wrapping has occurred */
348 do {
349 h = get_tbu();
350 l = get_tbl();
351 h1 = get_tbu();
352 } while (h != h1);
353 return ((int64_t)h << 32) | l;
354 }
355
356 #elif defined(__i386__)
357
358 int64_t cpu_get_real_ticks(void)
359 {
360 int64_t val;
361 asm volatile ("rdtsc" : "=A" (val));
362 return val;
363 }
364
365 #elif defined(__x86_64__)
366
367 int64_t cpu_get_real_ticks(void)
368 {
369 uint32_t low,high;
370 int64_t val;
371 asm volatile("rdtsc" : "=a" (low), "=d" (high));
372 val = high;
373 val <<= 32;
374 val |= low;
375 return val;
376 }
377
378 #else
379 #error unsupported CPU
380 #endif
381
382 static int64_t cpu_ticks_offset;
383 static int cpu_ticks_enabled;
384
385 static inline int64_t cpu_get_ticks(void)
386 {
387 if (!cpu_ticks_enabled) {
388 return cpu_ticks_offset;
389 } else {
390 return cpu_get_real_ticks() + cpu_ticks_offset;
391 }
392 }
393
394 /* enable cpu_get_ticks() */
395 void cpu_enable_ticks(void)
396 {
397 if (!cpu_ticks_enabled) {
398 cpu_ticks_offset -= cpu_get_real_ticks();
399 cpu_ticks_enabled = 1;
400 }
401 }
402
403 /* disable cpu_get_ticks() : the clock is stopped. You must not call
404 cpu_get_ticks() after that. */
405 void cpu_disable_ticks(void)
406 {
407 if (cpu_ticks_enabled) {
408 cpu_ticks_offset = cpu_get_ticks();
409 cpu_ticks_enabled = 0;
410 }
411 }
412
413 static int64_t get_clock(void)
414 {
415 #ifdef _WIN32
416 struct _timeb tb;
417 _ftime(&tb);
418 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
419 #else
420 struct timeval tv;
421 gettimeofday(&tv, NULL);
422 return tv.tv_sec * 1000000LL + tv.tv_usec;
423 #endif
424 }
425
426 void cpu_calibrate_ticks(void)
427 {
428 int64_t usec, ticks;
429
430 usec = get_clock();
431 ticks = cpu_get_real_ticks();
432 #ifdef _WIN32
433 Sleep(50);
434 #else
435 usleep(50 * 1000);
436 #endif
437 usec = get_clock() - usec;
438 ticks = cpu_get_real_ticks() - ticks;
439 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
440 }
441
442 /* compute with 96 bit intermediate result: (a*b)/c */
443 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
444 {
445 union {
446 uint64_t ll;
447 struct {
448 #ifdef WORDS_BIGENDIAN
449 uint32_t high, low;
450 #else
451 uint32_t low, high;
452 #endif
453 } l;
454 } u, res;
455 uint64_t rl, rh;
456
457 u.ll = a;
458 rl = (uint64_t)u.l.low * (uint64_t)b;
459 rh = (uint64_t)u.l.high * (uint64_t)b;
460 rh += (rl >> 32);
461 res.l.high = rh / c;
462 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
463 return res.ll;
464 }
465
466 #define QEMU_TIMER_REALTIME 0
467 #define QEMU_TIMER_VIRTUAL 1
468
469 struct QEMUClock {
470 int type;
471 /* XXX: add frequency */
472 };
473
474 struct QEMUTimer {
475 QEMUClock *clock;
476 int64_t expire_time;
477 QEMUTimerCB *cb;
478 void *opaque;
479 struct QEMUTimer *next;
480 };
481
482 QEMUClock *rt_clock;
483 QEMUClock *vm_clock;
484
485 static QEMUTimer *active_timers[2];
486 #ifdef _WIN32
487 static MMRESULT timerID;
488 #else
489 /* frequency of the times() clock tick */
490 static int timer_freq;
491 #endif
492
493 QEMUClock *qemu_new_clock(int type)
494 {
495 QEMUClock *clock;
496 clock = qemu_mallocz(sizeof(QEMUClock));
497 if (!clock)
498 return NULL;
499 clock->type = type;
500 return clock;
501 }
502
503 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
504 {
505 QEMUTimer *ts;
506
507 ts = qemu_mallocz(sizeof(QEMUTimer));
508 ts->clock = clock;
509 ts->cb = cb;
510 ts->opaque = opaque;
511 return ts;
512 }
513
514 void qemu_free_timer(QEMUTimer *ts)
515 {
516 qemu_free(ts);
517 }
518
519 /* stop a timer, but do not dealloc it */
520 void qemu_del_timer(QEMUTimer *ts)
521 {
522 QEMUTimer **pt, *t;
523
524 /* NOTE: this code must be signal safe because
525 qemu_timer_expired() can be called from a signal. */
526 pt = &active_timers[ts->clock->type];
527 for(;;) {
528 t = *pt;
529 if (!t)
530 break;
531 if (t == ts) {
532 *pt = t->next;
533 break;
534 }
535 pt = &t->next;
536 }
537 }
538
539 /* modify the current timer so that it will be fired when current_time
540 >= expire_time. The corresponding callback will be called. */
541 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
542 {
543 QEMUTimer **pt, *t;
544
545 qemu_del_timer(ts);
546
547 /* add the timer in the sorted list */
548 /* NOTE: this code must be signal safe because
549 qemu_timer_expired() can be called from a signal. */
550 pt = &active_timers[ts->clock->type];
551 for(;;) {
552 t = *pt;
553 if (!t)
554 break;
555 if (t->expire_time > expire_time)
556 break;
557 pt = &t->next;
558 }
559 ts->expire_time = expire_time;
560 ts->next = *pt;
561 *pt = ts;
562 }
563
564 int qemu_timer_pending(QEMUTimer *ts)
565 {
566 QEMUTimer *t;
567 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
568 if (t == ts)
569 return 1;
570 }
571 return 0;
572 }
573
574 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
575 {
576 if (!timer_head)
577 return 0;
578 return (timer_head->expire_time <= current_time);
579 }
580
581 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
582 {
583 QEMUTimer *ts;
584
585 for(;;) {
586 ts = *ptimer_head;
587 if (ts->expire_time > current_time)
588 break;
589 /* remove timer from the list before calling the callback */
590 *ptimer_head = ts->next;
591 ts->next = NULL;
592
593 /* run the callback (the timer list can be modified) */
594 ts->cb(ts->opaque);
595 }
596 }
597
598 int64_t qemu_get_clock(QEMUClock *clock)
599 {
600 switch(clock->type) {
601 case QEMU_TIMER_REALTIME:
602 #ifdef _WIN32
603 return GetTickCount();
604 #else
605 /* XXX: portability among Linux hosts */
606 if (timer_freq == 100) {
607 return times(NULL) * 10;
608 } else {
609 return ((int64_t)times(NULL) * 1000) / timer_freq;
610 }
611 #endif
612 default:
613 case QEMU_TIMER_VIRTUAL:
614 return cpu_get_ticks();
615 }
616 }
617
618 /* save a timer */
619 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
620 {
621 uint64_t expire_time;
622
623 if (qemu_timer_pending(ts)) {
624 expire_time = ts->expire_time;
625 } else {
626 expire_time = -1;
627 }
628 qemu_put_be64(f, expire_time);
629 }
630
631 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
632 {
633 uint64_t expire_time;
634
635 expire_time = qemu_get_be64(f);
636 if (expire_time != -1) {
637 qemu_mod_timer(ts, expire_time);
638 } else {
639 qemu_del_timer(ts);
640 }
641 }
642
643 static void timer_save(QEMUFile *f, void *opaque)
644 {
645 if (cpu_ticks_enabled) {
646 hw_error("cannot save state if virtual timers are running");
647 }
648 qemu_put_be64s(f, &cpu_ticks_offset);
649 qemu_put_be64s(f, &ticks_per_sec);
650 }
651
652 static int timer_load(QEMUFile *f, void *opaque, int version_id)
653 {
654 if (version_id != 1)
655 return -EINVAL;
656 if (cpu_ticks_enabled) {
657 return -EINVAL;
658 }
659 qemu_get_be64s(f, &cpu_ticks_offset);
660 qemu_get_be64s(f, &ticks_per_sec);
661 return 0;
662 }
663
664 #ifdef _WIN32
665 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
666 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
667 #else
668 static void host_alarm_handler(int host_signum)
669 #endif
670 {
671 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
672 qemu_get_clock(vm_clock)) ||
673 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
674 qemu_get_clock(rt_clock))) {
675 /* stop the cpu because a timer occured */
676 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
677 }
678 }
679
680 static void init_timers(void)
681 {
682 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
683 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
684
685 #ifdef _WIN32
686 {
687 int count=0;
688 timerID = timeSetEvent(10, // interval (ms)
689 0, // resolution
690 host_alarm_handler, // function
691 (DWORD)&count, // user parameter
692 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
693 if( !timerID ) {
694 perror("failed timer alarm");
695 exit(1);
696 }
697 }
698 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
699 #else
700 {
701 struct sigaction act;
702 struct itimerval itv;
703
704 /* get times() syscall frequency */
705 timer_freq = sysconf(_SC_CLK_TCK);
706
707 /* timer signal */
708 sigfillset(&act.sa_mask);
709 act.sa_flags = 0;
710 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
711 act.sa_flags |= SA_ONSTACK;
712 #endif
713 act.sa_handler = host_alarm_handler;
714 sigaction(SIGALRM, &act, NULL);
715
716 itv.it_interval.tv_sec = 0;
717 itv.it_interval.tv_usec = 1000;
718 itv.it_value.tv_sec = 0;
719 itv.it_value.tv_usec = 10 * 1000;
720 setitimer(ITIMER_REAL, &itv, NULL);
721 /* we probe the tick duration of the kernel to inform the user if
722 the emulated kernel requested a too high timer frequency */
723 getitimer(ITIMER_REAL, &itv);
724 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
725 1000000;
726 }
727 #endif
728 }
729
730 void quit_timers(void)
731 {
732 #ifdef _WIN32
733 timeKillEvent(timerID);
734 #endif
735 }
736
737 /***********************************************************/
738 /* serial device */
739
740 #ifdef _WIN32
741
742 int serial_open_device(void)
743 {
744 return -1;
745 }
746
747 #else
748
749 int serial_open_device(void)
750 {
751 char slave_name[1024];
752 int master_fd, slave_fd;
753
754 if (serial_console == NULL && nographic) {
755 /* use console for serial port */
756 return 0;
757 } else {
758 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
759 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
760 return -1;
761 }
762 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
763 return master_fd;
764 }
765 }
766
767 #endif
768
769 /***********************************************************/
770 /* Linux network device redirectors */
771
772 void hex_dump(FILE *f, const uint8_t *buf, int size)
773 {
774 int len, i, j, c;
775
776 for(i=0;i<size;i+=16) {
777 len = size - i;
778 if (len > 16)
779 len = 16;
780 fprintf(f, "%08x ", i);
781 for(j=0;j<16;j++) {
782 if (j < len)
783 fprintf(f, " %02x", buf[i+j]);
784 else
785 fprintf(f, " ");
786 }
787 fprintf(f, " ");
788 for(j=0;j<len;j++) {
789 c = buf[i+j];
790 if (c < ' ' || c > '~')
791 c = '.';
792 fprintf(f, "%c", c);
793 }
794 fprintf(f, "\n");
795 }
796 }
797
798 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
799 {
800 nd->send_packet(nd, buf, size);
801 }
802
803 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
804 IOReadHandler *fd_read, void *opaque)
805 {
806 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
807 }
808
809 /* dummy network adapter */
810
811 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
812 {
813 }
814
815 static void dummy_add_read_packet(NetDriverState *nd,
816 IOCanRWHandler *fd_can_read,
817 IOReadHandler *fd_read, void *opaque)
818 {
819 }
820
821 static int net_dummy_init(NetDriverState *nd)
822 {
823 nd->send_packet = dummy_send_packet;
824 nd->add_read_packet = dummy_add_read_packet;
825 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
826 return 0;
827 }
828
829 #if defined(CONFIG_SLIRP)
830
831 /* slirp network adapter */
832
833 static void *slirp_fd_opaque;
834 static IOCanRWHandler *slirp_fd_can_read;
835 static IOReadHandler *slirp_fd_read;
836 static int slirp_inited;
837
838 int slirp_can_output(void)
839 {
840 return slirp_fd_can_read(slirp_fd_opaque);
841 }
842
843 void slirp_output(const uint8_t *pkt, int pkt_len)
844 {
845 #if 0
846 printf("output:\n");
847 hex_dump(stdout, pkt, pkt_len);
848 #endif
849 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
850 }
851
852 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
853 {
854 #if 0
855 printf("input:\n");
856 hex_dump(stdout, buf, size);
857 #endif
858 slirp_input(buf, size);
859 }
860
861 static void slirp_add_read_packet(NetDriverState *nd,
862 IOCanRWHandler *fd_can_read,
863 IOReadHandler *fd_read, void *opaque)
864 {
865 slirp_fd_opaque = opaque;
866 slirp_fd_can_read = fd_can_read;
867 slirp_fd_read = fd_read;
868 }
869
870 static int net_slirp_init(NetDriverState *nd)
871 {
872 if (!slirp_inited) {
873 slirp_inited = 1;
874 slirp_init();
875 }
876 nd->send_packet = slirp_send_packet;
877 nd->add_read_packet = slirp_add_read_packet;
878 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
879 return 0;
880 }
881
882 #endif /* CONFIG_SLIRP */
883
884 #if !defined(_WIN32)
885
886 static int tun_open(char *ifname, int ifname_size)
887 {
888 struct ifreq ifr;
889 int fd, ret;
890
891 fd = open("/dev/net/tun", O_RDWR);
892 if (fd < 0) {
893 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
894 return -1;
895 }
896 memset(&ifr, 0, sizeof(ifr));
897 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
898 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
899 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
900 if (ret != 0) {
901 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
902 close(fd);
903 return -1;
904 }
905 printf("Connected to host network interface: %s\n", ifr.ifr_name);
906 pstrcpy(ifname, ifname_size, ifr.ifr_name);
907 fcntl(fd, F_SETFL, O_NONBLOCK);
908 return fd;
909 }
910
911 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
912 {
913 write(nd->fd, buf, size);
914 }
915
916 static void tun_add_read_packet(NetDriverState *nd,
917 IOCanRWHandler *fd_can_read,
918 IOReadHandler *fd_read, void *opaque)
919 {
920 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
921 }
922
923 static int net_tun_init(NetDriverState *nd)
924 {
925 int pid, status;
926 char *args[3];
927 char **parg;
928
929 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
930 if (nd->fd < 0)
931 return -1;
932
933 /* try to launch network init script */
934 pid = fork();
935 if (pid >= 0) {
936 if (pid == 0) {
937 parg = args;
938 *parg++ = network_script;
939 *parg++ = nd->ifname;
940 *parg++ = NULL;
941 execv(network_script, args);
942 exit(1);
943 }
944 while (waitpid(pid, &status, 0) != pid);
945 if (!WIFEXITED(status) ||
946 WEXITSTATUS(status) != 0) {
947 fprintf(stderr, "%s: could not launch network script\n",
948 network_script);
949 }
950 }
951 nd->send_packet = tun_send_packet;
952 nd->add_read_packet = tun_add_read_packet;
953 return 0;
954 }
955
956 static int net_fd_init(NetDriverState *nd, int fd)
957 {
958 nd->fd = fd;
959 nd->send_packet = tun_send_packet;
960 nd->add_read_packet = tun_add_read_packet;
961 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
962 return 0;
963 }
964
965 #endif /* !_WIN32 */
966
967 /***********************************************************/
968 /* dumb display */
969
970 #ifdef _WIN32
971
972 static void term_exit(void)
973 {
974 }
975
976 static void term_init(void)
977 {
978 }
979
980 #else
981
982 /* init terminal so that we can grab keys */
983 static struct termios oldtty;
984
985 static void term_exit(void)
986 {
987 tcsetattr (0, TCSANOW, &oldtty);
988 }
989
990 static void term_init(void)
991 {
992 struct termios tty;
993
994 tcgetattr (0, &tty);
995 oldtty = tty;
996
997 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
998 |INLCR|IGNCR|ICRNL|IXON);
999 tty.c_oflag |= OPOST;
1000 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1001 /* if graphical mode, we allow Ctrl-C handling */
1002 if (nographic)
1003 tty.c_lflag &= ~ISIG;
1004 tty.c_cflag &= ~(CSIZE|PARENB);
1005 tty.c_cflag |= CS8;
1006 tty.c_cc[VMIN] = 1;
1007 tty.c_cc[VTIME] = 0;
1008
1009 tcsetattr (0, TCSANOW, &tty);
1010
1011 atexit(term_exit);
1012
1013 fcntl(0, F_SETFL, O_NONBLOCK);
1014 }
1015
1016 #endif
1017
1018 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1019 {
1020 }
1021
1022 static void dumb_resize(DisplayState *ds, int w, int h)
1023 {
1024 }
1025
1026 static void dumb_refresh(DisplayState *ds)
1027 {
1028 vga_update_display();
1029 }
1030
1031 void dumb_display_init(DisplayState *ds)
1032 {
1033 ds->data = NULL;
1034 ds->linesize = 0;
1035 ds->depth = 0;
1036 ds->dpy_update = dumb_update;
1037 ds->dpy_resize = dumb_resize;
1038 ds->dpy_refresh = dumb_refresh;
1039 }
1040
1041 #if !defined(CONFIG_SOFTMMU)
1042 /***********************************************************/
1043 /* cpu signal handler */
1044 static void host_segv_handler(int host_signum, siginfo_t *info,
1045 void *puc)
1046 {
1047 if (cpu_signal_handler(host_signum, info, puc))
1048 return;
1049 term_exit();
1050 abort();
1051 }
1052 #endif
1053
1054 /***********************************************************/
1055 /* I/O handling */
1056
1057 #define MAX_IO_HANDLERS 64
1058
1059 typedef struct IOHandlerRecord {
1060 int fd;
1061 IOCanRWHandler *fd_can_read;
1062 IOReadHandler *fd_read;
1063 void *opaque;
1064 /* temporary data */
1065 struct pollfd *ufd;
1066 int max_size;
1067 struct IOHandlerRecord *next;
1068 } IOHandlerRecord;
1069
1070 static IOHandlerRecord *first_io_handler;
1071
1072 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1073 IOReadHandler *fd_read, void *opaque)
1074 {
1075 IOHandlerRecord *ioh;
1076
1077 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1078 if (!ioh)
1079 return -1;
1080 ioh->fd = fd;
1081 ioh->fd_can_read = fd_can_read;
1082 ioh->fd_read = fd_read;
1083 ioh->opaque = opaque;
1084 ioh->next = first_io_handler;
1085 first_io_handler = ioh;
1086 return 0;
1087 }
1088
1089 void qemu_del_fd_read_handler(int fd)
1090 {
1091 IOHandlerRecord **pioh, *ioh;
1092
1093 pioh = &first_io_handler;
1094 for(;;) {
1095 ioh = *pioh;
1096 if (ioh == NULL)
1097 break;
1098 if (ioh->fd == fd) {
1099 *pioh = ioh->next;
1100 break;
1101 }
1102 pioh = &ioh->next;
1103 }
1104 }
1105
1106 /***********************************************************/
1107 /* savevm/loadvm support */
1108
1109 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1110 {
1111 fwrite(buf, 1, size, f);
1112 }
1113
1114 void qemu_put_byte(QEMUFile *f, int v)
1115 {
1116 fputc(v, f);
1117 }
1118
1119 void qemu_put_be16(QEMUFile *f, unsigned int v)
1120 {
1121 qemu_put_byte(f, v >> 8);
1122 qemu_put_byte(f, v);
1123 }
1124
1125 void qemu_put_be32(QEMUFile *f, unsigned int v)
1126 {
1127 qemu_put_byte(f, v >> 24);
1128 qemu_put_byte(f, v >> 16);
1129 qemu_put_byte(f, v >> 8);
1130 qemu_put_byte(f, v);
1131 }
1132
1133 void qemu_put_be64(QEMUFile *f, uint64_t v)
1134 {
1135 qemu_put_be32(f, v >> 32);
1136 qemu_put_be32(f, v);
1137 }
1138
1139 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1140 {
1141 return fread(buf, 1, size, f);
1142 }
1143
1144 int qemu_get_byte(QEMUFile *f)
1145 {
1146 int v;
1147 v = fgetc(f);
1148 if (v == EOF)
1149 return 0;
1150 else
1151 return v;
1152 }
1153
1154 unsigned int qemu_get_be16(QEMUFile *f)
1155 {
1156 unsigned int v;
1157 v = qemu_get_byte(f) << 8;
1158 v |= qemu_get_byte(f);
1159 return v;
1160 }
1161
1162 unsigned int qemu_get_be32(QEMUFile *f)
1163 {
1164 unsigned int v;
1165 v = qemu_get_byte(f) << 24;
1166 v |= qemu_get_byte(f) << 16;
1167 v |= qemu_get_byte(f) << 8;
1168 v |= qemu_get_byte(f);
1169 return v;
1170 }
1171
1172 uint64_t qemu_get_be64(QEMUFile *f)
1173 {
1174 uint64_t v;
1175 v = (uint64_t)qemu_get_be32(f) << 32;
1176 v |= qemu_get_be32(f);
1177 return v;
1178 }
1179
1180 int64_t qemu_ftell(QEMUFile *f)
1181 {
1182 return ftell(f);
1183 }
1184
1185 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1186 {
1187 if (fseek(f, pos, whence) < 0)
1188 return -1;
1189 return ftell(f);
1190 }
1191
1192 typedef struct SaveStateEntry {
1193 char idstr[256];
1194 int instance_id;
1195 int version_id;
1196 SaveStateHandler *save_state;
1197 LoadStateHandler *load_state;
1198 void *opaque;
1199 struct SaveStateEntry *next;
1200 } SaveStateEntry;
1201
1202 static SaveStateEntry *first_se;
1203
1204 int register_savevm(const char *idstr,
1205 int instance_id,
1206 int version_id,
1207 SaveStateHandler *save_state,
1208 LoadStateHandler *load_state,
1209 void *opaque)
1210 {
1211 SaveStateEntry *se, **pse;
1212
1213 se = qemu_malloc(sizeof(SaveStateEntry));
1214 if (!se)
1215 return -1;
1216 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1217 se->instance_id = instance_id;
1218 se->version_id = version_id;
1219 se->save_state = save_state;
1220 se->load_state = load_state;
1221 se->opaque = opaque;
1222 se->next = NULL;
1223
1224 /* add at the end of list */
1225 pse = &first_se;
1226 while (*pse != NULL)
1227 pse = &(*pse)->next;
1228 *pse = se;
1229 return 0;
1230 }
1231
1232 #define QEMU_VM_FILE_MAGIC 0x5145564d
1233 #define QEMU_VM_FILE_VERSION 0x00000001
1234
1235 int qemu_savevm(const char *filename)
1236 {
1237 SaveStateEntry *se;
1238 QEMUFile *f;
1239 int len, len_pos, cur_pos, saved_vm_running, ret;
1240
1241 saved_vm_running = vm_running;
1242 vm_stop(0);
1243
1244 f = fopen(filename, "wb");
1245 if (!f) {
1246 ret = -1;
1247 goto the_end;
1248 }
1249
1250 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1251 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1252
1253 for(se = first_se; se != NULL; se = se->next) {
1254 /* ID string */
1255 len = strlen(se->idstr);
1256 qemu_put_byte(f, len);
1257 qemu_put_buffer(f, se->idstr, len);
1258
1259 qemu_put_be32(f, se->instance_id);
1260 qemu_put_be32(f, se->version_id);
1261
1262 /* record size: filled later */
1263 len_pos = ftell(f);
1264 qemu_put_be32(f, 0);
1265
1266 se->save_state(f, se->opaque);
1267
1268 /* fill record size */
1269 cur_pos = ftell(f);
1270 len = ftell(f) - len_pos - 4;
1271 fseek(f, len_pos, SEEK_SET);
1272 qemu_put_be32(f, len);
1273 fseek(f, cur_pos, SEEK_SET);
1274 }
1275
1276 fclose(f);
1277 ret = 0;
1278 the_end:
1279 if (saved_vm_running)
1280 vm_start();
1281 return ret;
1282 }
1283
1284 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1285 {
1286 SaveStateEntry *se;
1287
1288 for(se = first_se; se != NULL; se = se->next) {
1289 if (!strcmp(se->idstr, idstr) &&
1290 instance_id == se->instance_id)
1291 return se;
1292 }
1293 return NULL;
1294 }
1295
1296 int qemu_loadvm(const char *filename)
1297 {
1298 SaveStateEntry *se;
1299 QEMUFile *f;
1300 int len, cur_pos, ret, instance_id, record_len, version_id;
1301 int saved_vm_running;
1302 unsigned int v;
1303 char idstr[256];
1304
1305 saved_vm_running = vm_running;
1306 vm_stop(0);
1307
1308 f = fopen(filename, "rb");
1309 if (!f) {
1310 ret = -1;
1311 goto the_end;
1312 }
1313
1314 v = qemu_get_be32(f);
1315 if (v != QEMU_VM_FILE_MAGIC)
1316 goto fail;
1317 v = qemu_get_be32(f);
1318 if (v != QEMU_VM_FILE_VERSION) {
1319 fail:
1320 fclose(f);
1321 ret = -1;
1322 goto the_end;
1323 }
1324 for(;;) {
1325 #if defined (DO_TB_FLUSH)
1326 tb_flush(global_env);
1327 #endif
1328 len = qemu_get_byte(f);
1329 if (feof(f))
1330 break;
1331 qemu_get_buffer(f, idstr, len);
1332 idstr[len] = '\0';
1333 instance_id = qemu_get_be32(f);
1334 version_id = qemu_get_be32(f);
1335 record_len = qemu_get_be32(f);
1336 #if 0
1337 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1338 idstr, instance_id, version_id, record_len);
1339 #endif
1340 cur_pos = ftell(f);
1341 se = find_se(idstr, instance_id);
1342 if (!se) {
1343 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1344 instance_id, idstr);
1345 } else {
1346 ret = se->load_state(f, se->opaque, version_id);
1347 if (ret < 0) {
1348 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1349 instance_id, idstr);
1350 }
1351 }
1352 /* always seek to exact end of record */
1353 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1354 }
1355 fclose(f);
1356 ret = 0;
1357 the_end:
1358 if (saved_vm_running)
1359 vm_start();
1360 return ret;
1361 }
1362
1363 /***********************************************************/
1364 /* cpu save/restore */
1365
1366 #if defined(TARGET_I386)
1367
1368 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1369 {
1370 qemu_put_be32(f, (uint32_t)dt->base);
1371 qemu_put_be32(f, dt->limit);
1372 qemu_put_be32(f, dt->flags);
1373 }
1374
1375 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1376 {
1377 dt->base = (uint8_t *)qemu_get_be32(f);
1378 dt->limit = qemu_get_be32(f);
1379 dt->flags = qemu_get_be32(f);
1380 }
1381
1382 void cpu_save(QEMUFile *f, void *opaque)
1383 {
1384 CPUState *env = opaque;
1385 uint16_t fptag, fpus, fpuc;
1386 uint32_t hflags;
1387 int i;
1388
1389 for(i = 0; i < 8; i++)
1390 qemu_put_be32s(f, &env->regs[i]);
1391 qemu_put_be32s(f, &env->eip);
1392 qemu_put_be32s(f, &env->eflags);
1393 qemu_put_be32s(f, &env->eflags);
1394 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1395 qemu_put_be32s(f, &hflags);
1396
1397 /* FPU */
1398 fpuc = env->fpuc;
1399 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1400 fptag = 0;
1401 for (i=7; i>=0; i--) {
1402 fptag <<= 2;
1403 if (env->fptags[i]) {
1404 fptag |= 3;
1405 }
1406 }
1407
1408 qemu_put_be16s(f, &fpuc);
1409 qemu_put_be16s(f, &fpus);
1410 qemu_put_be16s(f, &fptag);
1411
1412 for(i = 0; i < 8; i++) {
1413 uint64_t mant;
1414 uint16_t exp;
1415 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1416 qemu_put_be64(f, mant);
1417 qemu_put_be16(f, exp);
1418 }
1419
1420 for(i = 0; i < 6; i++)
1421 cpu_put_seg(f, &env->segs[i]);
1422 cpu_put_seg(f, &env->ldt);
1423 cpu_put_seg(f, &env->tr);
1424 cpu_put_seg(f, &env->gdt);
1425 cpu_put_seg(f, &env->idt);
1426
1427 qemu_put_be32s(f, &env->sysenter_cs);
1428 qemu_put_be32s(f, &env->sysenter_esp);
1429 qemu_put_be32s(f, &env->sysenter_eip);
1430
1431 qemu_put_be32s(f, &env->cr[0]);
1432 qemu_put_be32s(f, &env->cr[2]);
1433 qemu_put_be32s(f, &env->cr[3]);
1434 qemu_put_be32s(f, &env->cr[4]);
1435
1436 for(i = 0; i < 8; i++)
1437 qemu_put_be32s(f, &env->dr[i]);
1438
1439 /* MMU */
1440 qemu_put_be32s(f, &env->a20_mask);
1441 }
1442
1443 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1444 {
1445 CPUState *env = opaque;
1446 int i;
1447 uint32_t hflags;
1448 uint16_t fpus, fpuc, fptag;
1449
1450 if (version_id != 1)
1451 return -EINVAL;
1452 for(i = 0; i < 8; i++)
1453 qemu_get_be32s(f, &env->regs[i]);
1454 qemu_get_be32s(f, &env->eip);
1455 qemu_get_be32s(f, &env->eflags);
1456 qemu_get_be32s(f, &env->eflags);
1457 qemu_get_be32s(f, &hflags);
1458
1459 qemu_get_be16s(f, &fpuc);
1460 qemu_get_be16s(f, &fpus);
1461 qemu_get_be16s(f, &fptag);
1462
1463 for(i = 0; i < 8; i++) {
1464 uint64_t mant;
1465 uint16_t exp;
1466 mant = qemu_get_be64(f);
1467 exp = qemu_get_be16(f);
1468 env->fpregs[i] = cpu_set_fp80(mant, exp);
1469 }
1470
1471 env->fpuc = fpuc;
1472 env->fpstt = (fpus >> 11) & 7;
1473 env->fpus = fpus & ~0x3800;
1474 for(i = 0; i < 8; i++) {
1475 env->fptags[i] = ((fptag & 3) == 3);
1476 fptag >>= 2;
1477 }
1478
1479 for(i = 0; i < 6; i++)
1480 cpu_get_seg(f, &env->segs[i]);
1481 cpu_get_seg(f, &env->ldt);
1482 cpu_get_seg(f, &env->tr);
1483 cpu_get_seg(f, &env->gdt);
1484 cpu_get_seg(f, &env->idt);
1485
1486 qemu_get_be32s(f, &env->sysenter_cs);
1487 qemu_get_be32s(f, &env->sysenter_esp);
1488 qemu_get_be32s(f, &env->sysenter_eip);
1489
1490 qemu_get_be32s(f, &env->cr[0]);
1491 qemu_get_be32s(f, &env->cr[2]);
1492 qemu_get_be32s(f, &env->cr[3]);
1493 qemu_get_be32s(f, &env->cr[4]);
1494
1495 for(i = 0; i < 8; i++)
1496 qemu_get_be32s(f, &env->dr[i]);
1497
1498 /* MMU */
1499 qemu_get_be32s(f, &env->a20_mask);
1500
1501 /* XXX: compute hflags from scratch, except for CPL and IIF */
1502 env->hflags = hflags;
1503 tlb_flush(env, 1);
1504 return 0;
1505 }
1506
1507 #elif defined(TARGET_PPC)
1508 void cpu_save(QEMUFile *f, void *opaque)
1509 {
1510 }
1511
1512 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1513 {
1514 return 0;
1515 }
1516 #else
1517
1518 #warning No CPU save/restore functions
1519
1520 #endif
1521
1522 /***********************************************************/
1523 /* ram save/restore */
1524
1525 /* we just avoid storing empty pages */
1526 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1527 {
1528 int i, v;
1529
1530 v = buf[0];
1531 for(i = 1; i < len; i++) {
1532 if (buf[i] != v)
1533 goto normal_save;
1534 }
1535 qemu_put_byte(f, 1);
1536 qemu_put_byte(f, v);
1537 return;
1538 normal_save:
1539 qemu_put_byte(f, 0);
1540 qemu_put_buffer(f, buf, len);
1541 }
1542
1543 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1544 {
1545 int v;
1546
1547 v = qemu_get_byte(f);
1548 switch(v) {
1549 case 0:
1550 if (qemu_get_buffer(f, buf, len) != len)
1551 return -EIO;
1552 break;
1553 case 1:
1554 v = qemu_get_byte(f);
1555 memset(buf, v, len);
1556 break;
1557 default:
1558 return -EINVAL;
1559 }
1560 return 0;
1561 }
1562
1563 static void ram_save(QEMUFile *f, void *opaque)
1564 {
1565 int i;
1566 qemu_put_be32(f, phys_ram_size);
1567 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1568 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1569 }
1570 }
1571
1572 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1573 {
1574 int i, ret;
1575
1576 if (version_id != 1)
1577 return -EINVAL;
1578 if (qemu_get_be32(f) != phys_ram_size)
1579 return -EINVAL;
1580 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1581 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1582 if (ret)
1583 return ret;
1584 }
1585 return 0;
1586 }
1587
1588 /***********************************************************/
1589 /* main execution loop */
1590
1591 void gui_update(void *opaque)
1592 {
1593 display_state.dpy_refresh(&display_state);
1594 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1595 }
1596
1597 /* XXX: support several handlers */
1598 VMStopHandler *vm_stop_cb;
1599 VMStopHandler *vm_stop_opaque;
1600
1601 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1602 {
1603 vm_stop_cb = cb;
1604 vm_stop_opaque = opaque;
1605 return 0;
1606 }
1607
1608 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1609 {
1610 vm_stop_cb = NULL;
1611 }
1612
1613 void vm_start(void)
1614 {
1615 if (!vm_running) {
1616 cpu_enable_ticks();
1617 vm_running = 1;
1618 }
1619 }
1620
1621 void vm_stop(int reason)
1622 {
1623 if (vm_running) {
1624 cpu_disable_ticks();
1625 vm_running = 0;
1626 if (reason != 0) {
1627 if (vm_stop_cb) {
1628 vm_stop_cb(vm_stop_opaque, reason);
1629 }
1630 }
1631 }
1632 }
1633
1634 int main_loop(void)
1635 {
1636 #ifndef _WIN32
1637 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1638 IOHandlerRecord *ioh, *ioh_next;
1639 uint8_t buf[4096];
1640 int n, max_size;
1641 #endif
1642 int ret, timeout;
1643 CPUState *env = global_env;
1644
1645 for(;;) {
1646 if (vm_running) {
1647 ret = cpu_exec(env);
1648 if (reset_requested) {
1649 ret = EXCP_INTERRUPT;
1650 break;
1651 }
1652 if (ret == EXCP_DEBUG) {
1653 vm_stop(EXCP_DEBUG);
1654 }
1655 /* if hlt instruction, we wait until the next IRQ */
1656 /* XXX: use timeout computed from timers */
1657 if (ret == EXCP_HLT)
1658 timeout = 10;
1659 else
1660 timeout = 0;
1661 } else {
1662 timeout = 10;
1663 }
1664
1665 #ifdef _WIN32
1666 if (timeout > 0)
1667 Sleep(timeout);
1668 #else
1669
1670 /* poll any events */
1671 /* XXX: separate device handlers from system ones */
1672 pf = ufds;
1673 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1674 if (!ioh->fd_can_read) {
1675 max_size = 0;
1676 pf->fd = ioh->fd;
1677 pf->events = POLLIN;
1678 ioh->ufd = pf;
1679 pf++;
1680 } else {
1681 max_size = ioh->fd_can_read(ioh->opaque);
1682 if (max_size > 0) {
1683 if (max_size > sizeof(buf))
1684 max_size = sizeof(buf);
1685 pf->fd = ioh->fd;
1686 pf->events = POLLIN;
1687 ioh->ufd = pf;
1688 pf++;
1689 } else {
1690 ioh->ufd = NULL;
1691 }
1692 }
1693 ioh->max_size = max_size;
1694 }
1695
1696 ret = poll(ufds, pf - ufds, timeout);
1697 if (ret > 0) {
1698 /* XXX: better handling of removal */
1699 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1700 ioh_next = ioh->next;
1701 pf = ioh->ufd;
1702 if (pf) {
1703 if (pf->revents & POLLIN) {
1704 if (ioh->max_size == 0) {
1705 /* just a read event */
1706 ioh->fd_read(ioh->opaque, NULL, 0);
1707 } else {
1708 n = read(ioh->fd, buf, ioh->max_size);
1709 if (n >= 0) {
1710 ioh->fd_read(ioh->opaque, buf, n);
1711 } else if (errno != -EAGAIN) {
1712 ioh->fd_read(ioh->opaque, NULL, -errno);
1713 }
1714 }
1715 }
1716 }
1717 }
1718 }
1719
1720 #if defined(CONFIG_SLIRP)
1721 /* XXX: merge with poll() */
1722 if (slirp_inited) {
1723 fd_set rfds, wfds, xfds;
1724 int nfds;
1725 struct timeval tv;
1726
1727 nfds = -1;
1728 FD_ZERO(&rfds);
1729 FD_ZERO(&wfds);
1730 FD_ZERO(&xfds);
1731 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1732 tv.tv_sec = 0;
1733 tv.tv_usec = 0;
1734 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1735 if (ret >= 0) {
1736 slirp_select_poll(&rfds, &wfds, &xfds);
1737 }
1738 }
1739 #endif
1740
1741 #endif
1742
1743 if (vm_running) {
1744 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1745 qemu_get_clock(vm_clock));
1746
1747 /* XXX: add explicit timer */
1748 SB16_run();
1749
1750 /* run dma transfers, if any */
1751 DMA_run();
1752 }
1753
1754 /* real time timers */
1755 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1756 qemu_get_clock(rt_clock));
1757 }
1758 cpu_disable_ticks();
1759 return ret;
1760 }
1761
1762 void help(void)
1763 {
1764 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
1765 "usage: %s [options] [disk_image]\n"
1766 "\n"
1767 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1768 "\n"
1769 "Standard options:\n"
1770 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1771 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1772 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1773 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1774 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1775 "-snapshot write to temporary files instead of disk image files\n"
1776 "-m megs set virtual RAM size to megs MB\n"
1777 "-nographic disable graphical output and redirect serial I/Os to console\n"
1778 "\n"
1779 "Network options:\n"
1780 "-nics n simulate 'n' network cards [default=1]\n"
1781 "-macaddr addr set the mac address of the first interface\n"
1782 "-n script set tap/tun network init script [default=%s]\n"
1783 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1784 #ifdef CONFIG_SLIRP
1785 "-user-net use user mode network stack [default if no tap/tun script]\n"
1786 #endif
1787 "-dummy-net use dummy network stack\n"
1788 "\n"
1789 "Linux boot specific:\n"
1790 "-kernel bzImage use 'bzImage' as kernel image\n"
1791 "-append cmdline use 'cmdline' as kernel command line\n"
1792 "-initrd file use 'file' as initial ram disk\n"
1793 "\n"
1794 "Debug/Expert options:\n"
1795 "-s wait gdb connection to port %d\n"
1796 "-p port change gdb connection port\n"
1797 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1798 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1799 "-L path set the directory for the BIOS and VGA BIOS\n"
1800 #ifdef USE_CODE_COPY
1801 "-no-code-copy disable code copy acceleration\n"
1802 #endif
1803
1804 "\n"
1805 "During emulation, use C-a h to get terminal commands:\n",
1806 #ifdef CONFIG_SOFTMMU
1807 "qemu",
1808 #else
1809 "qemu-fast",
1810 #endif
1811 DEFAULT_NETWORK_SCRIPT,
1812 DEFAULT_GDBSTUB_PORT,
1813 "/tmp/qemu.log");
1814 term_print_help();
1815 #ifndef CONFIG_SOFTMMU
1816 printf("\n"
1817 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1818 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1819 "PC emulation.\n");
1820 #endif
1821 exit(1);
1822 }
1823
1824 struct option long_options[] = {
1825 { "initrd", 1, NULL, 0, },
1826 { "hda", 1, NULL, 0, },
1827 { "hdb", 1, NULL, 0, },
1828 { "snapshot", 0, NULL, 0, },
1829 { "hdachs", 1, NULL, 0, },
1830 { "nographic", 0, NULL, 0, },
1831 { "kernel", 1, NULL, 0, },
1832 { "append", 1, NULL, 0, },
1833 { "tun-fd", 1, NULL, 0, },
1834 { "hdc", 1, NULL, 0, },
1835 { "hdd", 1, NULL, 0, },
1836 { "cdrom", 1, NULL, 0, },
1837 { "boot", 1, NULL, 0, },
1838 { "fda", 1, NULL, 0, },
1839 { "fdb", 1, NULL, 0, },
1840 { "no-code-copy", 0, NULL, 0 },
1841 { "nics", 1, NULL, 0 },
1842 { "macaddr", 1, NULL, 0 },
1843 { "user-net", 0, NULL, 0 },
1844 { "dummy-net", 0, NULL, 0 },
1845 { NULL, 0, NULL, 0 },
1846 };
1847
1848 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1849
1850 /* this stack is only used during signal handling */
1851 #define SIGNAL_STACK_SIZE 32768
1852
1853 static uint8_t *signal_stack;
1854
1855 #endif
1856
1857 #define NET_IF_TUN 0
1858 #define NET_IF_USER 1
1859 #define NET_IF_DUMMY 2
1860
1861 int main(int argc, char **argv)
1862 {
1863 #ifdef CONFIG_GDBSTUB
1864 int use_gdbstub, gdbstub_port;
1865 #endif
1866 int c, i, long_index, has_cdrom;
1867 int snapshot, linux_boot;
1868 CPUState *env;
1869 const char *initrd_filename;
1870 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
1871 const char *kernel_filename, *kernel_cmdline;
1872 DisplayState *ds = &display_state;
1873 int cyls, heads, secs;
1874 int start_emulation = 1;
1875 uint8_t macaddr[6];
1876 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
1877
1878 #if !defined(CONFIG_SOFTMMU)
1879 /* we never want that malloc() uses mmap() */
1880 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
1881 #endif
1882 initrd_filename = NULL;
1883 for(i = 0; i < MAX_FD; i++)
1884 fd_filename[i] = NULL;
1885 for(i = 0; i < MAX_DISKS; i++)
1886 hd_filename[i] = NULL;
1887 ram_size = 32 * 1024 * 1024;
1888 vga_ram_size = VGA_RAM_SIZE;
1889 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
1890 #ifdef CONFIG_GDBSTUB
1891 use_gdbstub = 0;
1892 gdbstub_port = DEFAULT_GDBSTUB_PORT;
1893 #endif
1894 snapshot = 0;
1895 nographic = 0;
1896 kernel_filename = NULL;
1897 kernel_cmdline = "";
1898 has_cdrom = 1;
1899 cyls = heads = secs = 0;
1900
1901 nb_tun_fds = 0;
1902 net_if_type = -1;
1903 nb_nics = 1;
1904 /* default mac address of the first network interface */
1905 macaddr[0] = 0x52;
1906 macaddr[1] = 0x54;
1907 macaddr[2] = 0x00;
1908 macaddr[3] = 0x12;
1909 macaddr[4] = 0x34;
1910 macaddr[5] = 0x56;
1911
1912
1913 for(;;) {
1914 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
1915 if (c == -1)
1916 break;
1917 switch(c) {
1918 case 0:
1919 switch(long_index) {
1920 case 0:
1921 initrd_filename = optarg;
1922 break;
1923 case 1:
1924 hd_filename[0] = optarg;
1925 break;
1926 case 2:
1927 hd_filename[1] = optarg;
1928 break;
1929 case 3:
1930 snapshot = 1;
1931 break;
1932 case 4:
1933 {
1934 const char *p;
1935 p = optarg;
1936 cyls = strtol(p, (char **)&p, 0);
1937 if (*p != ',')
1938 goto chs_fail;
1939 p++;
1940 heads = strtol(p, (char **)&p, 0);
1941 if (*p != ',')
1942 goto chs_fail;
1943 p++;
1944 secs = strtol(p, (char **)&p, 0);
1945 if (*p != '\0') {
1946 chs_fail:
1947 cyls = 0;
1948 }
1949 }
1950 break;
1951 case 5:
1952 nographic = 1;
1953 break;
1954 case 6:
1955 kernel_filename = optarg;
1956 break;
1957 case 7:
1958 kernel_cmdline = optarg;
1959 break;
1960 case 8:
1961 {
1962 const char *p;
1963 int fd;
1964 if (nb_tun_fds < MAX_NICS) {
1965 fd = strtol(optarg, (char **)&p, 0);
1966 if (*p != '\0') {
1967 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
1968 exit(1);
1969 }
1970 tun_fds[nb_tun_fds++] = fd;
1971 }
1972 }
1973 break;
1974 case 9:
1975 hd_filename[2] = optarg;
1976 has_cdrom = 0;
1977 break;
1978 case 10:
1979 hd_filename[3] = optarg;
1980 break;
1981 case 11:
1982 hd_filename[2] = optarg;
1983 has_cdrom = 1;
1984 break;
1985 case 12:
1986 boot_device = optarg[0];
1987 if (boot_device != 'a' && boot_device != 'b' &&
1988 boot_device != 'c' && boot_device != 'd') {
1989 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1990 exit(1);
1991 }
1992 break;
1993 case 13:
1994 fd_filename[0] = optarg;
1995 break;
1996 case 14:
1997 fd_filename[1] = optarg;
1998 break;
1999 case 15:
2000 code_copy_enabled = 0;
2001 break;
2002 case 16:
2003 nb_nics = atoi(optarg);
2004 if (nb_nics < 1 || nb_nics > MAX_NICS) {
2005 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2006 exit(1);
2007 }
2008 break;
2009 case 17:
2010 {
2011 const char *p;
2012 int i;
2013 p = optarg;
2014 for(i = 0; i < 6; i++) {
2015 macaddr[i] = strtol(p, (char **)&p, 16);
2016 if (i == 5) {
2017 if (*p != '\0')
2018 goto macaddr_error;
2019 } else {
2020 if (*p != ':') {
2021 macaddr_error:
2022 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2023 exit(1);
2024 }
2025 p++;
2026 }
2027 }
2028 }
2029 break;
2030 case 18:
2031 net_if_type = NET_IF_USER;
2032 break;
2033 case 19:
2034 net_if_type = NET_IF_DUMMY;
2035 break;
2036 }
2037 break;
2038 case 'h':
2039 help();
2040 break;
2041 case 'm':
2042 ram_size = atoi(optarg) * 1024 * 1024;
2043 if (ram_size <= 0)
2044 help();
2045 if (ram_size > PHYS_RAM_MAX_SIZE) {
2046 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2047 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2048 exit(1);
2049 }
2050 break;
2051 case 'd':
2052 {
2053 int mask;
2054 CPULogItem *item;
2055
2056 mask = cpu_str_to_log_mask(optarg);
2057 if (!mask) {
2058 printf("Log items (comma separated):\n");
2059 for(item = cpu_log_items; item->mask != 0; item++) {
2060 printf("%-10s %s\n", item->name, item->help);
2061 }
2062 exit(1);
2063 }
2064 cpu_set_log(mask);
2065 }
2066 break;
2067 case 'n':
2068 pstrcpy(network_script, sizeof(network_script), optarg);
2069 break;
2070 #ifdef CONFIG_GDBSTUB
2071 case 's':
2072 use_gdbstub = 1;
2073 break;
2074 case 'p':
2075 gdbstub_port = atoi(optarg);
2076 break;
2077 #endif
2078 case 'L':
2079 bios_dir = optarg;
2080 break;
2081 case 'S':
2082 start_emulation = 0;
2083 break;
2084 }
2085 }
2086
2087 if (optind < argc) {
2088 hd_filename[0] = argv[optind++];
2089 }
2090
2091 linux_boot = (kernel_filename != NULL);
2092
2093 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2094 fd_filename[0] == '\0')
2095 help();
2096
2097 /* boot to cd by default if no hard disk */
2098 if (hd_filename[0] == '\0' && boot_device == 'c') {
2099 if (fd_filename[0] != '\0')
2100 boot_device = 'a';
2101 else
2102 boot_device = 'd';
2103 }
2104
2105 #if !defined(CONFIG_SOFTMMU)
2106 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2107 {
2108 static uint8_t stdout_buf[4096];
2109 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2110 }
2111 #else
2112 setvbuf(stdout, NULL, _IOLBF, 0);
2113 #endif
2114
2115 /* init host network redirectors */
2116 if (net_if_type == -1) {
2117 net_if_type = NET_IF_TUN;
2118 #if defined(CONFIG_SLIRP)
2119 if (access(network_script, R_OK) < 0) {
2120 net_if_type = NET_IF_USER;
2121 }
2122 #endif
2123 }
2124
2125 for(i = 0; i < nb_nics; i++) {
2126 NetDriverState *nd = &nd_table[i];
2127 nd->index = i;
2128 /* init virtual mac address */
2129 nd->macaddr[0] = macaddr[0];
2130 nd->macaddr[1] = macaddr[1];
2131 nd->macaddr[2] = macaddr[2];
2132 nd->macaddr[3] = macaddr[3];
2133 nd->macaddr[4] = macaddr[4];
2134 nd->macaddr[5] = macaddr[5] + i;
2135 switch(net_if_type) {
2136 #if defined(CONFIG_SLIRP)
2137 case NET_IF_USER:
2138 net_slirp_init(nd);
2139 break;
2140 #endif
2141 #if !defined(_WIN32)
2142 case NET_IF_TUN:
2143 if (i < nb_tun_fds) {
2144 net_fd_init(nd, tun_fds[i]);
2145 } else {
2146 if (net_tun_init(nd) < 0)
2147 net_dummy_init(nd);
2148 }
2149 break;
2150 #endif
2151 case NET_IF_DUMMY:
2152 default:
2153 net_dummy_init(nd);
2154 break;
2155 }
2156 }
2157
2158 /* init the memory */
2159 phys_ram_size = ram_size + vga_ram_size;
2160
2161 #ifdef CONFIG_SOFTMMU
2162 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2163 if (!phys_ram_base) {
2164 fprintf(stderr, "Could not allocate physical memory\n");
2165 exit(1);
2166 }
2167 #else
2168 /* as we must map the same page at several addresses, we must use
2169 a fd */
2170 {
2171 const char *tmpdir;
2172
2173 tmpdir = getenv("QEMU_TMPDIR");
2174 if (!tmpdir)
2175 tmpdir = "/tmp";
2176 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2177 if (mkstemp(phys_ram_file) < 0) {
2178 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2179 phys_ram_file);
2180 exit(1);
2181 }
2182 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2183 if (phys_ram_fd < 0) {
2184 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2185 phys_ram_file);
2186 exit(1);
2187 }
2188 ftruncate(phys_ram_fd, phys_ram_size);
2189 unlink(phys_ram_file);
2190 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2191 phys_ram_size,
2192 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2193 phys_ram_fd, 0);
2194 if (phys_ram_base == MAP_FAILED) {
2195 fprintf(stderr, "Could not map physical memory\n");
2196 exit(1);
2197 }
2198 }
2199 #endif
2200
2201 /* we always create the cdrom drive, even if no disk is there */
2202 if (has_cdrom) {
2203 bs_table[2] = bdrv_new("cdrom");
2204 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2205 }
2206
2207 /* open the virtual block devices */
2208 for(i = 0; i < MAX_DISKS; i++) {
2209 if (hd_filename[i]) {
2210 if (!bs_table[i]) {
2211 char buf[64];
2212 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2213 bs_table[i] = bdrv_new(buf);
2214 }
2215 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2216 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2217 hd_filename[i]);
2218 exit(1);
2219 }
2220 if (i == 0 && cyls != 0)
2221 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2222 }
2223 }
2224
2225 /* we always create at least one floppy disk */
2226 fd_table[0] = bdrv_new("fda");
2227 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2228
2229 for(i = 0; i < MAX_FD; i++) {
2230 if (fd_filename[i]) {
2231 if (!fd_table[i]) {
2232 char buf[64];
2233 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2234 fd_table[i] = bdrv_new(buf);
2235 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2236 }
2237 if (fd_filename[i] != '\0') {
2238 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2239 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2240 fd_filename[i]);
2241 exit(1);
2242 }
2243 }
2244 }
2245 }
2246
2247 /* init CPU state */
2248 env = cpu_init();
2249 global_env = env;
2250 cpu_single_env = env;
2251
2252 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2253 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2254 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2255
2256 init_ioports();
2257 cpu_calibrate_ticks();
2258
2259 /* terminal init */
2260 if (nographic) {
2261 dumb_display_init(ds);
2262 } else {
2263 #ifdef CONFIG_SDL
2264 sdl_display_init(ds);
2265 #else
2266 dumb_display_init(ds);
2267 #endif
2268 }
2269
2270 /* setup cpu signal handlers for MMU / self modifying code handling */
2271 #if !defined(CONFIG_SOFTMMU)
2272
2273 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2274 {
2275 stack_t stk;
2276 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2277 stk.ss_sp = signal_stack;
2278 stk.ss_size = SIGNAL_STACK_SIZE;
2279 stk.ss_flags = 0;
2280
2281 if (sigaltstack(&stk, NULL) < 0) {
2282 perror("sigaltstack");
2283 exit(1);
2284 }
2285 }
2286 #endif
2287 {
2288 struct sigaction act;
2289
2290 sigfillset(&act.sa_mask);
2291 act.sa_flags = SA_SIGINFO;
2292 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2293 act.sa_flags |= SA_ONSTACK;
2294 #endif
2295 act.sa_sigaction = host_segv_handler;
2296 sigaction(SIGSEGV, &act, NULL);
2297 sigaction(SIGBUS, &act, NULL);
2298 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2299 sigaction(SIGFPE, &act, NULL);
2300 #endif
2301 }
2302 #endif
2303
2304 #ifndef _WIN32
2305 {
2306 struct sigaction act;
2307 sigfillset(&act.sa_mask);
2308 act.sa_flags = 0;
2309 act.sa_handler = SIG_IGN;
2310 sigaction(SIGPIPE, &act, NULL);
2311 }
2312 #endif
2313 init_timers();
2314
2315 #if defined(TARGET_I386)
2316 pc_init(ram_size, vga_ram_size, boot_device,
2317 ds, fd_filename, snapshot,
2318 kernel_filename, kernel_cmdline, initrd_filename);
2319 #elif defined(TARGET_PPC)
2320 ppc_init(ram_size, vga_ram_size, boot_device,
2321 ds, fd_filename, snapshot,
2322 kernel_filename, kernel_cmdline, initrd_filename);
2323 #endif
2324
2325 /* launched after the device init so that it can display or not a
2326 banner */
2327 monitor_init();
2328
2329 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2330 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2331
2332 #ifdef CONFIG_GDBSTUB
2333 if (use_gdbstub) {
2334 if (gdbserver_start(gdbstub_port) < 0) {
2335 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2336 gdbstub_port);
2337 exit(1);
2338 } else {
2339 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2340 }
2341 } else
2342 #endif
2343 if (start_emulation)
2344 {
2345 vm_start();
2346 }
2347 term_init();
2348 main_loop();
2349 quit_timers();
2350 return 0;
2351 }