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