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