]> git.proxmox.com Git - qemu.git/blame - vl.c
task switch fixes
[qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
0824d6fc 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
0824d6fc 5 *
1df912cf
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
0824d6fc
FB
23 */
24#include <stdlib.h>
25#include <stdio.h>
1df912cf 26#include <stdarg.h>
0824d6fc 27#include <string.h>
c45886db 28#include <ctype.h>
0824d6fc
FB
29#include <getopt.h>
30#include <inttypes.h>
31#include <unistd.h>
32#include <sys/mman.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <time.h>
36#include <sys/time.h>
37#include <malloc.h>
38#include <termios.h>
39#include <sys/poll.h>
40#include <errno.h>
f1510b2c 41#include <sys/wait.h>
c4b1fcc0 42#include <pty.h>
f1510b2c
FB
43
44#include <sys/ioctl.h>
45#include <sys/socket.h>
46#include <linux/if.h>
47#include <linux/if_tun.h>
0824d6fc 48
0824d6fc 49#include "disas.h"
fc01f7e7
FB
50
51#include "vl.h"
0824d6fc 52
5a67135a 53#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
f1510b2c 54
0824d6fc 55//#define DEBUG_UNUSED_IOPORT
330d0414 56
bb551faa 57#if !defined(CONFIG_SOFTMMU)
7916e224 58#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
bb551faa
FB
59#else
60#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
61#endif
7916e224 62
c45886db 63#if defined (TARGET_I386)
c45886db
FB
64#elif defined (TARGET_PPC)
65//#define USE_OPEN_FIRMWARE
3f5dcc34 66#if !defined (USE_OPEN_FIRMWARE)
c45886db
FB
67#define KERNEL_LOAD_ADDR 0x01000000
68#define KERNEL_STACK_ADDR 0x01200000
69#else
70#define KERNEL_LOAD_ADDR 0x00000000
71#define KERNEL_STACK_ADDR 0x00400000
72#endif
73#endif
0824d6fc 74
313aa567
FB
75#define GUI_REFRESH_INTERVAL 30
76
7dea1da4
FB
77/* XXX: use a two level table to limit memory usage */
78#define MAX_IOPORTS 65536
0824d6fc 79
80cabfad 80const char *bios_dir = CONFIG_QEMU_SHAREDIR;
0824d6fc 81char phys_ram_file[1024];
c45886db
FB
82CPUState *global_env;
83CPUState *cpu_single_env;
c4b1fcc0 84void *ioport_opaque[MAX_IOPORTS];
fc01f7e7
FB
85IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
86IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
c45886db 87BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
313aa567
FB
88int vga_ram_size;
89static DisplayState display_state;
a20dd508 90int nographic;
313aa567
FB
91int term_inited;
92int64_t ticks_per_sec;
36b486bb 93int boot_device = 'c';
1ccde1cb 94static int ram_size;
80cabfad
FB
95static char network_script[1024];
96int pit_min_timer_count = 0;
c4b1fcc0
FB
97int nb_nics;
98NetDriverState nd_table[MAX_NICS];
99SerialState *serial_console;
0824d6fc
FB
100
101/***********************************************************/
102/* x86 io ports */
103
c4b1fcc0 104uint32_t default_ioport_readb(void *opaque, uint32_t address)
0824d6fc
FB
105{
106#ifdef DEBUG_UNUSED_IOPORT
107 fprintf(stderr, "inb: port=0x%04x\n", address);
108#endif
fc01f7e7 109 return 0xff;
0824d6fc
FB
110}
111
c4b1fcc0 112void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
0824d6fc
FB
113{
114#ifdef DEBUG_UNUSED_IOPORT
115 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
116#endif
117}
118
119/* default is to make two byte accesses */
c4b1fcc0 120uint32_t default_ioport_readw(void *opaque, uint32_t address)
0824d6fc
FB
121{
122 uint32_t data;
c4b1fcc0
FB
123 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
124 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
0824d6fc
FB
125 return data;
126}
127
c4b1fcc0 128void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
0824d6fc 129{
c4b1fcc0
FB
130 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
131 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
0824d6fc
FB
132}
133
c4b1fcc0 134uint32_t default_ioport_readl(void *opaque, uint32_t address)
0824d6fc 135{
fc01f7e7
FB
136#ifdef DEBUG_UNUSED_IOPORT
137 fprintf(stderr, "inl: port=0x%04x\n", address);
138#endif
139 return 0xffffffff;
0824d6fc
FB
140}
141
c4b1fcc0 142void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
0824d6fc 143{
fc01f7e7
FB
144#ifdef DEBUG_UNUSED_IOPORT
145 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
146#endif
0824d6fc
FB
147}
148
fc01f7e7 149void init_ioports(void)
0824d6fc
FB
150{
151 int i;
152
fc01f7e7
FB
153 for(i = 0; i < MAX_IOPORTS; i++) {
154 ioport_read_table[0][i] = default_ioport_readb;
155 ioport_write_table[0][i] = default_ioport_writeb;
156 ioport_read_table[1][i] = default_ioport_readw;
157 ioport_write_table[1][i] = default_ioport_writew;
158 ioport_read_table[2][i] = default_ioport_readl;
159 ioport_write_table[2][i] = default_ioport_writel;
160 }
0824d6fc
FB
161}
162
fc01f7e7 163/* size is the word size in byte */
c4b1fcc0
FB
164int register_ioport_read(int start, int length, int size,
165 IOPortReadFunc *func, void *opaque)
f1510b2c 166{
fc01f7e7 167 int i, bsize;
f1510b2c 168
c4b1fcc0 169 if (size == 1) {
fc01f7e7 170 bsize = 0;
c4b1fcc0 171 } else if (size == 2) {
fc01f7e7 172 bsize = 1;
c4b1fcc0 173 } else if (size == 4) {
fc01f7e7 174 bsize = 2;
c4b1fcc0
FB
175 } else {
176 hw_error("register_ioport_read: invalid size");
fc01f7e7 177 return -1;
c4b1fcc0
FB
178 }
179 for(i = start; i < start + length; i += size) {
fc01f7e7 180 ioport_read_table[bsize][i] = func;
c4b1fcc0
FB
181 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
182 hw_error("register_ioport_read: invalid opaque");
183 ioport_opaque[i] = opaque;
184 }
f1510b2c
FB
185 return 0;
186}
187
fc01f7e7 188/* size is the word size in byte */
c4b1fcc0
FB
189int register_ioport_write(int start, int length, int size,
190 IOPortWriteFunc *func, void *opaque)
f1510b2c 191{
fc01f7e7 192 int i, bsize;
f1510b2c 193
c4b1fcc0 194 if (size == 1) {
fc01f7e7 195 bsize = 0;
c4b1fcc0 196 } else if (size == 2) {
fc01f7e7 197 bsize = 1;
c4b1fcc0 198 } else if (size == 4) {
fc01f7e7 199 bsize = 2;
c4b1fcc0
FB
200 } else {
201 hw_error("register_ioport_write: invalid size");
fc01f7e7 202 return -1;
c4b1fcc0
FB
203 }
204 for(i = start; i < start + length; i += size) {
fc01f7e7 205 ioport_write_table[bsize][i] = func;
c4b1fcc0
FB
206 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
207 hw_error("register_ioport_read: invalid opaque");
208 ioport_opaque[i] = opaque;
209 }
f1510b2c
FB
210 return 0;
211}
212
0824d6fc
FB
213void pstrcpy(char *buf, int buf_size, const char *str)
214{
215 int c;
216 char *q = buf;
217
218 if (buf_size <= 0)
219 return;
220
221 for(;;) {
222 c = *str++;
223 if (c == 0 || q >= buf + buf_size - 1)
224 break;
225 *q++ = c;
226 }
227 *q = '\0';
228}
229
230/* strcat and truncate. */
231char *pstrcat(char *buf, int buf_size, const char *s)
232{
233 int len;
234 len = strlen(buf);
235 if (len < buf_size)
236 pstrcpy(buf + len, buf_size - len, s);
237 return buf;
238}
239
0824d6fc
FB
240/* return the size or -1 if error */
241int load_image(const char *filename, uint8_t *addr)
242{
243 int fd, size;
244 fd = open(filename, O_RDONLY);
245 if (fd < 0)
246 return -1;
247 size = lseek(fd, 0, SEEK_END);
248 lseek(fd, 0, SEEK_SET);
249 if (read(fd, addr, size) != size) {
250 close(fd);
251 return -1;
252 }
253 close(fd);
254 return size;
255}
256
c45886db 257void cpu_outb(CPUState *env, int addr, int val)
0824d6fc 258{
c4b1fcc0
FB
259 addr &= (MAX_IOPORTS - 1);
260 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
0824d6fc
FB
261}
262
c45886db 263void cpu_outw(CPUState *env, int addr, int val)
0824d6fc 264{
c4b1fcc0
FB
265 addr &= (MAX_IOPORTS - 1);
266 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
0824d6fc
FB
267}
268
c45886db 269void cpu_outl(CPUState *env, int addr, int val)
0824d6fc 270{
c4b1fcc0
FB
271 addr &= (MAX_IOPORTS - 1);
272 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
0824d6fc
FB
273}
274
c45886db 275int cpu_inb(CPUState *env, int addr)
0824d6fc 276{
c4b1fcc0
FB
277 addr &= (MAX_IOPORTS - 1);
278 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
0824d6fc
FB
279}
280
c45886db 281int cpu_inw(CPUState *env, int addr)
0824d6fc 282{
c4b1fcc0
FB
283 addr &= (MAX_IOPORTS - 1);
284 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
0824d6fc
FB
285}
286
c45886db 287int cpu_inl(CPUState *env, int addr)
0824d6fc 288{
c4b1fcc0
FB
289 addr &= (MAX_IOPORTS - 1);
290 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
0824d6fc
FB
291}
292
293/***********************************************************/
0824d6fc
FB
294void hw_error(const char *fmt, ...)
295{
296 va_list ap;
297
298 va_start(ap, fmt);
299 fprintf(stderr, "qemu: hardware error: ");
300 vfprintf(stderr, fmt, ap);
301 fprintf(stderr, "\n");
302#ifdef TARGET_I386
303 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
c45886db
FB
304#else
305 cpu_dump_state(global_env, stderr, 0);
0824d6fc
FB
306#endif
307 va_end(ap);
308 abort();
309}
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;
344 asm("rdtsc" : "=A" (val));
345 return val;
346}
347
34865134
FB
348#else
349#error unsupported CPU
350#endif
351
352static int64_t cpu_ticks_offset;
353static int64_t cpu_ticks_last;
354
355int64_t cpu_get_ticks(void)
356{
357 return cpu_get_real_ticks() + cpu_ticks_offset;
358}
359
360/* enable cpu_get_ticks() */
361void cpu_enable_ticks(void)
362{
363 cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
364}
365
366/* disable cpu_get_ticks() : the clock is stopped. You must not call
367 cpu_get_ticks() after that. */
368void cpu_disable_ticks(void)
369{
370 cpu_ticks_last = cpu_get_ticks();
371}
372
373int64_t get_clock(void)
374{
375 struct timeval tv;
376 gettimeofday(&tv, NULL);
377 return tv.tv_sec * 1000000LL + tv.tv_usec;
378}
379
0824d6fc
FB
380void cpu_calibrate_ticks(void)
381{
382 int64_t usec, ticks;
383
384 usec = get_clock();
385 ticks = cpu_get_ticks();
386 usleep(50 * 1000);
387 usec = get_clock() - usec;
388 ticks = cpu_get_ticks() - ticks;
389 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
390}
391
87858c89 392/* compute with 96 bit intermediate result: (a*b)/c */
80cabfad 393uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
87858c89
FB
394{
395 union {
396 uint64_t ll;
397 struct {
398#ifdef WORDS_BIGENDIAN
399 uint32_t high, low;
400#else
401 uint32_t low, high;
402#endif
403 } l;
404 } u, res;
405 uint64_t rl, rh;
406
407 u.ll = a;
408 rl = (uint64_t)u.l.low * (uint64_t)b;
409 rh = (uint64_t)u.l.high * (uint64_t)b;
410 rh += (rl >> 32);
411 res.l.high = rh / c;
412 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
413 return res.ll;
414}
415
c4b1fcc0
FB
416/***********************************************************/
417/* serial device */
313aa567 418
c4b1fcc0 419int serial_open_device(void)
313aa567 420{
c4b1fcc0
FB
421 char slave_name[1024];
422 int master_fd, slave_fd;
313aa567 423
c4b1fcc0
FB
424 if (serial_console == NULL && nographic) {
425 /* use console for serial port */
426 return 0;
80cabfad 427 } else {
c4b1fcc0
FB
428 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
429 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
430 return -1;
431 }
432 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
433 return master_fd;
330d0414 434 }
330d0414
FB
435}
436
80cabfad
FB
437/***********************************************************/
438/* Linux network device redirector */
330d0414 439
c4b1fcc0 440static int tun_open(char *ifname, int ifname_size)
330d0414 441{
80cabfad 442 struct ifreq ifr;
c4b1fcc0 443 int fd, ret;
80cabfad
FB
444
445 fd = open("/dev/net/tun", O_RDWR);
446 if (fd < 0) {
447 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
448 return -1;
330d0414 449 }
80cabfad
FB
450 memset(&ifr, 0, sizeof(ifr));
451 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
452 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
453 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
454 if (ret != 0) {
455 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
456 close(fd);
457 return -1;
458 }
459 printf("Connected to host network interface: %s\n", ifr.ifr_name);
c4b1fcc0 460 pstrcpy(ifname, ifname_size, ifr.ifr_name);
80cabfad 461 fcntl(fd, F_SETFL, O_NONBLOCK);
c4b1fcc0
FB
462 return fd;
463}
330d0414 464
c4b1fcc0
FB
465static int net_init(void)
466{
467 int pid, status, launch_script, i;
468 NetDriverState *nd;
469 char *args[MAX_NICS + 2];
470 char **parg;
471
472 launch_script = 0;
473 for(i = 0; i < nb_nics; i++) {
474 nd = &nd_table[i];
475 if (nd->fd < 0) {
476 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
477 if (nd->fd >= 0)
478 launch_script = 1;
80cabfad 479 }
c4b1fcc0
FB
480 }
481
482 if (launch_script) {
483 /* try to launch network init script */
484 pid = fork();
485 if (pid >= 0) {
486 if (pid == 0) {
487 parg = args;
488 *parg++ = network_script;
489 for(i = 0; i < nb_nics; i++) {
490 nd = &nd_table[i];
491 if (nd->fd >= 0) {
492 *parg++ = nd->ifname;
493 }
494 }
495 *parg++ = NULL;
496 execv(network_script, args);
497 exit(1);
498 }
499 while (waitpid(pid, &status, 0) != pid);
500 if (!WIFEXITED(status) ||
501 WEXITSTATUS(status) != 0) {
502 fprintf(stderr, "%s: could not launch network script\n",
503 network_script);
504 }
80cabfad 505 }
330d0414 506 }
80cabfad 507 return 0;
330d0414
FB
508}
509
c4b1fcc0 510void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
330d0414 511{
80cabfad
FB
512#ifdef DEBUG_NE2000
513 printf("NE2000: sending packet size=%d\n", size);
c45886db 514#endif
c4b1fcc0 515 write(nd->fd, buf, size);
80cabfad 516}
330d0414 517
313aa567
FB
518/***********************************************************/
519/* dumb display */
520
521/* init terminal so that we can grab keys */
522static struct termios oldtty;
523
524static void term_exit(void)
525{
526 tcsetattr (0, TCSANOW, &oldtty);
527}
528
529static void term_init(void)
530{
531 struct termios tty;
532
533 tcgetattr (0, &tty);
534 oldtty = tty;
535
536 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
537 |INLCR|IGNCR|ICRNL|IXON);
538 tty.c_oflag |= OPOST;
a20dd508
FB
539 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
540 /* if graphical mode, we allow Ctrl-C handling */
541 if (nographic)
542 tty.c_lflag &= ~ISIG;
313aa567
FB
543 tty.c_cflag &= ~(CSIZE|PARENB);
544 tty.c_cflag |= CS8;
545 tty.c_cc[VMIN] = 1;
546 tty.c_cc[VTIME] = 0;
547
548 tcsetattr (0, TCSANOW, &tty);
549
550 atexit(term_exit);
551
552 fcntl(0, F_SETFL, O_NONBLOCK);
553}
554
555static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
556{
557}
558
559static void dumb_resize(DisplayState *ds, int w, int h)
560{
561}
562
563static void dumb_refresh(DisplayState *ds)
564{
565 vga_update_display();
566}
567
568void dumb_display_init(DisplayState *ds)
569{
570 ds->data = NULL;
571 ds->linesize = 0;
572 ds->depth = 0;
573 ds->dpy_update = dumb_update;
574 ds->dpy_resize = dumb_resize;
575 ds->dpy_refresh = dumb_refresh;
576}
577
3a51dee6 578#if !defined(CONFIG_SOFTMMU)
f1510b2c 579/***********************************************************/
0824d6fc
FB
580/* cpu signal handler */
581static void host_segv_handler(int host_signum, siginfo_t *info,
582 void *puc)
583{
584 if (cpu_signal_handler(host_signum, info, puc))
585 return;
586 term_exit();
587 abort();
588}
3a51dee6 589#endif
0824d6fc
FB
590
591static int timer_irq_pending;
87858c89 592static int timer_irq_count;
0824d6fc 593
313aa567
FB
594static int timer_ms;
595static int gui_refresh_pending, gui_refresh_count;
596
0824d6fc
FB
597static void host_alarm_handler(int host_signum, siginfo_t *info,
598 void *puc)
599{
87858c89
FB
600 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
601 some drift between cpu_get_ticks() and the interrupt time. So
602 we queue some interrupts to avoid missing some */
603 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
604 if (timer_irq_count) {
605 if (timer_irq_count > 2)
606 timer_irq_count = 2;
607 timer_irq_count--;
313aa567
FB
608 timer_irq_pending = 1;
609 }
610 gui_refresh_count += timer_ms;
611 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
612 gui_refresh_count = 0;
613 gui_refresh_pending = 1;
614 }
615
616 if (gui_refresh_pending || timer_irq_pending) {
87858c89 617 /* just exit from the cpu to have a chance to handle timers */
c45886db 618 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
87858c89 619 }
0824d6fc
FB
620}
621
c4b1fcc0
FB
622#define MAX_IO_HANDLERS 64
623
624typedef struct IOHandlerRecord {
625 int fd;
626 IOCanRWHandler *fd_can_read;
627 IOReadHandler *fd_read;
628 void *opaque;
629 /* temporary data */
630 struct pollfd *ufd;
631 int max_size;
632} IOHandlerRecord;
633
634static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
635static int nb_io_handlers = 0;
636
637int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
638 IOReadHandler *fd_read, void *opaque)
639{
640 IOHandlerRecord *ioh;
641
642 if (nb_io_handlers >= MAX_IO_HANDLERS)
643 return -1;
644 ioh = &io_handlers[nb_io_handlers];
645 ioh->fd = fd;
646 ioh->fd_can_read = fd_can_read;
647 ioh->fd_read = fd_read;
648 ioh->opaque = opaque;
649 nb_io_handlers++;
650 return 0;
651}
652
b4608c04
FB
653/* main execution loop */
654
655CPUState *cpu_gdbstub_get_env(void *opaque)
656{
657 return global_env;
658}
659
4c3a88a2 660int main_loop(void *opaque)
b4608c04 661{
c4b1fcc0
FB
662 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
663 int ret, n, timeout, serial_ok, max_size, i;
664 uint8_t buf[4096];
665 IOHandlerRecord *ioh;
b4608c04
FB
666 CPUState *env = global_env;
667
a20dd508 668 if (!term_inited) {
313aa567
FB
669 /* initialize terminal only there so that the user has a
670 chance to stop QEMU with Ctrl-C before the gdb connection
671 is launched */
672 term_inited = 1;
673 term_init();
674 }
675
27c3f2cb 676 serial_ok = 1;
34865134 677 cpu_enable_ticks();
b4608c04 678 for(;;) {
c45886db
FB
679#if defined (DO_TB_FLUSH)
680 tb_flush();
681#endif
682 ret = cpu_exec(env);
34865134
FB
683 if (reset_requested) {
684 ret = EXCP_INTERRUPT;
cd4c3e88 685 break;
34865134
FB
686 }
687 if (ret == EXCP_DEBUG) {
688 ret = EXCP_DEBUG;
689 break;
690 }
b4608c04
FB
691 /* if hlt instruction, we wait until the next IRQ */
692 if (ret == EXCP_HLT)
693 timeout = 10;
694 else
695 timeout = 0;
c4b1fcc0 696
b4608c04 697 /* poll any events */
b4608c04 698 pf = ufds;
c4b1fcc0
FB
699 ioh = io_handlers;
700 for(i = 0; i < nb_io_handlers; i++) {
701 max_size = ioh->fd_can_read(ioh->opaque);
702 if (max_size > 0) {
703 if (max_size > sizeof(buf))
704 max_size = sizeof(buf);
705 pf->fd = ioh->fd;
706 pf->events = POLLIN;
707 ioh->ufd = pf;
708 pf++;
709 } else {
710 ioh->ufd = NULL;
711 }
712 ioh->max_size = max_size;
713 ioh++;
b4608c04 714 }
c4b1fcc0 715
b4608c04
FB
716 gdb_ufd = NULL;
717 if (gdbstub_fd > 0) {
718 gdb_ufd = pf;
719 pf->fd = gdbstub_fd;
720 pf->events = POLLIN;
721 pf++;
722 }
723
724 ret = poll(ufds, pf - ufds, timeout);
725 if (ret > 0) {
c4b1fcc0
FB
726 ioh = io_handlers;
727 for(i = 0; i < nb_io_handlers; i++) {
728 pf = ioh->ufd;
729 if (pf) {
730 n = read(ioh->fd, buf, ioh->max_size);
731 if (n > 0) {
732 ioh->fd_read(ioh->opaque, buf, n);
b4608c04 733 }
b4608c04 734 }
c4b1fcc0 735 ioh++;
b4608c04
FB
736 }
737 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
738 uint8_t buf[1];
739 /* stop emulation if requested by gdb */
740 n = read(gdbstub_fd, buf, 1);
34865134
FB
741 if (n == 1) {
742 ret = EXCP_INTERRUPT;
b4608c04 743 break;
34865134 744 }
b4608c04
FB
745 }
746 }
747
748 /* timer IRQ */
749 if (timer_irq_pending) {
c45886db 750#if defined (TARGET_I386)
b4608c04
FB
751 pic_set_irq(0, 1);
752 pic_set_irq(0, 0);
753 timer_irq_pending = 0;
80cabfad 754 rtc_timer();
c45886db 755#endif
b4608c04 756 }
8dc75d75
FB
757 /* XXX: add explicit timer */
758 SB16_run();
759
760 /* run dma transfers, if any */
761 DMA_run();
313aa567
FB
762
763 /* VGA */
764 if (gui_refresh_pending) {
765 display_state.dpy_refresh(&display_state);
766 gui_refresh_pending = 0;
767 }
b4608c04 768 }
34865134
FB
769 cpu_disable_ticks();
770 return ret;
b4608c04
FB
771}
772
0824d6fc
FB
773void help(void)
774{
a20dd508 775 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
0db63474 776 "usage: %s [options] [disk_image]\n"
0824d6fc 777 "\n"
a20dd508 778 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
fc01f7e7 779 "\n"
a20dd508 780 "Standard options:\n"
c45886db 781 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
36b486bb
FB
782 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
783 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
c4b1fcc0 784 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
6e44ba7f 785 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
a20dd508
FB
786 "-snapshot write to temporary files instead of disk image files\n"
787 "-m megs set virtual RAM size to megs MB\n"
c4b1fcc0
FB
788 "-nographic disable graphical output and redirect serial I/Os to console\n"
789 "\n"
790 "Network options:\n"
a20dd508 791 "-n script set network init script [default=%s]\n"
c4b1fcc0
FB
792 "-nics n simulate 'n' network interfaces [default=1]\n"
793 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
a20dd508 794 "\n"
c4b1fcc0 795 "Linux boot specific:\n"
a20dd508
FB
796 "-kernel bzImage use 'bzImage' as kernel image\n"
797 "-append cmdline use 'cmdline' as kernel command line\n"
798 "-initrd file use 'file' as initial ram disk\n"
fc01f7e7 799 "\n"
330d0414 800 "Debug/Expert options:\n"
a20dd508
FB
801 "-s wait gdb connection to port %d\n"
802 "-p port change gdb connection port\n"
f193c797 803 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
a20dd508
FB
804 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
805 "-L path set the directory for the BIOS and VGA BIOS\n"
77fef8c1
FB
806#ifdef USE_CODE_COPY
807 "-no-code-copy disable code copy acceleration\n"
808#endif
809
0824d6fc 810 "\n"
f1510b2c 811 "During emulation, use C-a h to get terminal commands:\n",
0db63474
FB
812#ifdef CONFIG_SOFTMMU
813 "qemu",
814#else
815 "qemu-fast",
816#endif
817 DEFAULT_NETWORK_SCRIPT,
6e44ba7f
FB
818 DEFAULT_GDBSTUB_PORT,
819 "/tmp/qemu.log");
0824d6fc 820 term_print_help();
0db63474
FB
821#ifndef CONFIG_SOFTMMU
822 printf("\n"
823 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
824 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
825 "PC emulation.\n");
826#endif
0824d6fc
FB
827 exit(1);
828}
829
fc01f7e7
FB
830struct option long_options[] = {
831 { "initrd", 1, NULL, 0, },
832 { "hda", 1, NULL, 0, },
833 { "hdb", 1, NULL, 0, },
33e3963e 834 { "snapshot", 0, NULL, 0, },
330d0414 835 { "hdachs", 1, NULL, 0, },
a20dd508
FB
836 { "nographic", 0, NULL, 0, },
837 { "kernel", 1, NULL, 0, },
838 { "append", 1, NULL, 0, },
42f1e0e4 839 { "tun-fd", 1, NULL, 0, },
36b486bb
FB
840 { "hdc", 1, NULL, 0, },
841 { "hdd", 1, NULL, 0, },
842 { "cdrom", 1, NULL, 0, },
843 { "boot", 1, NULL, 0, },
c45886db
FB
844 { "fda", 1, NULL, 0, },
845 { "fdb", 1, NULL, 0, },
c4b1fcc0
FB
846 { "no-code-copy", 0, NULL, 0 },
847 { "nics", 1, NULL, 0 },
fc01f7e7
FB
848 { NULL, 0, NULL, 0 },
849};
850
a20dd508
FB
851#ifdef CONFIG_SDL
852/* SDL use the pthreads and they modify sigaction. We don't
853 want that. */
dc887a4d 854#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
a20dd508
FB
855extern void __libc_sigaction();
856#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
857#else
858extern void __sigaction();
859#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
860#endif
861#endif /* CONFIG_SDL */
862
77fef8c1
FB
863#if defined (TARGET_I386) && defined(USE_CODE_COPY)
864
865/* this stack is only used during signal handling */
866#define SIGNAL_STACK_SIZE 32768
867
868static uint8_t *signal_stack;
869
870#endif
871
0824d6fc
FB
872int main(int argc, char **argv)
873{
c4b1fcc0 874 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
1ccde1cb 875 int snapshot, linux_boot;
0824d6fc
FB
876 struct sigaction act;
877 struct itimerval itv;
c45886db 878 CPUState *env;
7f7f9873 879 const char *initrd_filename;
c45886db 880 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
a20dd508 881 const char *kernel_filename, *kernel_cmdline;
313aa567 882 DisplayState *ds = &display_state;
c4b1fcc0 883 int cyls, heads, secs;
313aa567 884
0824d6fc
FB
885 /* we never want that malloc() uses mmap() */
886 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
fc01f7e7 887 initrd_filename = NULL;
c45886db
FB
888 for(i = 0; i < MAX_FD; i++)
889 fd_filename[i] = NULL;
fc01f7e7
FB
890 for(i = 0; i < MAX_DISKS; i++)
891 hd_filename[i] = NULL;
1ccde1cb 892 ram_size = 32 * 1024 * 1024;
313aa567 893 vga_ram_size = VGA_RAM_SIZE;
f1510b2c 894 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
b4608c04
FB
895 use_gdbstub = 0;
896 gdbstub_port = DEFAULT_GDBSTUB_PORT;
33e3963e 897 snapshot = 0;
a20dd508
FB
898 nographic = 0;
899 kernel_filename = NULL;
900 kernel_cmdline = "";
c4b1fcc0
FB
901 has_cdrom = 1;
902 cyls = heads = secs = 0;
903
904 nb_nics = 1;
905 for(i = 0; i < MAX_NICS; i++) {
906 NetDriverState *nd = &nd_table[i];
907 nd->fd = -1;
908 /* init virtual mac address */
909 nd->macaddr[0] = 0x52;
910 nd->macaddr[1] = 0x54;
911 nd->macaddr[2] = 0x00;
912 nd->macaddr[3] = 0x12;
913 nd->macaddr[4] = 0x34;
914 nd->macaddr[5] = 0x56 + i;
915 }
916
0824d6fc 917 for(;;) {
f193c797 918 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:", long_options, &long_index);
0824d6fc
FB
919 if (c == -1)
920 break;
921 switch(c) {
fc01f7e7
FB
922 case 0:
923 switch(long_index) {
924 case 0:
925 initrd_filename = optarg;
926 break;
927 case 1:
928 hd_filename[0] = optarg;
929 break;
930 case 2:
931 hd_filename[1] = optarg;
932 break;
33e3963e
FB
933 case 3:
934 snapshot = 1;
935 break;
330d0414
FB
936 case 4:
937 {
330d0414
FB
938 const char *p;
939 p = optarg;
940 cyls = strtol(p, (char **)&p, 0);
941 if (*p != ',')
942 goto chs_fail;
943 p++;
944 heads = strtol(p, (char **)&p, 0);
945 if (*p != ',')
946 goto chs_fail;
947 p++;
948 secs = strtol(p, (char **)&p, 0);
c4b1fcc0
FB
949 if (*p != '\0') {
950 chs_fail:
951 cyls = 0;
952 }
330d0414
FB
953 }
954 break;
313aa567 955 case 5:
a20dd508
FB
956 nographic = 1;
957 break;
958 case 6:
959 kernel_filename = optarg;
960 break;
961 case 7:
962 kernel_cmdline = optarg;
313aa567 963 break;
42f1e0e4 964 case 8:
c4b1fcc0
FB
965 {
966 const char *p;
967 int fd;
968 p = optarg;
969 nb_nics = 0;
970 for(;;) {
971 fd = strtol(p, (char **)&p, 0);
972 nd_table[nb_nics].fd = fd;
973 snprintf(nd_table[nb_nics].ifname,
974 sizeof(nd_table[nb_nics].ifname),
975 "fd%d", nb_nics);
976 nb_nics++;
977 if (*p == ',') {
978 p++;
979 } else if (*p != '\0') {
980 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
981 exit(1);
4e463d8d
FB
982 } else {
983 break;
c4b1fcc0
FB
984 }
985 }
986 }
42f1e0e4 987 break;
36b486bb
FB
988 case 9:
989 hd_filename[2] = optarg;
c4b1fcc0 990 has_cdrom = 0;
36b486bb
FB
991 break;
992 case 10:
993 hd_filename[3] = optarg;
994 break;
995 case 11:
996 hd_filename[2] = optarg;
c4b1fcc0 997 has_cdrom = 1;
36b486bb
FB
998 break;
999 case 12:
1000 boot_device = optarg[0];
c45886db
FB
1001 if (boot_device != 'a' && boot_device != 'b' &&
1002 boot_device != 'c' && boot_device != 'd') {
36b486bb
FB
1003 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1004 exit(1);
1005 }
1006 break;
c45886db
FB
1007 case 13:
1008 fd_filename[0] = optarg;
1009 break;
1010 case 14:
1011 fd_filename[1] = optarg;
1012 break;
77fef8c1
FB
1013 case 15:
1014 code_copy_enabled = 0;
1015 break;
c4b1fcc0
FB
1016 case 16:
1017 nb_nics = atoi(optarg);
1018 if (nb_nics < 1 || nb_nics > MAX_NICS) {
1019 fprintf(stderr, "qemu: invalid number of network interfaces\n");
1020 exit(1);
1021 }
1022 break;
fc01f7e7
FB
1023 }
1024 break;
0824d6fc
FB
1025 case 'h':
1026 help();
1027 break;
1028 case 'm':
1ccde1cb
FB
1029 ram_size = atoi(optarg) * 1024 * 1024;
1030 if (ram_size <= 0)
0824d6fc 1031 help();
1ccde1cb 1032 if (ram_size > PHYS_RAM_MAX_SIZE) {
36b486bb 1033 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
7916e224
FB
1034 PHYS_RAM_MAX_SIZE / (1024 * 1024));
1035 exit(1);
1036 }
0824d6fc
FB
1037 break;
1038 case 'd':
f193c797
FB
1039 {
1040 int mask;
1041 CPULogItem *item;
1042
1043 mask = cpu_str_to_log_mask(optarg);
1044 if (!mask) {
1045 printf("Log items (comma separated):\n");
1046 for(item = cpu_log_items; item->mask != 0; item++) {
1047 printf("%-10s %s\n", item->name, item->help);
1048 }
1049 exit(1);
1050 }
1051 cpu_set_log(mask);
1052 }
0824d6fc 1053 break;
f1510b2c
FB
1054 case 'n':
1055 pstrcpy(network_script, sizeof(network_script), optarg);
1056 break;
b4608c04
FB
1057 case 's':
1058 use_gdbstub = 1;
1059 break;
1060 case 'p':
1061 gdbstub_port = atoi(optarg);
1062 break;
330d0414 1063 case 'L':
5a67135a 1064 bios_dir = optarg;
330d0414 1065 break;
0824d6fc
FB
1066 }
1067 }
330d0414 1068
a20dd508
FB
1069 if (optind < argc) {
1070 hd_filename[0] = argv[optind++];
1071 }
1072
1073 linux_boot = (kernel_filename != NULL);
330d0414 1074
c45886db
FB
1075 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1076 fd_filename[0] == '\0')
0824d6fc 1077 help();
8f2b1fb0
FB
1078
1079 /* boot to cd by default if no hard disk */
d0309311
FB
1080 if (hd_filename[0] == '\0' && boot_device == 'c') {
1081 if (fd_filename[0] != '\0')
1082 boot_device = 'a';
1083 else
1084 boot_device = 'd';
1085 }
0824d6fc 1086
dc887a4d
FB
1087#if !defined(CONFIG_SOFTMMU)
1088 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1089 {
1090 static uint8_t stdout_buf[4096];
1091 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1092 }
1093#else
b118d61e 1094 setvbuf(stdout, NULL, _IOLBF, 0);
dc887a4d 1095#endif
0824d6fc 1096
c4b1fcc0
FB
1097 /* init host network redirectors */
1098 net_init();
f1510b2c 1099
0824d6fc 1100 /* init the memory */
1ccde1cb 1101 phys_ram_size = ram_size + vga_ram_size;
7f7f9873
FB
1102
1103#ifdef CONFIG_SOFTMMU
1ccde1cb 1104 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
7f7f9873
FB
1105 if (!phys_ram_base) {
1106 fprintf(stderr, "Could not allocate physical memory\n");
0824d6fc
FB
1107 exit(1);
1108 }
7f7f9873
FB
1109#else
1110 /* as we must map the same page at several addresses, we must use
1111 a fd */
1112 {
1113 const char *tmpdir;
1114
1115 tmpdir = getenv("QEMU_TMPDIR");
1116 if (!tmpdir)
1117 tmpdir = "/tmp";
1118 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1119 if (mkstemp(phys_ram_file) < 0) {
1120 fprintf(stderr, "Could not create temporary memory file '%s'\n",
1121 phys_ram_file);
1122 exit(1);
1123 }
1124 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1125 if (phys_ram_fd < 0) {
1126 fprintf(stderr, "Could not open temporary memory file '%s'\n",
1127 phys_ram_file);
1128 exit(1);
1129 }
1ccde1cb 1130 ftruncate(phys_ram_fd, phys_ram_size);
7f7f9873 1131 unlink(phys_ram_file);
1ccde1cb
FB
1132 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
1133 phys_ram_size,
7f7f9873
FB
1134 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
1135 phys_ram_fd, 0);
1136 if (phys_ram_base == MAP_FAILED) {
1137 fprintf(stderr, "Could not map physical memory\n");
1138 exit(1);
1139 }
1140 }
1141#endif
0824d6fc 1142
c4b1fcc0
FB
1143 /* we always create the cdrom drive, even if no disk is there */
1144 if (has_cdrom) {
1145 bs_table[2] = bdrv_new("cdrom");
1146 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1147 }
1148
33e3963e
FB
1149 /* open the virtual block devices */
1150 for(i = 0; i < MAX_DISKS; i++) {
1151 if (hd_filename[i]) {
33e3963e 1152 if (!bs_table[i]) {
c4b1fcc0
FB
1153 char buf[64];
1154 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1155 bs_table[i] = bdrv_new(buf);
1156 }
1157 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
36b486bb 1158 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
33e3963e
FB
1159 hd_filename[i]);
1160 exit(1);
1161 }
c4b1fcc0
FB
1162 if (i == 0 && cyls != 0)
1163 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1164 }
1165 }
1166
1167 /* we always create at least one floppy disk */
1168 fd_table[0] = bdrv_new("fda");
1169 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1170
1171 for(i = 0; i < MAX_FD; i++) {
1172 if (fd_filename[i]) {
1173 if (!fd_table[i]) {
1174 char buf[64];
1175 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1176 fd_table[i] = bdrv_new(buf);
1177 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1178 }
1179 if (fd_filename[i] != '\0') {
1180 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1181 fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1182 fd_filename[i]);
1183 exit(1);
1184 }
1185 }
33e3963e
FB
1186 }
1187 }
1188
330d0414
FB
1189 /* init CPU state */
1190 env = cpu_init();
1191 global_env = env;
1192 cpu_single_env = env;
1193
1194 init_ioports();
80cabfad 1195 cpu_calibrate_ticks();
0824d6fc 1196
313aa567 1197 /* terminal init */
a20dd508 1198 if (nographic) {
313aa567
FB
1199 dumb_display_init(ds);
1200 } else {
1201#ifdef CONFIG_SDL
1202 sdl_display_init(ds);
313aa567
FB
1203#else
1204 dumb_display_init(ds);
1205#endif
1206 }
0824d6fc 1207
80cabfad
FB
1208#if defined(TARGET_I386)
1209 pc_init(ram_size, vga_ram_size, boot_device,
1210 ds, fd_filename, snapshot,
1211 kernel_filename, kernel_cmdline, initrd_filename);
1212#elif defined(TARGET_PPC)
1213 ppc_init();
c45886db 1214#endif
77fef8c1 1215
c4b1fcc0
FB
1216 /* launched after the device init so that it can display or not a
1217 banner */
1218 monitor_init();
1219
0824d6fc 1220 /* setup cpu signal handlers for MMU / self modifying code handling */
77fef8c1
FB
1221#if !defined(CONFIG_SOFTMMU)
1222
1223#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1224 {
1225 stack_t stk;
1226 signal_stack = malloc(SIGNAL_STACK_SIZE);
1227 stk.ss_sp = signal_stack;
1228 stk.ss_size = SIGNAL_STACK_SIZE;
1229 stk.ss_flags = 0;
1230
1231 if (sigaltstack(&stk, NULL) < 0) {
1232 perror("sigaltstack");
1233 exit(1);
1234 }
1235 }
1236#endif
1237
0824d6fc
FB
1238 sigfillset(&act.sa_mask);
1239 act.sa_flags = SA_SIGINFO;
77fef8c1
FB
1240#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1241 act.sa_flags |= SA_ONSTACK;
1242#endif
0824d6fc
FB
1243 act.sa_sigaction = host_segv_handler;
1244 sigaction(SIGSEGV, &act, NULL);
1245 sigaction(SIGBUS, &act, NULL);
77fef8c1
FB
1246#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1247 sigaction(SIGFPE, &act, NULL);
1248#endif
3a51dee6 1249#endif
0824d6fc 1250
77fef8c1
FB
1251 /* timer signal */
1252 sigfillset(&act.sa_mask);
1253 act.sa_flags = SA_SIGINFO;
1254#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1255 act.sa_flags |= SA_ONSTACK;
1256#endif
0824d6fc
FB
1257 act.sa_sigaction = host_alarm_handler;
1258 sigaction(SIGALRM, &act, NULL);
1259
0824d6fc 1260 itv.it_interval.tv_sec = 0;
87858c89 1261 itv.it_interval.tv_usec = 1000;
0824d6fc
FB
1262 itv.it_value.tv_sec = 0;
1263 itv.it_value.tv_usec = 10 * 1000;
1264 setitimer(ITIMER_REAL, &itv, NULL);
87858c89
FB
1265 /* we probe the tick duration of the kernel to inform the user if
1266 the emulated kernel requested a too high timer frequency */
1267 getitimer(ITIMER_REAL, &itv);
313aa567 1268 timer_ms = itv.it_interval.tv_usec / 1000;
87858c89
FB
1269 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1270 1000000;
7f7f9873 1271
b4608c04
FB
1272 if (use_gdbstub) {
1273 cpu_gdbstub(NULL, main_loop, gdbstub_port);
1274 } else {
1275 main_loop(NULL);
0824d6fc 1276 }
0824d6fc
FB
1277 return 0;
1278}