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