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