Statistics
| Branch: | Revision:

root / vl.c @ 67b915a5

History | View | Annotate | Download (52.9 kB)

1
/*
2
 * QEMU System Emulator
3
 * 
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "vl.h"
25

    
26
#include <getopt.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29
#include <signal.h>
30
#include <time.h>
31
#include <malloc.h>
32
#include <errno.h>
33
#include <sys/time.h>
34

    
35
#ifndef _WIN32
36
#include <sys/times.h>
37
#include <sys/wait.h>
38
#include <pty.h>
39
#include <termios.h>
40
#include <sys/poll.h>
41
#include <sys/mman.h>
42
#include <sys/ioctl.h>
43
#include <sys/socket.h>
44
#include <linux/if.h>
45
#include <linux/if_tun.h>
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

    
55

    
56
#include "disas.h"
57

    
58
#include "exec-all.h"
59

    
60
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
61

    
62
//#define DEBUG_UNUSED_IOPORT
63

    
64
#if !defined(CONFIG_SOFTMMU)
65
#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
66
#else
67
#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
68
#endif
69

    
70
/* in ms */
71
#define GUI_REFRESH_INTERVAL 30
72

    
73
/* XXX: use a two level table to limit memory usage */
74
#define MAX_IOPORTS 65536
75

    
76
const char *bios_dir = CONFIG_QEMU_SHAREDIR;
77
char phys_ram_file[1024];
78
CPUState *global_env;
79
CPUState *cpu_single_env;
80
void *ioport_opaque[MAX_IOPORTS];
81
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
82
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
83
BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
84
int vga_ram_size;
85
static DisplayState display_state;
86
int nographic;
87
int64_t ticks_per_sec;
88
int boot_device = 'c';
89
static int ram_size;
90
static char network_script[1024];
91
int pit_min_timer_count = 0;
92
int nb_nics;
93
NetDriverState nd_table[MAX_NICS];
94
SerialState *serial_console;
95
QEMUTimer *gui_timer;
96
int vm_running;
97

    
98
/***********************************************************/
99
/* x86 io ports */
100

    
101
uint32_t default_ioport_readb(void *opaque, uint32_t address)
102
{
103
#ifdef DEBUG_UNUSED_IOPORT
104
    fprintf(stderr, "inb: port=0x%04x\n", address);
105
#endif
106
    return 0xff;
107
}
108

    
109
void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
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 */
117
uint32_t default_ioport_readw(void *opaque, uint32_t address)
118
{
119
    uint32_t data;
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;
122
    return data;
123
}
124

    
125
void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
126
{
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);
129
}
130

    
131
uint32_t default_ioport_readl(void *opaque, uint32_t address)
132
{
133
#ifdef DEBUG_UNUSED_IOPORT
134
    fprintf(stderr, "inl: port=0x%04x\n", address);
135
#endif
136
    return 0xffffffff;
137
}
138

    
139
void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
140
{
141
#ifdef DEBUG_UNUSED_IOPORT
142
    fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
143
#endif
144
}
145

    
146
void init_ioports(void)
147
{
148
    int i;
149

    
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
    }
158
}
159

    
160
/* size is the word size in byte */
161
int register_ioport_read(int start, int length, int size, 
162
                         IOPortReadFunc *func, void *opaque)
163
{
164
    int i, bsize;
165

    
166
    if (size == 1) {
167
        bsize = 0;
168
    } else if (size == 2) {
169
        bsize = 1;
170
    } else if (size == 4) {
171
        bsize = 2;
172
    } else {
173
        hw_error("register_ioport_read: invalid size");
174
        return -1;
175
    }
176
    for(i = start; i < start + length; i += size) {
177
        ioport_read_table[bsize][i] = func;
178
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
179
            hw_error("register_ioport_read: invalid opaque");
180
        ioport_opaque[i] = opaque;
181
    }
182
    return 0;
183
}
184

    
185
/* size is the word size in byte */
186
int register_ioport_write(int start, int length, int size, 
187
                          IOPortWriteFunc *func, void *opaque)
188
{
189
    int i, bsize;
190

    
191
    if (size == 1) {
192
        bsize = 0;
193
    } else if (size == 2) {
194
        bsize = 1;
195
    } else if (size == 4) {
196
        bsize = 2;
197
    } else {
198
        hw_error("register_ioport_write: invalid size");
199
        return -1;
200
    }
201
    for(i = start; i < start + length; i += size) {
202
        ioport_write_table[bsize][i] = func;
203
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
204
            hw_error("register_ioport_read: invalid opaque");
205
        ioport_opaque[i] = opaque;
206
    }
207
    return 0;
208
}
209

    
210
void 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. */
228
char *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

    
237
/* return the size or -1 if error */
238
int load_image(const char *filename, uint8_t *addr)
239
{
240
    int fd, size;
241
    fd = open(filename, O_RDONLY);
242
    if (fd < 0)
243
        return -1;
244
    size = lseek(fd, 0, SEEK_END);
245
    lseek(fd, 0, SEEK_SET);
246
    if (read(fd, addr, size) != size) {
247
        close(fd);
248
        return -1;
249
    }
250
    close(fd);
251
    return size;
252
}
253

    
254
void cpu_outb(CPUState *env, int addr, int val)
255
{
256
    addr &= (MAX_IOPORTS - 1);
257
    ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
258
}
259

    
260
void cpu_outw(CPUState *env, int addr, int val)
261
{
262
    addr &= (MAX_IOPORTS - 1);
263
    ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
264
}
265

    
266
void cpu_outl(CPUState *env, int addr, int val)
267
{
268
    addr &= (MAX_IOPORTS - 1);
269
    ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
270
}
271

    
272
int cpu_inb(CPUState *env, int addr)
273
{
274
    addr &= (MAX_IOPORTS - 1);
275
    return ioport_read_table[0][addr](ioport_opaque[addr], addr);
276
}
277

    
278
int cpu_inw(CPUState *env, int addr)
279
{
280
    addr &= (MAX_IOPORTS - 1);
281
    return ioport_read_table[1][addr](ioport_opaque[addr], addr);
282
}
283

    
284
int cpu_inl(CPUState *env, int addr)
285
{
286
    addr &= (MAX_IOPORTS - 1);
287
    return ioport_read_table[2][addr](ioport_opaque[addr], addr);
288
}
289

    
290
/***********************************************************/
291
void 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);
301
#else
302
    cpu_dump_state(global_env, stderr, 0);
303
#endif
304
    va_end(ap);
305
    abort();
306
}
307

    
308
/***********************************************************/
309
/* timers */
310

    
311
#if defined(__powerpc__)
312

    
313
static inline uint32_t get_tbl(void) 
314
{
315
    uint32_t tbl;
316
    asm volatile("mftb %0" : "=r" (tbl));
317
    return tbl;
318
}
319

    
320
static inline uint32_t get_tbu(void) 
321
{
322
        uint32_t tbl;
323
        asm volatile("mftbu %0" : "=r" (tbl));
324
        return tbl;
325
}
326

    
327
int64_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

    
341
int64_t cpu_get_real_ticks(void)
342
{
343
    int64_t val;
344
    asm("rdtsc" : "=A" (val));
345
    return val;
346
}
347

    
348
#else
349
#error unsupported CPU
350
#endif
351

    
352
static int64_t cpu_ticks_offset;
353
static int cpu_ticks_enabled;
354

    
355
static inline int64_t cpu_get_ticks(void)
356
{
357
    if (!cpu_ticks_enabled) {
358
        return cpu_ticks_offset;
359
    } else {
360
        return cpu_get_real_ticks() + cpu_ticks_offset;
361
    }
362
}
363

    
364
/* enable cpu_get_ticks() */
365
void cpu_enable_ticks(void)
366
{
367
    if (!cpu_ticks_enabled) {
368
        cpu_ticks_offset -= cpu_get_real_ticks();
369
        cpu_ticks_enabled = 1;
370
    }
371
}
372

    
373
/* disable cpu_get_ticks() : the clock is stopped. You must not call
374
   cpu_get_ticks() after that.  */
375
void cpu_disable_ticks(void)
376
{
377
    if (cpu_ticks_enabled) {
378
        cpu_ticks_offset = cpu_get_ticks();
379
        cpu_ticks_enabled = 0;
380
    }
381
}
382

    
383
static int64_t get_clock(void)
384
{
385
#ifdef _WIN32
386
    struct _timeb tb;
387
    _ftime(&tb);
388
    return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
389
#else
390
    struct timeval tv;
391
    gettimeofday(&tv, NULL);
392
    return tv.tv_sec * 1000000LL + tv.tv_usec;
393
#endif
394
}
395

    
396
void cpu_calibrate_ticks(void)
397
{
398
    int64_t usec, ticks;
399

    
400
    usec = get_clock();
401
    ticks = cpu_get_real_ticks();
402
#ifdef _WIN32
403
    Sleep(50);
404
#else
405
    usleep(50 * 1000);
406
#endif
407
    usec = get_clock() - usec;
408
    ticks = cpu_get_real_ticks() - ticks;
409
    ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
410
}
411

    
412
/* compute with 96 bit intermediate result: (a*b)/c */
413
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
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

    
436
#define QEMU_TIMER_REALTIME 0
437
#define QEMU_TIMER_VIRTUAL  1
438

    
439
struct QEMUClock {
440
    int type;
441
    /* XXX: add frequency */
442
};
443

    
444
struct QEMUTimer {
445
    QEMUClock *clock;
446
    int64_t expire_time;
447
    QEMUTimerCB *cb;
448
    void *opaque;
449
    struct QEMUTimer *next;
450
};
451

    
452
QEMUClock *rt_clock;
453
QEMUClock *vm_clock;
454

    
455
static QEMUTimer *active_timers[2];
456
#ifndef _WIN32
457
/* frequency of the times() clock tick */
458
static int timer_freq;
459
#endif
460

    
461
QEMUClock *qemu_new_clock(int type)
462
{
463
    QEMUClock *clock;
464
    clock = qemu_mallocz(sizeof(QEMUClock));
465
    if (!clock)
466
        return NULL;
467
    clock->type = type;
468
    return clock;
469
}
470

    
471
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
472
{
473
    QEMUTimer *ts;
474

    
475
    ts = qemu_mallocz(sizeof(QEMUTimer));
476
    ts->clock = clock;
477
    ts->cb = cb;
478
    ts->opaque = opaque;
479
    return ts;
480
}
481

    
482
void qemu_free_timer(QEMUTimer *ts)
483
{
484
    qemu_free(ts);
485
}
486

    
487
/* stop a timer, but do not dealloc it */
488
void qemu_del_timer(QEMUTimer *ts)
489
{
490
    QEMUTimer **pt, *t;
491

    
492
    /* NOTE: this code must be signal safe because
493
       qemu_timer_expired() can be called from a signal. */
494
    pt = &active_timers[ts->clock->type];
495
    for(;;) {
496
        t = *pt;
497
        if (!t)
498
            break;
499
        if (t == ts) {
500
            *pt = t->next;
501
            break;
502
        }
503
        pt = &t->next;
504
    }
505
}
506

    
507
/* modify the current timer so that it will be fired when current_time
508
   >= expire_time. The corresponding callback will be called. */
509
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
510
{
511
    QEMUTimer **pt, *t;
512

    
513
    qemu_del_timer(ts);
514

    
515
    /* add the timer in the sorted list */
516
    /* NOTE: this code must be signal safe because
517
       qemu_timer_expired() can be called from a signal. */
518
    pt = &active_timers[ts->clock->type];
519
    for(;;) {
520
        t = *pt;
521
        if (!t)
522
            break;
523
        if (t->expire_time > expire_time) 
524
            break;
525
        pt = &t->next;
526
    }
527
    ts->expire_time = expire_time;
528
    ts->next = *pt;
529
    *pt = ts;
530
}
531

    
532
int qemu_timer_pending(QEMUTimer *ts)
533
{
534
    QEMUTimer *t;
535
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
536
        if (t == ts)
537
            return 1;
538
    }
539
    return 0;
540
}
541

    
542
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
543
{
544
    if (!timer_head)
545
        return 0;
546
    return (timer_head->expire_time <= current_time);
547
}
548

    
549
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
550
{
551
    QEMUTimer *ts;
552
    
553
    for(;;) {
554
        ts = *ptimer_head;
555
        if (ts->expire_time > current_time)
556
            break;
557
        /* remove timer from the list before calling the callback */
558
        *ptimer_head = ts->next;
559
        ts->next = NULL;
560
        
561
        /* run the callback (the timer list can be modified) */
562
        ts->cb(ts->opaque);
563
    }
564
}
565

    
566
int64_t qemu_get_clock(QEMUClock *clock)
567
{
568
    switch(clock->type) {
569
    case QEMU_TIMER_REALTIME:
570
#ifdef _WIN32
571
        return GetTickCount();
572
#else
573
        /* XXX: portability among Linux hosts */
574
        if (timer_freq == 100) {
575
            return times(NULL) * 10;
576
        } else {
577
            return ((int64_t)times(NULL) * 1000) / timer_freq;
578
        }
579
#endif
580
    default:
581
    case QEMU_TIMER_VIRTUAL:
582
        return cpu_get_ticks();
583
    }
584
}
585

    
586
/* save a timer */
587
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
588
{
589
    uint64_t expire_time;
590

    
591
    if (qemu_timer_pending(ts)) {
592
        expire_time = ts->expire_time;
593
    } else {
594
        expire_time = -1;
595
    }
596
    qemu_put_be64(f, expire_time);
597
}
598

    
599
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
600
{
601
    uint64_t expire_time;
602

    
603
    expire_time = qemu_get_be64(f);
604
    if (expire_time != -1) {
605
        qemu_mod_timer(ts, expire_time);
606
    } else {
607
        qemu_del_timer(ts);
608
    }
609
}
610

    
611
static void timer_save(QEMUFile *f, void *opaque)
612
{
613
    if (cpu_ticks_enabled) {
614
        hw_error("cannot save state if virtual timers are running");
615
    }
616
    qemu_put_be64s(f, &cpu_ticks_offset);
617
    qemu_put_be64s(f, &ticks_per_sec);
618
}
619

    
620
static int timer_load(QEMUFile *f, void *opaque, int version_id)
621
{
622
    if (version_id != 1)
623
        return -EINVAL;
624
    if (cpu_ticks_enabled) {
625
        return -EINVAL;
626
    }
627
    qemu_get_be64s(f, &cpu_ticks_offset);
628
    qemu_get_be64s(f, &ticks_per_sec);
629
    return 0;
630
}
631

    
632
#ifdef _WIN32
633
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, 
634
                                 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
635
#else
636
static void host_alarm_handler(int host_signum)
637
#endif
638
{
639
    if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
640
                           qemu_get_clock(vm_clock)) ||
641
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
642
                           qemu_get_clock(rt_clock))) {
643
        /* stop the cpu because a timer occured */
644
        cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
645
    }
646
}
647

    
648
static void init_timers(void)
649
{
650
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
651
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
652

    
653
#ifdef _WIN32
654
    {
655
        int count=0;
656
        MMRESULT timerID = timeSetEvent(10,    // interval (ms)
657
                                        0,     // resolution
658
                                        host_alarm_handler, // function
659
                                        (DWORD)&count,  // user parameter
660
                                        TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
661
         if( !timerID ) {
662
            perror("failed timer alarm");
663
            exit(1);
664
         }
665
    }
666
    pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
667
#else
668
    {
669
        struct sigaction act;
670
        struct itimerval itv;
671
        
672
        /* get times() syscall frequency */
673
        timer_freq = sysconf(_SC_CLK_TCK);
674
        
675
        /* timer signal */
676
        sigfillset(&act.sa_mask);
677
        act.sa_flags = 0;
678
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
679
        act.sa_flags |= SA_ONSTACK;
680
#endif
681
        act.sa_handler = host_alarm_handler;
682
        sigaction(SIGALRM, &act, NULL);
683
        
684
        itv.it_interval.tv_sec = 0;
685
        itv.it_interval.tv_usec = 1000;
686
        itv.it_value.tv_sec = 0;
687
        itv.it_value.tv_usec = 10 * 1000;
688
        setitimer(ITIMER_REAL, &itv, NULL);
689
        /* we probe the tick duration of the kernel to inform the user if
690
           the emulated kernel requested a too high timer frequency */
691
        getitimer(ITIMER_REAL, &itv);
692
        pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) / 
693
            1000000;
694
    }
695
#endif
696
}
697

    
698
/***********************************************************/
699
/* serial device */
700

    
701
#ifdef _WIN32
702

    
703
int serial_open_device(void)
704
{
705
    return -1;
706
}
707

    
708
#else
709

    
710
int serial_open_device(void)
711
{
712
    char slave_name[1024];
713
    int master_fd, slave_fd;
714

    
715
    if (serial_console == NULL && nographic) {
716
        /* use console for serial port */
717
        return 0;
718
    } else {
719
        if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
720
            fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
721
            return -1;
722
        }
723
        fprintf(stderr, "Serial port redirected to %s\n", slave_name);
724
        return master_fd;
725
    }
726
}
727

    
728
#endif
729

    
730
/***********************************************************/
731
/* Linux network device redirector */
732

    
733
#ifdef _WIN32
734

    
735
static int net_init(void)
736
{
737
    return 0;
738
}
739

    
740
void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
741
{
742
}
743

    
744
#else
745

    
746
static int tun_open(char *ifname, int ifname_size)
747
{
748
    struct ifreq ifr;
749
    int fd, ret;
750
    
751
    fd = open("/dev/net/tun", O_RDWR);
752
    if (fd < 0) {
753
        fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
754
        return -1;
755
    }
756
    memset(&ifr, 0, sizeof(ifr));
757
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
758
    pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
759
    ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
760
    if (ret != 0) {
761
        fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
762
        close(fd);
763
        return -1;
764
    }
765
    printf("Connected to host network interface: %s\n", ifr.ifr_name);
766
    pstrcpy(ifname, ifname_size, ifr.ifr_name);
767
    fcntl(fd, F_SETFL, O_NONBLOCK);
768
    return fd;
769
}
770

    
771
static int net_init(void)
772
{
773
    int pid, status, launch_script, i;
774
    NetDriverState *nd;
775
    char *args[MAX_NICS + 2];
776
    char **parg;
777

    
778
    launch_script = 0;
779
    for(i = 0; i < nb_nics; i++) {
780
        nd = &nd_table[i];
781
        if (nd->fd < 0) {
782
            nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
783
            if (nd->fd >= 0) 
784
                launch_script = 1;
785
        }
786
    }
787

    
788
    if (launch_script) {
789
        /* try to launch network init script */
790
        pid = fork();
791
        if (pid >= 0) {
792
            if (pid == 0) {
793
                parg = args;
794
                *parg++ = network_script;
795
                for(i = 0; i < nb_nics; i++) {
796
                    nd = &nd_table[i];
797
                    if (nd->fd >= 0) {
798
                        *parg++ = nd->ifname;
799
                    }
800
                }
801
                *parg++ = NULL;
802
                execv(network_script, args);
803
                exit(1);
804
            }
805
            while (waitpid(pid, &status, 0) != pid);
806
            if (!WIFEXITED(status) ||
807
                WEXITSTATUS(status) != 0) {
808
                fprintf(stderr, "%s: could not launch network script\n",
809
                        network_script);
810
            }
811
        }
812
    }
813
    return 0;
814
}
815

    
816
void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
817
{
818
#ifdef DEBUG_NE2000
819
    printf("NE2000: sending packet size=%d\n", size);
820
#endif
821
    write(nd->fd, buf, size);
822
}
823

    
824
#endif
825

    
826
/***********************************************************/
827
/* dumb display */
828

    
829
#ifdef _WIN32
830

    
831
static void term_exit(void)
832
{
833
}
834

    
835
static void term_init(void)
836
{
837
}
838

    
839
#else
840

    
841
/* init terminal so that we can grab keys */
842
static struct termios oldtty;
843

    
844
static void term_exit(void)
845
{
846
    tcsetattr (0, TCSANOW, &oldtty);
847
}
848

    
849
static void term_init(void)
850
{
851
    struct termios tty;
852

    
853
    tcgetattr (0, &tty);
854
    oldtty = tty;
855

    
856
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
857
                          |INLCR|IGNCR|ICRNL|IXON);
858
    tty.c_oflag |= OPOST;
859
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
860
    /* if graphical mode, we allow Ctrl-C handling */
861
    if (nographic)
862
        tty.c_lflag &= ~ISIG;
863
    tty.c_cflag &= ~(CSIZE|PARENB);
864
    tty.c_cflag |= CS8;
865
    tty.c_cc[VMIN] = 1;
866
    tty.c_cc[VTIME] = 0;
867
    
868
    tcsetattr (0, TCSANOW, &tty);
869

    
870
    atexit(term_exit);
871

    
872
    fcntl(0, F_SETFL, O_NONBLOCK);
873
}
874

    
875
#endif
876

    
877
static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
878
{
879
}
880

    
881
static void dumb_resize(DisplayState *ds, int w, int h)
882
{
883
}
884

    
885
static void dumb_refresh(DisplayState *ds)
886
{
887
    vga_update_display();
888
}
889

    
890
void dumb_display_init(DisplayState *ds)
891
{
892
    ds->data = NULL;
893
    ds->linesize = 0;
894
    ds->depth = 0;
895
    ds->dpy_update = dumb_update;
896
    ds->dpy_resize = dumb_resize;
897
    ds->dpy_refresh = dumb_refresh;
898
}
899

    
900
#if !defined(CONFIG_SOFTMMU)
901
/***********************************************************/
902
/* cpu signal handler */
903
static void host_segv_handler(int host_signum, siginfo_t *info, 
904
                              void *puc)
905
{
906
    if (cpu_signal_handler(host_signum, info, puc))
907
        return;
908
    term_exit();
909
    abort();
910
}
911
#endif
912

    
913
/***********************************************************/
914
/* I/O handling */
915

    
916
#define MAX_IO_HANDLERS 64
917

    
918
typedef struct IOHandlerRecord {
919
    int fd;
920
    IOCanRWHandler *fd_can_read;
921
    IOReadHandler *fd_read;
922
    void *opaque;
923
    /* temporary data */
924
    struct pollfd *ufd;
925
    int max_size;
926
    struct IOHandlerRecord *next;
927
} IOHandlerRecord;
928

    
929
static IOHandlerRecord *first_io_handler;
930

    
931
int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, 
932
                             IOReadHandler *fd_read, void *opaque)
933
{
934
    IOHandlerRecord *ioh;
935

    
936
    ioh = qemu_mallocz(sizeof(IOHandlerRecord));
937
    if (!ioh)
938
        return -1;
939
    ioh->fd = fd;
940
    ioh->fd_can_read = fd_can_read;
941
    ioh->fd_read = fd_read;
942
    ioh->opaque = opaque;
943
    ioh->next = first_io_handler;
944
    first_io_handler = ioh;
945
    return 0;
946
}
947

    
948
void qemu_del_fd_read_handler(int fd)
949
{
950
    IOHandlerRecord **pioh, *ioh;
951

    
952
    pioh = &first_io_handler;
953
    for(;;) {
954
        ioh = *pioh;
955
        if (ioh == NULL)
956
            break;
957
        if (ioh->fd == fd) {
958
            *pioh = ioh->next;
959
            break;
960
        }
961
        pioh = &ioh->next;
962
    }
963
}
964

    
965
/***********************************************************/
966
/* savevm/loadvm support */
967

    
968
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
969
{
970
    fwrite(buf, 1, size, f);
971
}
972

    
973
void qemu_put_byte(QEMUFile *f, int v)
974
{
975
    fputc(v, f);
976
}
977

    
978
void qemu_put_be16(QEMUFile *f, unsigned int v)
979
{
980
    qemu_put_byte(f, v >> 8);
981
    qemu_put_byte(f, v);
982
}
983

    
984
void qemu_put_be32(QEMUFile *f, unsigned int v)
985
{
986
    qemu_put_byte(f, v >> 24);
987
    qemu_put_byte(f, v >> 16);
988
    qemu_put_byte(f, v >> 8);
989
    qemu_put_byte(f, v);
990
}
991

    
992
void qemu_put_be64(QEMUFile *f, uint64_t v)
993
{
994
    qemu_put_be32(f, v >> 32);
995
    qemu_put_be32(f, v);
996
}
997

    
998
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
999
{
1000
    return fread(buf, 1, size, f);
1001
}
1002

    
1003
int qemu_get_byte(QEMUFile *f)
1004
{
1005
    int v;
1006
    v = fgetc(f);
1007
    if (v == EOF)
1008
        return 0;
1009
    else
1010
        return v;
1011
}
1012

    
1013
unsigned int qemu_get_be16(QEMUFile *f)
1014
{
1015
    unsigned int v;
1016
    v = qemu_get_byte(f) << 8;
1017
    v |= qemu_get_byte(f);
1018
    return v;
1019
}
1020

    
1021
unsigned int qemu_get_be32(QEMUFile *f)
1022
{
1023
    unsigned int v;
1024
    v = qemu_get_byte(f) << 24;
1025
    v |= qemu_get_byte(f) << 16;
1026
    v |= qemu_get_byte(f) << 8;
1027
    v |= qemu_get_byte(f);
1028
    return v;
1029
}
1030

    
1031
uint64_t qemu_get_be64(QEMUFile *f)
1032
{
1033
    uint64_t v;
1034
    v = (uint64_t)qemu_get_be32(f) << 32;
1035
    v |= qemu_get_be32(f);
1036
    return v;
1037
}
1038

    
1039
int64_t qemu_ftell(QEMUFile *f)
1040
{
1041
    return ftell(f);
1042
}
1043

    
1044
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1045
{
1046
    if (fseek(f, pos, whence) < 0)
1047
        return -1;
1048
    return ftell(f);
1049
}
1050

    
1051
typedef struct SaveStateEntry {
1052
    char idstr[256];
1053
    int instance_id;
1054
    int version_id;
1055
    SaveStateHandler *save_state;
1056
    LoadStateHandler *load_state;
1057
    void *opaque;
1058
    struct SaveStateEntry *next;
1059
} SaveStateEntry;
1060

    
1061
static SaveStateEntry *first_se;
1062

    
1063
int register_savevm(const char *idstr, 
1064
                    int instance_id, 
1065
                    int version_id,
1066
                    SaveStateHandler *save_state,
1067
                    LoadStateHandler *load_state,
1068
                    void *opaque)
1069
{
1070
    SaveStateEntry *se, **pse;
1071

    
1072
    se = qemu_malloc(sizeof(SaveStateEntry));
1073
    if (!se)
1074
        return -1;
1075
    pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1076
    se->instance_id = instance_id;
1077
    se->version_id = version_id;
1078
    se->save_state = save_state;
1079
    se->load_state = load_state;
1080
    se->opaque = opaque;
1081
    se->next = NULL;
1082

    
1083
    /* add at the end of list */
1084
    pse = &first_se;
1085
    while (*pse != NULL)
1086
        pse = &(*pse)->next;
1087
    *pse = se;
1088
    return 0;
1089
}
1090

    
1091
#define QEMU_VM_FILE_MAGIC   0x5145564d
1092
#define QEMU_VM_FILE_VERSION 0x00000001
1093

    
1094
int qemu_savevm(const char *filename)
1095
{
1096
    SaveStateEntry *se;
1097
    QEMUFile *f;
1098
    int len, len_pos, cur_pos, saved_vm_running, ret;
1099

    
1100
    saved_vm_running = vm_running;
1101
    vm_stop(0);
1102

    
1103
    f = fopen(filename, "wb");
1104
    if (!f) {
1105
        ret = -1;
1106
        goto the_end;
1107
    }
1108

    
1109
    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1110
    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1111

    
1112
    for(se = first_se; se != NULL; se = se->next) {
1113
        /* ID string */
1114
        len = strlen(se->idstr);
1115
        qemu_put_byte(f, len);
1116
        qemu_put_buffer(f, se->idstr, len);
1117

    
1118
        qemu_put_be32(f, se->instance_id);
1119
        qemu_put_be32(f, se->version_id);
1120

    
1121
        /* record size: filled later */
1122
        len_pos = ftell(f);
1123
        qemu_put_be32(f, 0);
1124
        
1125
        se->save_state(f, se->opaque);
1126

    
1127
        /* fill record size */
1128
        cur_pos = ftell(f);
1129
        len = ftell(f) - len_pos - 4;
1130
        fseek(f, len_pos, SEEK_SET);
1131
        qemu_put_be32(f, len);
1132
        fseek(f, cur_pos, SEEK_SET);
1133
    }
1134

    
1135
    fclose(f);
1136
    ret = 0;
1137
 the_end:
1138
    if (saved_vm_running)
1139
        vm_start();
1140
    return ret;
1141
}
1142

    
1143
static SaveStateEntry *find_se(const char *idstr, int instance_id)
1144
{
1145
    SaveStateEntry *se;
1146

    
1147
    for(se = first_se; se != NULL; se = se->next) {
1148
        if (!strcmp(se->idstr, idstr) && 
1149
            instance_id == se->instance_id)
1150
            return se;
1151
    }
1152
    return NULL;
1153
}
1154

    
1155
int qemu_loadvm(const char *filename)
1156
{
1157
    SaveStateEntry *se;
1158
    QEMUFile *f;
1159
    int len, cur_pos, ret, instance_id, record_len, version_id;
1160
    int saved_vm_running;
1161
    unsigned int v;
1162
    char idstr[256];
1163
    
1164
    saved_vm_running = vm_running;
1165
    vm_stop(0);
1166

    
1167
    f = fopen(filename, "rb");
1168
    if (!f) {
1169
        ret = -1;
1170
        goto the_end;
1171
    }
1172

    
1173
    v = qemu_get_be32(f);
1174
    if (v != QEMU_VM_FILE_MAGIC)
1175
        goto fail;
1176
    v = qemu_get_be32(f);
1177
    if (v != QEMU_VM_FILE_VERSION) {
1178
    fail:
1179
        fclose(f);
1180
        ret = -1;
1181
        goto the_end;
1182
    }
1183
    for(;;) {
1184
        len = qemu_get_byte(f);
1185
        if (feof(f))
1186
            break;
1187
        qemu_get_buffer(f, idstr, len);
1188
        idstr[len] = '\0';
1189
        instance_id = qemu_get_be32(f);
1190
        version_id = qemu_get_be32(f);
1191
        record_len = qemu_get_be32(f);
1192
#if 0
1193
        printf("idstr=%s instance=0x%x version=%d len=%d\n", 
1194
               idstr, instance_id, version_id, record_len);
1195
#endif
1196
        cur_pos = ftell(f);
1197
        se = find_se(idstr, instance_id);
1198
        if (!se) {
1199
            fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", 
1200
                    instance_id, idstr);
1201
        } else {
1202
            ret = se->load_state(f, se->opaque, version_id);
1203
            if (ret < 0) {
1204
                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", 
1205
                        instance_id, idstr);
1206
            }
1207
        }
1208
        /* always seek to exact end of record */
1209
        qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1210
    }
1211
    fclose(f);
1212
    ret = 0;
1213
 the_end:
1214
    if (saved_vm_running)
1215
        vm_start();
1216
    return ret;
1217
}
1218

    
1219
/***********************************************************/
1220
/* cpu save/restore */
1221

    
1222
#if defined(TARGET_I386)
1223

    
1224
static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1225
{
1226
    qemu_put_be32(f, (uint32_t)dt->base);
1227
    qemu_put_be32(f, dt->limit);
1228
    qemu_put_be32(f, dt->flags);
1229
}
1230

    
1231
static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1232
{
1233
    dt->base = (uint8_t *)qemu_get_be32(f);
1234
    dt->limit = qemu_get_be32(f);
1235
    dt->flags = qemu_get_be32(f);
1236
}
1237

    
1238
void cpu_save(QEMUFile *f, void *opaque)
1239
{
1240
    CPUState *env = opaque;
1241
    uint16_t fptag, fpus, fpuc;
1242
    uint32_t hflags;
1243
    int i;
1244

    
1245
    for(i = 0; i < 8; i++)
1246
        qemu_put_be32s(f, &env->regs[i]);
1247
    qemu_put_be32s(f, &env->eip);
1248
    qemu_put_be32s(f, &env->eflags);
1249
    qemu_put_be32s(f, &env->eflags);
1250
    hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1251
    qemu_put_be32s(f, &hflags);
1252
    
1253
    /* FPU */
1254
    fpuc = env->fpuc;
1255
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1256
    fptag = 0;
1257
    for (i=7; i>=0; i--) {
1258
        fptag <<= 2;
1259
        if (env->fptags[i]) {
1260
            fptag |= 3;
1261
        }
1262
    }
1263
    
1264
    qemu_put_be16s(f, &fpuc);
1265
    qemu_put_be16s(f, &fpus);
1266
    qemu_put_be16s(f, &fptag);
1267

    
1268
    for(i = 0; i < 8; i++) {
1269
        uint64_t mant;
1270
        uint16_t exp;
1271
        cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1272
        qemu_put_be64(f, mant);
1273
        qemu_put_be16(f, exp);
1274
    }
1275

    
1276
    for(i = 0; i < 6; i++)
1277
        cpu_put_seg(f, &env->segs[i]);
1278
    cpu_put_seg(f, &env->ldt);
1279
    cpu_put_seg(f, &env->tr);
1280
    cpu_put_seg(f, &env->gdt);
1281
    cpu_put_seg(f, &env->idt);
1282
    
1283
    qemu_put_be32s(f, &env->sysenter_cs);
1284
    qemu_put_be32s(f, &env->sysenter_esp);
1285
    qemu_put_be32s(f, &env->sysenter_eip);
1286
    
1287
    qemu_put_be32s(f, &env->cr[0]);
1288
    qemu_put_be32s(f, &env->cr[2]);
1289
    qemu_put_be32s(f, &env->cr[3]);
1290
    qemu_put_be32s(f, &env->cr[4]);
1291
    
1292
    for(i = 0; i < 8; i++)
1293
        qemu_put_be32s(f, &env->dr[i]);
1294

    
1295
    /* MMU */
1296
    qemu_put_be32s(f, &env->a20_mask);
1297
}
1298

    
1299
int cpu_load(QEMUFile *f, void *opaque, int version_id)
1300
{
1301
    CPUState *env = opaque;
1302
    int i;
1303
    uint32_t hflags;
1304
    uint16_t fpus, fpuc, fptag;
1305

    
1306
    if (version_id != 1)
1307
        return -EINVAL;
1308
    for(i = 0; i < 8; i++)
1309
        qemu_get_be32s(f, &env->regs[i]);
1310
    qemu_get_be32s(f, &env->eip);
1311
    qemu_get_be32s(f, &env->eflags);
1312
    qemu_get_be32s(f, &env->eflags);
1313
    qemu_get_be32s(f, &hflags);
1314

    
1315
    qemu_get_be16s(f, &fpuc);
1316
    qemu_get_be16s(f, &fpus);
1317
    qemu_get_be16s(f, &fptag);
1318

    
1319
    for(i = 0; i < 8; i++) {
1320
        uint64_t mant;
1321
        uint16_t exp;
1322
        mant = qemu_get_be64(f);
1323
        exp = qemu_get_be16(f);
1324
        env->fpregs[i] = cpu_set_fp80(mant, exp);
1325
    }
1326

    
1327
    env->fpuc = fpuc;
1328
    env->fpstt = (fpus >> 11) & 7;
1329
    env->fpus = fpus & ~0x3800;
1330
    for(i = 0; i < 8; i++) {
1331
        env->fptags[i] = ((fptag & 3) == 3);
1332
        fptag >>= 2;
1333
    }
1334
    
1335
    for(i = 0; i < 6; i++)
1336
        cpu_get_seg(f, &env->segs[i]);
1337
    cpu_get_seg(f, &env->ldt);
1338
    cpu_get_seg(f, &env->tr);
1339
    cpu_get_seg(f, &env->gdt);
1340
    cpu_get_seg(f, &env->idt);
1341
    
1342
    qemu_get_be32s(f, &env->sysenter_cs);
1343
    qemu_get_be32s(f, &env->sysenter_esp);
1344
    qemu_get_be32s(f, &env->sysenter_eip);
1345
    
1346
    qemu_get_be32s(f, &env->cr[0]);
1347
    qemu_get_be32s(f, &env->cr[2]);
1348
    qemu_get_be32s(f, &env->cr[3]);
1349
    qemu_get_be32s(f, &env->cr[4]);
1350
    
1351
    for(i = 0; i < 8; i++)
1352
        qemu_get_be32s(f, &env->dr[i]);
1353

    
1354
    /* MMU */
1355
    qemu_get_be32s(f, &env->a20_mask);
1356

    
1357
    /* XXX: compute hflags from scratch, except for CPL and IIF */
1358
    env->hflags = hflags;
1359
    tlb_flush(env, 1);
1360
    return 0;
1361
}
1362

    
1363
#else
1364

    
1365
#warning No CPU save/restore functions
1366

    
1367
#endif
1368

    
1369
/***********************************************************/
1370
/* ram save/restore */
1371

    
1372
/* we just avoid storing empty pages */
1373
static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1374
{
1375
    int i, v;
1376

    
1377
    v = buf[0];
1378
    for(i = 1; i < len; i++) {
1379
        if (buf[i] != v)
1380
            goto normal_save;
1381
    }
1382
    qemu_put_byte(f, 1);
1383
    qemu_put_byte(f, v);
1384
    return;
1385
 normal_save:
1386
    qemu_put_byte(f, 0); 
1387
    qemu_put_buffer(f, buf, len);
1388
}
1389

    
1390
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1391
{
1392
    int v;
1393

    
1394
    v = qemu_get_byte(f);
1395
    switch(v) {
1396
    case 0:
1397
        if (qemu_get_buffer(f, buf, len) != len)
1398
            return -EIO;
1399
        break;
1400
    case 1:
1401
        v = qemu_get_byte(f);
1402
        memset(buf, v, len);
1403
        break;
1404
    default:
1405
        return -EINVAL;
1406
    }
1407
    return 0;
1408
}
1409

    
1410
static void ram_save(QEMUFile *f, void *opaque)
1411
{
1412
    int i;
1413
    qemu_put_be32(f, phys_ram_size);
1414
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1415
        ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1416
    }
1417
}
1418

    
1419
static int ram_load(QEMUFile *f, void *opaque, int version_id)
1420
{
1421
    int i, ret;
1422

    
1423
    if (version_id != 1)
1424
        return -EINVAL;
1425
    if (qemu_get_be32(f) != phys_ram_size)
1426
        return -EINVAL;
1427
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1428
        ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1429
        if (ret)
1430
            return ret;
1431
    }
1432
    return 0;
1433
}
1434

    
1435
/***********************************************************/
1436
/* main execution loop */
1437

    
1438
void gui_update(void *opaque)
1439
{
1440
    display_state.dpy_refresh(&display_state);
1441
    qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1442
}
1443

    
1444
/* XXX: support several handlers */
1445
VMStopHandler *vm_stop_cb;
1446
VMStopHandler *vm_stop_opaque;
1447

    
1448
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1449
{
1450
    vm_stop_cb = cb;
1451
    vm_stop_opaque = opaque;
1452
    return 0;
1453
}
1454

    
1455
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1456
{
1457
    vm_stop_cb = NULL;
1458
}
1459

    
1460
void vm_start(void)
1461
{
1462
    if (!vm_running) {
1463
        cpu_enable_ticks();
1464
        vm_running = 1;
1465
    }
1466
}
1467

    
1468
void vm_stop(int reason) 
1469
{
1470
    if (vm_running) {
1471
        cpu_disable_ticks();
1472
        vm_running = 0;
1473
        if (reason != 0) {
1474
            if (vm_stop_cb) {
1475
                vm_stop_cb(vm_stop_opaque, reason);
1476
            }
1477
        }
1478
    }
1479
}
1480

    
1481
int main_loop(void)
1482
{
1483
#ifndef _WIN32
1484
    struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1485
    IOHandlerRecord *ioh, *ioh_next;
1486
    uint8_t buf[4096];
1487
    int n, max_size;
1488
#endif
1489
    int ret, timeout;
1490
    CPUState *env = global_env;
1491

    
1492
    for(;;) {
1493
        if (vm_running) {
1494
            ret = cpu_exec(env);
1495
            if (reset_requested) {
1496
                ret = EXCP_INTERRUPT; 
1497
                break;
1498
            }
1499
            if (ret == EXCP_DEBUG) {
1500
                vm_stop(EXCP_DEBUG);
1501
            }
1502
            /* if hlt instruction, we wait until the next IRQ */
1503
            /* XXX: use timeout computed from timers */
1504
            if (ret == EXCP_HLT) 
1505
                timeout = 10;
1506
            else
1507
                timeout = 0;
1508
        } else {
1509
            timeout = 10;
1510
        }
1511

    
1512
#ifndef _WIN32
1513
        /* poll any events */
1514
        /* XXX: separate device handlers from system ones */
1515
        pf = ufds;
1516
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1517
            if (!ioh->fd_can_read) {
1518
                max_size = 0;
1519
                pf->fd = ioh->fd;
1520
                pf->events = POLLIN;
1521
                ioh->ufd = pf;
1522
                pf++;
1523
            } else {
1524
                max_size = ioh->fd_can_read(ioh->opaque);
1525
                if (max_size > 0) {
1526
                    if (max_size > sizeof(buf))
1527
                        max_size = sizeof(buf);
1528
                    pf->fd = ioh->fd;
1529
                    pf->events = POLLIN;
1530
                    ioh->ufd = pf;
1531
                    pf++;
1532
                } else {
1533
                    ioh->ufd = NULL;
1534
                }
1535
            }
1536
            ioh->max_size = max_size;
1537
        }
1538

    
1539
        ret = poll(ufds, pf - ufds, timeout);
1540
        if (ret > 0) {
1541
            /* XXX: better handling of removal */
1542
            for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1543
                ioh_next = ioh->next;
1544
                pf = ioh->ufd;
1545
                if (pf) {
1546
                    if (pf->revents & POLLIN) {
1547
                        if (ioh->max_size == 0) {
1548
                            /* just a read event */
1549
                            ioh->fd_read(ioh->opaque, NULL, 0);
1550
                        } else {
1551
                            n = read(ioh->fd, buf, ioh->max_size);
1552
                            if (n >= 0) {
1553
                                ioh->fd_read(ioh->opaque, buf, n);
1554
                            } else if (errno != -EAGAIN) {
1555
                                ioh->fd_read(ioh->opaque, NULL, -errno);
1556
                            }
1557
                        }
1558
                    }
1559
                }
1560
            }
1561
        }
1562
#endif
1563

    
1564
        if (vm_running) {
1565
            qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], 
1566
                            qemu_get_clock(vm_clock));
1567
            
1568
            /* XXX: add explicit timer */
1569
            SB16_run();
1570
            
1571
            /* run dma transfers, if any */
1572
            DMA_run();
1573
        }
1574

    
1575
        /* real time timers */
1576
        qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], 
1577
                        qemu_get_clock(rt_clock));
1578
    }
1579
    cpu_disable_ticks();
1580
    return ret;
1581
}
1582

    
1583
void help(void)
1584
{
1585
    printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
1586
           "usage: %s [options] [disk_image]\n"
1587
           "\n"
1588
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1589
           "\n"
1590
           "Standard options:\n"
1591
           "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
1592
           "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
1593
           "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
1594
           "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1595
           "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1596
           "-snapshot       write to temporary files instead of disk image files\n"
1597
           "-m megs         set virtual RAM size to megs MB\n"
1598
           "-nographic      disable graphical output and redirect serial I/Os to console\n"
1599
           "\n"
1600
           "Network options:\n"
1601
           "-n script       set network init script [default=%s]\n"
1602
           "-nics n         simulate 'n' network interfaces [default=1]\n"
1603
           "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
1604
           "\n"
1605
           "Linux boot specific:\n"
1606
           "-kernel bzImage use 'bzImage' as kernel image\n"
1607
           "-append cmdline use 'cmdline' as kernel command line\n"
1608
           "-initrd file    use 'file' as initial ram disk\n"
1609
           "\n"
1610
           "Debug/Expert options:\n"
1611
           "-s              wait gdb connection to port %d\n"
1612
           "-p port         change gdb connection port\n"
1613
           "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
1614
           "-hdachs c,h,s   force hard disk 0 geometry (usually qemu can guess it)\n"
1615
           "-L path         set the directory for the BIOS and VGA BIOS\n"
1616
#ifdef USE_CODE_COPY
1617
           "-no-code-copy   disable code copy acceleration\n"
1618
#endif
1619

    
1620
           "\n"
1621
           "During emulation, use C-a h to get terminal commands:\n",
1622
#ifdef CONFIG_SOFTMMU
1623
           "qemu",
1624
#else
1625
           "qemu-fast",
1626
#endif
1627
           DEFAULT_NETWORK_SCRIPT, 
1628
           DEFAULT_GDBSTUB_PORT,
1629
           "/tmp/qemu.log");
1630
    term_print_help();
1631
#ifndef CONFIG_SOFTMMU
1632
    printf("\n"
1633
           "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1634
           "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1635
           "PC emulation.\n");
1636
#endif
1637
    exit(1);
1638
}
1639

    
1640
struct option long_options[] = {
1641
    { "initrd", 1, NULL, 0, },
1642
    { "hda", 1, NULL, 0, },
1643
    { "hdb", 1, NULL, 0, },
1644
    { "snapshot", 0, NULL, 0, },
1645
    { "hdachs", 1, NULL, 0, },
1646
    { "nographic", 0, NULL, 0, },
1647
    { "kernel", 1, NULL, 0, },
1648
    { "append", 1, NULL, 0, },
1649
    { "tun-fd", 1, NULL, 0, },
1650
    { "hdc", 1, NULL, 0, },
1651
    { "hdd", 1, NULL, 0, },
1652
    { "cdrom", 1, NULL, 0, },
1653
    { "boot", 1, NULL, 0, },
1654
    { "fda", 1, NULL, 0, },
1655
    { "fdb", 1, NULL, 0, },
1656
    { "no-code-copy", 0, NULL, 0 },
1657
    { "nics", 1, NULL, 0 },
1658
    { NULL, 0, NULL, 0 },
1659
};
1660

    
1661
#ifdef CONFIG_SDL
1662
/* SDL use the pthreads and they modify sigaction. We don't
1663
   want that. */
1664
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
1665
extern void __libc_sigaction();
1666
#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
1667
#else
1668
extern void __sigaction();
1669
#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
1670
#endif
1671
#endif /* CONFIG_SDL */
1672

    
1673
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1674

    
1675
/* this stack is only used during signal handling */
1676
#define SIGNAL_STACK_SIZE 32768
1677

    
1678
static uint8_t *signal_stack;
1679

    
1680
#endif
1681

    
1682
int main(int argc, char **argv)
1683
{
1684
#ifdef CONFIG_GDBSTUB
1685
    int use_gdbstub, gdbstub_port;
1686
#endif
1687
    int c, i, long_index, has_cdrom;
1688
    int snapshot, linux_boot;
1689
    CPUState *env;
1690
    const char *initrd_filename;
1691
    const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
1692
    const char *kernel_filename, *kernel_cmdline;
1693
    DisplayState *ds = &display_state;
1694
    int cyls, heads, secs;
1695

    
1696
#if !defined(CONFIG_SOFTMMU)
1697
    /* we never want that malloc() uses mmap() */
1698
    mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
1699
#endif
1700
    initrd_filename = NULL;
1701
    for(i = 0; i < MAX_FD; i++)
1702
        fd_filename[i] = NULL;
1703
    for(i = 0; i < MAX_DISKS; i++)
1704
        hd_filename[i] = NULL;
1705
    ram_size = 32 * 1024 * 1024;
1706
    vga_ram_size = VGA_RAM_SIZE;
1707
    pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
1708
#ifdef CONFIG_GDBSTUB
1709
    use_gdbstub = 0;
1710
    gdbstub_port = DEFAULT_GDBSTUB_PORT;
1711
#endif
1712
    snapshot = 0;
1713
    nographic = 0;
1714
    kernel_filename = NULL;
1715
    kernel_cmdline = "";
1716
    has_cdrom = 1;
1717
    cyls = heads = secs = 0;
1718

    
1719
    nb_nics = 1;
1720
    for(i = 0; i < MAX_NICS; i++) {
1721
        NetDriverState *nd = &nd_table[i];
1722
        nd->fd = -1;
1723
        /* init virtual mac address */
1724
        nd->macaddr[0] = 0x52;
1725
        nd->macaddr[1] = 0x54;
1726
        nd->macaddr[2] = 0x00;
1727
        nd->macaddr[3] = 0x12;
1728
        nd->macaddr[4] = 0x34;
1729
        nd->macaddr[5] = 0x56 + i;
1730
    }
1731
    
1732
    for(;;) {
1733
        c = getopt_long_only(argc, argv, "hm:d:n:sp:L:", long_options, &long_index);
1734
        if (c == -1)
1735
            break;
1736
        switch(c) {
1737
        case 0:
1738
            switch(long_index) {
1739
            case 0:
1740
                initrd_filename = optarg;
1741
                break;
1742
            case 1:
1743
                hd_filename[0] = optarg;
1744
                break;
1745
            case 2:
1746
                hd_filename[1] = optarg;
1747
                break;
1748
            case 3:
1749
                snapshot = 1;
1750
                break;
1751
            case 4:
1752
                {
1753
                    const char *p;
1754
                    p = optarg;
1755
                    cyls = strtol(p, (char **)&p, 0);
1756
                    if (*p != ',')
1757
                        goto chs_fail;
1758
                    p++;
1759
                    heads = strtol(p, (char **)&p, 0);
1760
                    if (*p != ',')
1761
                        goto chs_fail;
1762
                    p++;
1763
                    secs = strtol(p, (char **)&p, 0);
1764
                    if (*p != '\0') {
1765
                    chs_fail:
1766
                        cyls = 0;
1767
                    }
1768
                }
1769
                break;
1770
            case 5:
1771
                nographic = 1;
1772
                break;
1773
            case 6:
1774
                kernel_filename = optarg;
1775
                break;
1776
            case 7:
1777
                kernel_cmdline = optarg;
1778
                break;
1779
            case 8:
1780
                {
1781
                    const char *p;
1782
                    int fd;
1783
                    p = optarg;
1784
                    nb_nics = 0;
1785
                    for(;;) {
1786
                        fd = strtol(p, (char **)&p, 0);
1787
                        nd_table[nb_nics].fd = fd;
1788
                        snprintf(nd_table[nb_nics].ifname, 
1789
                                 sizeof(nd_table[nb_nics].ifname),
1790
                                 "fd%d", nb_nics);
1791
                        nb_nics++;
1792
                        if (*p == ',') {
1793
                            p++;
1794
                        } else if (*p != '\0') {
1795
                            fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
1796
                            exit(1);
1797
                        } else {
1798
                            break;
1799
                        }
1800
                    }
1801
                }
1802
                break;
1803
            case 9:
1804
                hd_filename[2] = optarg;
1805
                has_cdrom = 0;
1806
                break;
1807
            case 10:
1808
                hd_filename[3] = optarg;
1809
                break;
1810
            case 11:
1811
                hd_filename[2] = optarg;
1812
                has_cdrom = 1;
1813
                break;
1814
            case 12:
1815
                boot_device = optarg[0];
1816
                if (boot_device != 'a' && boot_device != 'b' &&
1817
                    boot_device != 'c' && boot_device != 'd') {
1818
                    fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1819
                    exit(1);
1820
                }
1821
                break;
1822
            case 13:
1823
                fd_filename[0] = optarg;
1824
                break;
1825
            case 14:
1826
                fd_filename[1] = optarg;
1827
                break;
1828
            case 15:
1829
                code_copy_enabled = 0;
1830
                break;
1831
            case 16:
1832
                nb_nics = atoi(optarg);
1833
                if (nb_nics < 1 || nb_nics > MAX_NICS) {
1834
                    fprintf(stderr, "qemu: invalid number of network interfaces\n");
1835
                    exit(1);
1836
                }
1837
                break;
1838
            }
1839
            break;
1840
        case 'h':
1841
            help();
1842
            break;
1843
        case 'm':
1844
            ram_size = atoi(optarg) * 1024 * 1024;
1845
            if (ram_size <= 0)
1846
                help();
1847
            if (ram_size > PHYS_RAM_MAX_SIZE) {
1848
                fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
1849
                        PHYS_RAM_MAX_SIZE / (1024 * 1024));
1850
                exit(1);
1851
            }
1852
            break;
1853
        case 'd':
1854
            {
1855
                int mask;
1856
                CPULogItem *item;
1857

    
1858
                mask = cpu_str_to_log_mask(optarg);
1859
                if (!mask) {
1860
                    printf("Log items (comma separated):\n");
1861
                    for(item = cpu_log_items; item->mask != 0; item++) {
1862
                        printf("%-10s %s\n", item->name, item->help);
1863
                    }
1864
                    exit(1);
1865
                }
1866
                cpu_set_log(mask);
1867
            }
1868
            break;
1869
        case 'n':
1870
            pstrcpy(network_script, sizeof(network_script), optarg);
1871
            break;
1872
#ifdef CONFIG_GDBSTUB
1873
        case 's':
1874
            use_gdbstub = 1;
1875
            break;
1876
        case 'p':
1877
            gdbstub_port = atoi(optarg);
1878
            break;
1879
#endif
1880
        case 'L':
1881
            bios_dir = optarg;
1882
            break;
1883
        }
1884
    }
1885

    
1886
    if (optind < argc) {
1887
        hd_filename[0] = argv[optind++];
1888
    }
1889

    
1890
    linux_boot = (kernel_filename != NULL);
1891
        
1892
    if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1893
        fd_filename[0] == '\0')
1894
        help();
1895
    
1896
    /* boot to cd by default if no hard disk */
1897
    if (hd_filename[0] == '\0' && boot_device == 'c') {
1898
        if (fd_filename[0] != '\0')
1899
            boot_device = 'a';
1900
        else
1901
            boot_device = 'd';
1902
    }
1903

    
1904
#if !defined(CONFIG_SOFTMMU)
1905
    /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1906
    {
1907
        static uint8_t stdout_buf[4096];
1908
        setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1909
    }
1910
#else
1911
    setvbuf(stdout, NULL, _IOLBF, 0);
1912
#endif
1913

    
1914
    /* init host network redirectors */
1915
    net_init();
1916

    
1917
    /* init the memory */
1918
    phys_ram_size = ram_size + vga_ram_size;
1919

    
1920
#ifdef CONFIG_SOFTMMU
1921
    phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
1922
    if (!phys_ram_base) {
1923
        fprintf(stderr, "Could not allocate physical memory\n");
1924
        exit(1);
1925
    }
1926
#else
1927
    /* as we must map the same page at several addresses, we must use
1928
       a fd */
1929
    {
1930
        const char *tmpdir;
1931

    
1932
        tmpdir = getenv("QEMU_TMPDIR");
1933
        if (!tmpdir)
1934
            tmpdir = "/tmp";
1935
        snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1936
        if (mkstemp(phys_ram_file) < 0) {
1937
            fprintf(stderr, "Could not create temporary memory file '%s'\n", 
1938
                    phys_ram_file);
1939
            exit(1);
1940
        }
1941
        phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1942
        if (phys_ram_fd < 0) {
1943
            fprintf(stderr, "Could not open temporary memory file '%s'\n", 
1944
                    phys_ram_file);
1945
            exit(1);
1946
        }
1947
        ftruncate(phys_ram_fd, phys_ram_size);
1948
        unlink(phys_ram_file);
1949
        phys_ram_base = mmap(get_mmap_addr(phys_ram_size), 
1950
                             phys_ram_size, 
1951
                             PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, 
1952
                             phys_ram_fd, 0);
1953
        if (phys_ram_base == MAP_FAILED) {
1954
            fprintf(stderr, "Could not map physical memory\n");
1955
            exit(1);
1956
        }
1957
    }
1958
#endif
1959

    
1960
    /* we always create the cdrom drive, even if no disk is there */
1961
    if (has_cdrom) {
1962
        bs_table[2] = bdrv_new("cdrom");
1963
        bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1964
    }
1965

    
1966
    /* open the virtual block devices */
1967
    for(i = 0; i < MAX_DISKS; i++) {
1968
        if (hd_filename[i]) {
1969
            if (!bs_table[i]) {
1970
                char buf[64];
1971
                snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1972
                bs_table[i] = bdrv_new(buf);
1973
            }
1974
            if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
1975
                fprintf(stderr, "qemu: could not open hard disk image '%s\n",
1976
                        hd_filename[i]);
1977
                exit(1);
1978
            }
1979
            if (i == 0 && cyls != 0) 
1980
                bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1981
        }
1982
    }
1983

    
1984
    /* we always create at least one floppy disk */
1985
    fd_table[0] = bdrv_new("fda");
1986
    bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1987

    
1988
    for(i = 0; i < MAX_FD; i++) {
1989
        if (fd_filename[i]) {
1990
            if (!fd_table[i]) {
1991
                char buf[64];
1992
                snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1993
                fd_table[i] = bdrv_new(buf);
1994
                bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1995
            }
1996
            if (fd_filename[i] != '\0') {
1997
                if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1998
                    fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1999
                            fd_filename[i]);
2000
                    exit(1);
2001
                }
2002
            }
2003
        }
2004
    }
2005

    
2006
    init_timers();
2007

    
2008
    /* init CPU state */
2009
    env = cpu_init();
2010
    global_env = env;
2011
    cpu_single_env = env;
2012

    
2013
    register_savevm("timer", 0, 1, timer_save, timer_load, env);
2014
    register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2015
    register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2016

    
2017
    init_ioports();
2018
    cpu_calibrate_ticks();
2019

    
2020
    /* terminal init */
2021
    if (nographic) {
2022
        dumb_display_init(ds);
2023
    } else {
2024
#ifdef CONFIG_SDL
2025
        sdl_display_init(ds);
2026
#else
2027
        dumb_display_init(ds);
2028
#endif
2029
    }
2030

    
2031
#if defined(TARGET_I386)
2032
    pc_init(ram_size, vga_ram_size, boot_device,
2033
            ds, fd_filename, snapshot,
2034
            kernel_filename, kernel_cmdline, initrd_filename);
2035
#elif defined(TARGET_PPC)
2036
    ppc_init();
2037
#endif
2038

    
2039
    /* launched after the device init so that it can display or not a
2040
       banner */
2041
    monitor_init();
2042

    
2043
    /* setup cpu signal handlers for MMU / self modifying code handling */
2044
#if !defined(CONFIG_SOFTMMU)
2045
    
2046
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
2047
    {
2048
        stack_t stk;
2049
        signal_stack = malloc(SIGNAL_STACK_SIZE);
2050
        stk.ss_sp = signal_stack;
2051
        stk.ss_size = SIGNAL_STACK_SIZE;
2052
        stk.ss_flags = 0;
2053

    
2054
        if (sigaltstack(&stk, NULL) < 0) {
2055
            perror("sigaltstack");
2056
            exit(1);
2057
        }
2058
    }
2059
#endif
2060
    {
2061
        struct sigaction act;
2062
        
2063
        sigfillset(&act.sa_mask);
2064
        act.sa_flags = SA_SIGINFO;
2065
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
2066
        act.sa_flags |= SA_ONSTACK;
2067
#endif
2068
        act.sa_sigaction = host_segv_handler;
2069
        sigaction(SIGSEGV, &act, NULL);
2070
        sigaction(SIGBUS, &act, NULL);
2071
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
2072
        sigaction(SIGFPE, &act, NULL);
2073
#endif
2074
    }
2075
#endif
2076

    
2077
#ifndef _WIN32
2078
    {
2079
        struct sigaction act;
2080
        sigfillset(&act.sa_mask);
2081
        act.sa_flags = 0;
2082
        act.sa_handler = SIG_IGN;
2083
        sigaction(SIGPIPE, &act, NULL);
2084
    }
2085
#endif
2086

    
2087
    gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2088
    qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2089

    
2090
#ifdef CONFIG_GDBSTUB
2091
    if (use_gdbstub) {
2092
        if (gdbserver_start(gdbstub_port) < 0) {
2093
            fprintf(stderr, "Could not open gdbserver socket on port %d\n", 
2094
                    gdbstub_port);
2095
            exit(1);
2096
        } else {
2097
            printf("Waiting gdb connection on port %d\n", gdbstub_port);
2098
        }
2099
    } else 
2100
#endif
2101
    {
2102
        vm_start();
2103
    }
2104
    term_init();
2105
    main_loop();
2106
    return 0;
2107
}