~ubuntu-branches/ubuntu/hardy/kvm/hardy-backports

« back to all changes in this revision

Viewing changes to qemu/vl.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-02-26 13:10:57 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20080226131057-s67x6l89mtjw1x9b
Tags: 1:62+dfsg-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * QEMU System Emulator
3
3
 *
4
 
 * Copyright (c) 2003-2007 Fabrice Bellard
 
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
5
 *
6
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
 * of this software and associated documentation files (the "Software"), to deal
133
133
 
134
134
#include "exec-all.h"
135
135
 
136
 
#if USE_KVM
137
136
#include "qemu-kvm.h"
138
 
#endif
139
137
 
140
138
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
141
139
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
239
237
int autostart = 1;
240
238
int time_drift_fix = 0;
241
239
unsigned int kvm_shadow_memory = 0;
 
240
const char *hugetlbpath = NULL;
 
241
const char *hugetlbfile = NULL;
 
242
int hpagesize = 0;
242
243
const char *cpu_vendor_string;
243
244
#ifdef TARGET_ARM
244
245
int old_param = 0;
250
251
const char *prom_envs[MAX_PROM_ENVS];
251
252
#endif
252
253
int nb_drives_opt;
253
 
char drives_opt[MAX_DRIVES][1024];
 
254
struct drive_opt {
 
255
    const char *file;
 
256
    char opt[1024];
 
257
} drives_opt[MAX_DRIVES];
254
258
 
255
259
static CPUState *cur_cpu;
256
260
static CPUState *next_cpu;
260
264
 
261
265
void decorate_application_name(char *appname, int max_len)
262
266
{
263
 
#if USE_KVM
264
 
    if (kvm_allowed)
 
267
    if (kvm_enabled())
265
268
    {
266
269
        int remain = max_len - strlen(appname) - 1;
267
270
 
268
271
        if (remain > 0)
269
272
            strncat(appname, "/KVM", remain);
270
273
    }
271
 
#endif
272
274
}
273
275
 
274
276
/***********************************************************/
854
856
};
855
857
 
856
858
#define ALARM_FLAG_DYNTICKS  0x1
 
859
#define ALARM_FLAG_EXPIRED   0x2
857
860
 
858
861
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
859
862
{
1065
1068
    ts->expire_time = expire_time;
1066
1069
    ts->next = *pt;
1067
1070
    *pt = ts;
 
1071
 
 
1072
    /* Rearm if necessary  */
 
1073
    if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0 &&
 
1074
        pt == &active_timers[ts->clock->type])
 
1075
        qemu_rearm_alarm_timer(alarm_timer);
1068
1076
}
1069
1077
 
1070
1078
int qemu_timer_pending(QEMUTimer *ts)
1099
1107
        /* run the callback (the timer list can be modified) */
1100
1108
        ts->cb(ts->opaque);
1101
1109
    }
1102
 
    qemu_rearm_alarm_timer(alarm_timer);
1103
1110
}
1104
1111
 
1105
1112
int64_t qemu_get_clock(QEMUClock *clock)
1219
1226
#endif
1220
1227
        CPUState *env = next_cpu;
1221
1228
 
 
1229
        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
 
1230
 
1222
1231
        if (env) {
1223
1232
            /* stop the currently executing cpu because a timer occured */
1224
1233
            cpu_interrupt(env, CPU_INTERRUPT_EXIT);
1328
1337
static int rtc_start_timer(struct qemu_alarm_timer *t)
1329
1338
{
1330
1339
    int rtc_fd;
 
1340
    unsigned long current_rtc_freq = 0;
1331
1341
 
1332
1342
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1333
1343
    if (rtc_fd < 0)
1334
1344
        return -1;
1335
 
    if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
 
1345
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
 
1346
    if (current_rtc_freq != RTC_FREQ &&
 
1347
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1336
1348
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1337
1349
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1338
1350
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1404
1416
 
1405
1417
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1406
1418
                !active_timers[QEMU_TIMER_VIRTUAL])
1407
 
            return;
 
1419
        return;
1408
1420
 
1409
1421
    nearest_delta_us = qemu_next_deadline();
1410
1422
 
1531
1543
 
1532
1544
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1533
1545
                !active_timers[QEMU_TIMER_VIRTUAL])
1534
 
            return;
 
1546
        return;
1535
1547
 
1536
1548
    nearest_delta_us = qemu_next_deadline();
1537
1549
    nearest_delta_us /= 1000;
2073
2085
    }
2074
2086
}
2075
2087
 
 
2088
static void fd_chr_close(struct CharDriverState *chr)
 
2089
{
 
2090
    FDCharDriver *s = chr->opaque;
 
2091
 
 
2092
    if (s->fd_in >= 0) {
 
2093
        if (nographic && s->fd_in == 0) {
 
2094
        } else {
 
2095
            qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
 
2096
        }
 
2097
    }
 
2098
 
 
2099
    qemu_free(s);
 
2100
}
 
2101
 
2076
2102
/* open a character device to a unix fd */
2077
2103
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2078
2104
{
2092
2118
    chr->opaque = s;
2093
2119
    chr->chr_write = fd_chr_write;
2094
2120
    chr->chr_update_read_handler = fd_chr_update_read_handler;
 
2121
    chr->chr_close = fd_chr_close;
2095
2122
 
2096
2123
    qemu_chr_reset(chr);
2097
2124
 
2178
2205
/* init terminal so that we can grab keys */
2179
2206
static struct termios oldtty;
2180
2207
static int old_fd0_flags;
 
2208
static int term_atexit_done;
2181
2209
 
2182
2210
static void term_exit(void)
2183
2211
{
2207
2235
 
2208
2236
    tcsetattr (0, TCSANOW, &tty);
2209
2237
 
2210
 
    atexit(term_exit);
 
2238
    if (!term_atexit_done++)
 
2239
        atexit(term_exit);
2211
2240
 
2212
2241
    fcntl(0, F_SETFL, O_NONBLOCK);
2213
2242
}
2214
2243
 
 
2244
static void qemu_chr_close_stdio(struct CharDriverState *chr)
 
2245
{
 
2246
    term_exit();
 
2247
    stdio_nb_clients--;
 
2248
    qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
 
2249
    fd_chr_close(chr);
 
2250
}
 
2251
 
2215
2252
static CharDriverState *qemu_chr_open_stdio(void)
2216
2253
{
2217
2254
    CharDriverState *chr;
2219
2256
    if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2220
2257
        return NULL;
2221
2258
    chr = qemu_chr_open_fd(0, 1);
 
2259
    chr->chr_close = qemu_chr_close_stdio;
2222
2260
    qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2223
2261
    stdio_nb_clients++;
2224
2262
    term_init();
2263
2301
#endif
2264
2302
    tcgetattr (fd, &tty);
2265
2303
 
2266
 
    switch(speed) {
2267
 
    case 50:
 
2304
#define MARGIN 1.1
 
2305
    if (speed <= 50 * MARGIN)
2268
2306
        spd = B50;
2269
 
        break;
2270
 
    case 75:
 
2307
    else if (speed <= 75 * MARGIN)
2271
2308
        spd = B75;
2272
 
        break;
2273
 
    case 300:
 
2309
    else if (speed <= 300 * MARGIN)
2274
2310
        spd = B300;
2275
 
        break;
2276
 
    case 600:
 
2311
    else if (speed <= 600 * MARGIN)
2277
2312
        spd = B600;
2278
 
        break;
2279
 
    case 1200:
 
2313
    else if (speed <= 1200 * MARGIN)
2280
2314
        spd = B1200;
2281
 
        break;
2282
 
    case 2400:
 
2315
    else if (speed <= 2400 * MARGIN)
2283
2316
        spd = B2400;
2284
 
        break;
2285
 
    case 4800:
 
2317
    else if (speed <= 4800 * MARGIN)
2286
2318
        spd = B4800;
2287
 
        break;
2288
 
    case 9600:
 
2319
    else if (speed <= 9600 * MARGIN)
2289
2320
        spd = B9600;
2290
 
        break;
2291
 
    case 19200:
 
2321
    else if (speed <= 19200 * MARGIN)
2292
2322
        spd = B19200;
2293
 
        break;
2294
 
    case 38400:
 
2323
    else if (speed <= 38400 * MARGIN)
2295
2324
        spd = B38400;
2296
 
        break;
2297
 
    case 57600:
 
2325
    else if (speed <= 57600 * MARGIN)
2298
2326
        spd = B57600;
2299
 
        break;
2300
 
    default:
2301
 
    case 115200:
2302
 
        spd = B115200;
2303
 
        break;
2304
 
    }
 
2327
    else if (speed <= 115200 * MARGIN)
 
2328
        spd = B115200;
 
2329
    else
 
2330
        spd = B115200;
2305
2331
 
2306
2332
    cfsetispeed(&tty, spd);
2307
2333
    cfsetospeed(&tty, spd);
3452
3478
{
3453
3479
    if (chr->chr_close)
3454
3480
        chr->chr_close(chr);
 
3481
    qemu_free(chr);
3455
3482
}
3456
3483
 
3457
3484
/***********************************************************/
3797
3824
 
3798
3825
char smb_dir[1024];
3799
3826
 
3800
 
static void smb_exit(void)
 
3827
static void erase_dir(char *dir_name)
3801
3828
{
3802
3829
    DIR *d;
3803
3830
    struct dirent *de;
3804
3831
    char filename[1024];
3805
3832
 
3806
3833
    /* erase all the files in the directory */
3807
 
    d = opendir(smb_dir);
3808
 
    for(;;) {
3809
 
        de = readdir(d);
3810
 
        if (!de)
3811
 
            break;
3812
 
        if (strcmp(de->d_name, ".") != 0 &&
3813
 
            strcmp(de->d_name, "..") != 0) {
3814
 
            snprintf(filename, sizeof(filename), "%s/%s",
3815
 
                     smb_dir, de->d_name);
3816
 
            unlink(filename);
 
3834
    if ((d = opendir(dir_name)) != 0) {
 
3835
        for(;;) {
 
3836
            de = readdir(d);
 
3837
            if (!de)
 
3838
                break;
 
3839
            if (strcmp(de->d_name, ".") != 0 &&
 
3840
                strcmp(de->d_name, "..") != 0) {
 
3841
                snprintf(filename, sizeof(filename), "%s/%s",
 
3842
                         smb_dir, de->d_name);
 
3843
                if (unlink(filename) != 0)  /* is it a directory? */
 
3844
                    erase_dir(filename);
 
3845
            }
3817
3846
        }
 
3847
        closedir(d);
 
3848
        rmdir(dir_name);
3818
3849
    }
3819
 
    closedir(d);
3820
 
    rmdir(smb_dir);
 
3850
}
 
3851
 
 
3852
/* automatic user mode samba server configuration */
 
3853
static void smb_exit(void)
 
3854
{
 
3855
    erase_dir(smb_dir);
3821
3856
}
3822
3857
 
3823
3858
/* automatic user mode samba server configuration */
4631
4666
 
4632
4667
}
4633
4668
 
4634
 
static const char *get_word(char *buf, int buf_size, const char *p)
4635
 
{
4636
 
    char *q;
4637
 
    int substring;
4638
 
 
4639
 
    substring = 0;
 
4669
static const char *get_opt_name(char *buf, int buf_size, const char *p)
 
4670
{
 
4671
    char *q;
 
4672
 
 
4673
    q = buf;
 
4674
    while (*p != '\0' && *p != '=') {
 
4675
        if (q && (q - buf) < buf_size - 1)
 
4676
            *q++ = *p;
 
4677
        p++;
 
4678
    }
 
4679
    if (q)
 
4680
        *q = '\0';
 
4681
 
 
4682
    return p;
 
4683
}
 
4684
 
 
4685
static const char *get_opt_value(char *buf, int buf_size, const char *p)
 
4686
{
 
4687
    char *q;
 
4688
 
4640
4689
    q = buf;
4641
4690
    while (*p != '\0') {
4642
 
        if (*p == '\\') {
4643
 
            p++;
4644
 
            if (*p == '\0')
 
4691
        if (*p == ',') {
 
4692
            if (*(p + 1) != ',')
4645
4693
                break;
4646
 
        } else if (*p == '\"') {
4647
 
            substring = !substring;
4648
4694
            p++;
4649
 
            continue;
4650
 
        } else if (!substring && (*p == ',' || *p == '='))
4651
 
            break;
 
4695
        }
4652
4696
        if (q && (q - buf) < buf_size - 1)
4653
4697
            *q++ = *p;
4654
4698
        p++;
4667
4711
 
4668
4712
    p = str;
4669
4713
    for(;;) {
4670
 
        p = get_word(option, sizeof(option), p);
 
4714
        p = get_opt_name(option, sizeof(option), p);
4671
4715
        if (*p != '=')
4672
4716
            break;
4673
4717
        p++;
4674
4718
        if (!strcmp(tag, option)) {
4675
 
            (void)get_word(buf, buf_size, p);
 
4719
            (void)get_opt_value(buf, buf_size, p);
4676
4720
            return strlen(buf);
4677
4721
        } else {
4678
 
            p = get_word(NULL, 0, p);
 
4722
            p = get_opt_value(NULL, 0, p);
4679
4723
        }
4680
4724
        if (*p != ',')
4681
4725
            break;
4692
4736
 
4693
4737
    p = str;
4694
4738
    for(;;) {
4695
 
        p = get_word(buf, buf_size, p);
 
4739
        p = get_opt_name(buf, buf_size, p);
4696
4740
        if (*p != '=')
4697
4741
            return -1;
4698
4742
        p++;
4701
4745
                break;
4702
4746
        if (params[i] == NULL)
4703
4747
            return -1;
4704
 
        p = get_word(NULL, 0, p);
 
4748
        p = get_opt_value(NULL, 0, p);
4705
4749
        if (*p != ',')
4706
4750
            break;
4707
4751
        p++;
4860
4904
    }
4861
4905
}
4862
4906
 
4863
 
#define HD_ALIAS "file=\"%s\",index=%d,media=disk"
 
4907
#define HD_ALIAS "index=%d,media=disk"
4864
4908
#ifdef TARGET_PPC
4865
4909
#define CDROM_ALIAS "index=1,media=cdrom"
4866
4910
#else
4867
4911
#define CDROM_ALIAS "index=2,media=cdrom"
4868
4912
#endif
4869
4913
#define FD_ALIAS "index=%d,if=floppy"
4870
 
#define PFLASH_ALIAS "file=\"%s\",if=pflash"
4871
 
#define MTD_ALIAS "file=\"%s\",if=mtd"
 
4914
#define PFLASH_ALIAS "if=pflash"
 
4915
#define MTD_ALIAS "if=mtd"
4872
4916
#define SD_ALIAS "index=0,if=sd"
4873
4917
 
4874
 
static int drive_add(const char *fmt, ...)
 
4918
static int drive_add(const char *file, const char *fmt, ...)
4875
4919
{
4876
4920
    va_list ap;
4877
4921
 
4880
4924
        exit(1);
4881
4925
    }
4882
4926
 
 
4927
    drives_opt[nb_drives_opt].file = file;
4883
4928
    va_start(ap, fmt);
4884
 
    vsnprintf(drives_opt[nb_drives_opt], sizeof(drives_opt[0]), fmt, ap);
 
4929
    vsnprintf(drives_opt[nb_drives_opt].opt,
 
4930
              sizeof(drives_opt[0].opt), fmt, ap);
4885
4931
    va_end(ap);
4886
4932
 
4887
4933
    return nb_drives_opt++;
4916
4962
    return max_bus;
4917
4963
}
4918
4964
 
4919
 
static int drive_init(const char *str, int snapshot, QEMUMachine *machine)
 
4965
static int drive_init(struct drive_opt *arg, int snapshot,
 
4966
                      QEMUMachine *machine)
4920
4967
{
4921
4968
    char buf[128];
4922
4969
    char file[1024];
4931
4978
    int index;
4932
4979
    int cache;
4933
4980
    int bdrv_flags;
 
4981
    char *str = arg->opt;
4934
4982
    char *params[] = { "bus", "unit", "if", "index", "cyls", "heads",
4935
4983
                       "secs", "trans", "media", "snapshot", "file",
4936
4984
                       "cache", "boot", NULL };
5117
5165
        }
5118
5166
    }
5119
5167
 
5120
 
    get_param_value(file, sizeof(file), "file", str);
 
5168
    if (arg->file == NULL)
 
5169
        get_param_value(file, sizeof(file), "file", str);
 
5170
    else
 
5171
        pstrcpy(file, sizeof(file), arg->file);
5121
5172
 
5122
5173
    /* compute bus and unit according index */
5123
5174
 
5263
5314
        dev = usb_msd_init(p);
5264
5315
    } else if (!strcmp(devname, "wacom-tablet")) {
5265
5316
        dev = usb_wacom_init();
 
5317
    } else if (strstart(devname, "serial:", &p)) {
 
5318
        dev = usb_serial_init(p);
5266
5319
    } else {
5267
5320
        return -1;
5268
5321
    }
6494
6547
    uint32_t hflags;
6495
6548
    int i;
6496
6549
 
6497
 
#ifdef USE_KVM
6498
 
    if (kvm_allowed)
 
6550
    if (kvm_enabled())
6499
6551
        kvm_save_registers(env);
6500
 
#endif
6501
6552
 
6502
6553
    for(i = 0; i < CPU_NB_REGS; i++)
6503
6554
        qemu_put_betls(f, &env->regs[i]);
6584
6635
#endif
6585
6636
    qemu_put_be32s(f, &env->smbase);
6586
6637
 
6587
 
#ifdef USE_KVM
6588
 
    if (kvm_allowed) {
 
6638
    if (kvm_enabled()) {
6589
6639
        for (i = 0; i < NR_IRQ_WORDS ; i++) {
6590
6640
            qemu_put_be32s(f, &env->kvm_interrupt_bitmap[i]);
6591
6641
        }
6592
6642
        qemu_put_be64s(f, &env->tsc);
6593
6643
    }
6594
 
#endif
6595
 
 
6596
6644
}
6597
6645
 
6598
6646
#ifdef USE_X86LDOUBLE
6735
6783
    /* XXX: compute hflags from scratch, except for CPL and IIF */
6736
6784
    env->hflags = hflags;
6737
6785
    tlb_flush(env, 1);
6738
 
#ifdef USE_KVM
6739
 
    if (kvm_allowed) {
 
6786
    if (kvm_enabled()) {
6740
6787
        /* when in-kernel irqchip is used, HF_HALTED_MASK causes deadlock
6741
6788
           because no userspace IRQs will ever clear this flag */
6742
6789
        env->hflags &= ~HF_HALTED_MASK;
6746
6793
        qemu_get_be64s(f, &env->tsc);
6747
6794
        kvm_load_registers(env);
6748
6795
    }
6749
 
#endif
6750
6796
    return 0;
6751
6797
}
6752
6798
 
7083
7129
    if (qemu_get_be32(f) != phys_ram_size)
7084
7130
        return -EINVAL;
7085
7131
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
7086
 
#ifdef USE_KVM
7087
 
        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
 
7132
        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
7088
7133
            continue;
7089
 
#endif
7090
7134
        ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
7091
7135
        if (ret)
7092
7136
            return ret;
7221
7265
    target_ulong addr;
7222
7266
 
7223
7267
    for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
7224
 
#ifdef USE_KVM
7225
 
        if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
 
7268
        if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
7226
7269
            continue;
7227
 
#endif
7228
7270
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
7229
7271
            qemu_put_be32(f, addr);
7230
7272
            qemu_put_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
7243
7285
    if (ram_compress_open(s, f) < 0)
7244
7286
        return;
7245
7287
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
7246
 
#ifdef USE_KVM
7247
 
        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
 
7288
        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
7248
7289
            continue;
7249
 
#endif
7250
7290
#if 0
7251
7291
        if (tight_savevm_enabled) {
7252
7292
            int64_t sector_num;
7318
7358
    if (ram_decompress_open(s, f) < 0)
7319
7359
        return -EINVAL;
7320
7360
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
7321
 
#ifdef USE_KVM
7322
 
        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
 
7361
        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
7323
7362
            continue;
7324
 
#endif
7325
7363
        if (ram_decompress_buf(s, buf, 1) < 0) {
7326
7364
            fprintf(stderr, "Error while reading ram block header\n");
7327
7365
            goto error;
7751
7789
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
7752
7790
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
7753
7791
                ioh->fd_read(ioh->opaque);
7754
 
                more = 1;
 
7792
                if (!ioh->fd_read_poll || ioh->fd_read_poll(ioh->opaque))
 
7793
                    more = 1;
 
7794
                else
 
7795
                    FD_CLR(ioh->fd, &rfds);
7755
7796
            }
7756
7797
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
7757
7798
                ioh->fd_write(ioh->opaque);
7797
7838
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
7798
7839
                    qemu_get_clock(rt_clock));
7799
7840
 
 
7841
    if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
 
7842
        alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
 
7843
        qemu_rearm_alarm_timer(alarm_timer);
 
7844
    }
 
7845
 
7800
7846
    /* Check bottom-halves last in case any of the earlier events triggered
7801
7847
       them.  */
7802
7848
    qemu_bh_poll();
7812
7858
    CPUState *env;
7813
7859
 
7814
7860
 
7815
 
#ifdef USE_KVM
7816
 
    if (kvm_allowed) {
 
7861
    if (kvm_enabled()) {
7817
7862
        kvm_main_loop();
7818
7863
        cpu_disable_ticks();
7819
7864
        return 0;
7820
7865
    }
7821
 
#endif
 
7866
 
7822
7867
    cur_cpu = first_cpu;
7823
7868
    next_cpu = cur_cpu->next_cpu ?: first_cpu;
7824
7869
    for(;;) {
7860
7905
            if (reset_requested) {
7861
7906
                reset_requested = 0;
7862
7907
                qemu_system_reset();
7863
 
#ifdef USE_KVM
7864
 
                if (kvm_allowed)
 
7908
                if (kvm_enabled())
7865
7909
                        kvm_load_registers(env);
7866
 
#endif
7867
7910
                ret = EXCP_INTERRUPT;
7868
7911
            }
7869
7912
            if (powerdown_requested) {
7898
7941
static void help(int exitcode)
7899
7942
{
7900
7943
    printf("QEMU PC emulator version " QEMU_VERSION " (" KVM_VERSION ")"
7901
 
           ", Copyright (c) 2003-2007 Fabrice Bellard\n"
 
7944
           ", Copyright (c) 2003-2008 Fabrice Bellard\n"
7902
7945
           "usage: %s [options] [disk_image]\n"
7903
7946
           "\n"
7904
7947
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
8013
8056
           "-no-kqemu       disable KQEMU kernel module usage\n"
8014
8057
#endif
8015
8058
#ifdef USE_KVM
 
8059
#ifndef NO_CPU_EMULATION
8016
8060
           "-no-kvm         disable KVM hardware virtualization\n"
 
8061
#endif
8017
8062
           "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
8018
8063
#endif
8019
8064
#ifdef TARGET_I386
8029
8074
#endif
8030
8075
           "-tdf            inject timer interrupts that got lost\n"
8031
8076
           "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
 
8077
           "-hugetlb-path   set the path to hugetlbfs mounted directory, also enables allocation of guest memory with huge pages\n"
8032
8078
           "-option-rom rom load a file, rom, into the option ROM space\n"
8033
8079
#ifdef TARGET_SPARC
8034
8080
           "-prom-env variable=value  set OpenBIOS nvram variables\n"
8035
8081
#endif
8036
8082
           "-clock          force the use of the given methods for timer alarm.\n"
8037
8083
           "                To see what timers are available use -clock help\n"
 
8084
           "-startdate      select initial date of the clock\n"
8038
8085
           "\n"
8039
8086
           "During emulation, the following keys are useful:\n"
8040
8087
           "ctrl-alt-f      toggle full screen\n"
8142
8189
    QEMU_OPTION_old_param,
8143
8190
    QEMU_OPTION_clock,
8144
8191
    QEMU_OPTION_startdate,
 
8192
    QEMU_OPTION_translation,
8145
8193
    QEMU_OPTION_incoming,
8146
8194
    QEMU_OPTION_tdf,
8147
8195
    QEMU_OPTION_kvm_shadow_memory,
 
8196
    QEMU_OPTION_hugetlbpath,
8148
8197
};
8149
8198
 
8150
8199
typedef struct QEMUOption {
8211
8260
    { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
8212
8261
#endif
8213
8262
#ifdef USE_KVM
 
8263
#ifndef NO_CPU_EMULATION
8214
8264
    { "no-kvm", 0, QEMU_OPTION_no_kvm },
 
8265
#endif
8215
8266
    { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip },
8216
8267
#endif
8217
8268
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
8264
8315
#endif
8265
8316
    { "clock", HAS_ARG, QEMU_OPTION_clock },
8266
8317
    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
 
8318
    { "hugetlb-path", HAS_ARG, QEMU_OPTION_hugetlbpath },
8267
8319
    { NULL },
8268
8320
};
8269
8321
 
8318
8370
    qemu_register_machine(&prep_machine);
8319
8371
    qemu_register_machine(&ref405ep_machine);
8320
8372
    qemu_register_machine(&taihu_machine);
 
8373
    qemu_register_machine(&bamboo_machine);
8321
8374
#elif defined(TARGET_MIPS)
8322
8375
    qemu_register_machine(&mips_machine);
8323
8376
    qemu_register_machine(&mips_malta_machine);
8331
8384
    qemu_register_machine(&ss10_machine);
8332
8385
    qemu_register_machine(&ss600mp_machine);
8333
8386
    qemu_register_machine(&ss20_machine);
 
8387
    qemu_register_machine(&ss2_machine);
 
8388
    qemu_register_machine(&ss1000_machine);
 
8389
    qemu_register_machine(&ss2000_machine);
8334
8390
#endif
8335
8391
#elif defined(TARGET_ARM)
8336
8392
    qemu_register_machine(&integratorcp_machine);
8409
8465
    },
8410
8466
#endif
8411
8467
 
 
8468
#ifdef CONFIG_AC97
 
8469
    {
 
8470
        "ac97",
 
8471
        "Intel 82801AA AC97 Audio",
 
8472
        0,
 
8473
        0,
 
8474
        { .init_pci = ac97_init }
 
8475
    },
 
8476
#endif
 
8477
 
8412
8478
    {
8413
8479
        "es1370",
8414
8480
        "ENSONIQ AudioPCI ES1370",
8501
8567
    *opt_incoming = incoming;
8502
8568
}
8503
8569
 
 
8570
 
 
8571
static int gethugepagesize(void)
 
8572
{
 
8573
        int ret, fd;
 
8574
        char buf[4096];
 
8575
        char *needle = "Hugepagesize:";
 
8576
        char *size;
 
8577
        unsigned long hugepagesize;
 
8578
 
 
8579
        fd = open("/proc/meminfo", O_RDONLY);
 
8580
        if (fd < 0) {
 
8581
                perror("open");
 
8582
                exit(0);
 
8583
        }
 
8584
 
 
8585
        ret = read(fd, buf, sizeof(buf));
 
8586
        if (ret < 0) {
 
8587
                perror("read");
 
8588
                exit(0);
 
8589
        }
 
8590
 
 
8591
        size = strstr(buf, needle);
 
8592
        if (!size)
 
8593
                return 0;
 
8594
        size += strlen(needle);
 
8595
        hugepagesize = strtol(size, NULL, 0);
 
8596
        return hugepagesize;
 
8597
}
 
8598
 
 
8599
void cleanup_hugetlb(void)
 
8600
{
 
8601
        if (hugetlbfile)
 
8602
                unlink(hugetlbfile);
 
8603
}
 
8604
 
 
8605
void *alloc_huge_area(unsigned long memory, const char *path)
 
8606
{
 
8607
        void *area;
 
8608
        int fd;
 
8609
        char *filename;
 
8610
        char *tmpfile = "/kvm.XXXXXX";
 
8611
 
 
8612
        filename = qemu_malloc(4096);
 
8613
        if (!filename)
 
8614
                return NULL;
 
8615
 
 
8616
        memset(filename, 0, 4096);
 
8617
        strncpy(filename, path, 4096 - strlen(tmpfile) - 1);
 
8618
        strcat(filename, tmpfile);
 
8619
 
 
8620
        hpagesize = gethugepagesize() * 1024;
 
8621
        if (!hpagesize)
 
8622
                return NULL;
 
8623
 
 
8624
        mkstemp(filename);
 
8625
        fd = open(filename, O_RDWR);
 
8626
        if (fd < 0) {
 
8627
                perror("open");
 
8628
                hpagesize = 0;
 
8629
                exit(0);
 
8630
        }
 
8631
        memory = (memory+hpagesize-1) & ~(hpagesize-1);
 
8632
 
 
8633
        area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
 
8634
        if (area == MAP_FAILED) {
 
8635
                perror("mmap");
 
8636
                hpagesize = 0;
 
8637
                exit(0);
 
8638
        }
 
8639
 
 
8640
        hugetlbfile = filename;
 
8641
        atexit(cleanup_hugetlb);
 
8642
 
 
8643
        return area;
 
8644
}
 
8645
 
 
8646
void *qemu_alloc_physram(unsigned long memory)
 
8647
{
 
8648
        void *area = NULL;
 
8649
 
 
8650
        if (hugetlbpath)
 
8651
                area = alloc_huge_area(memory, hugetlbpath);
 
8652
        if (!area)
 
8653
                area = qemu_vmalloc(memory);
 
8654
 
 
8655
        return area;
 
8656
}
 
8657
 
8504
8658
int main(int argc, char **argv)
8505
8659
{
8506
8660
#ifdef CONFIG_GDBSTUB
8619
8773
            break;
8620
8774
        r = argv[optind];
8621
8775
        if (r[0] != '-') {
8622
 
            hda_index = drive_add(HD_ALIAS, argv[optind++], 0);
 
8776
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
8623
8777
        } else {
8624
8778
            const QEMUOption *popt;
8625
8779
 
8680
8834
                break;
8681
8835
            case QEMU_OPTION_hda:
8682
8836
                if (cyls == 0)
8683
 
                    hda_index = drive_add(HD_ALIAS, optarg, 0);
 
8837
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
8684
8838
                else
8685
 
                    hda_index = drive_add(HD_ALIAS
 
8839
                    hda_index = drive_add(optarg, HD_ALIAS
8686
8840
                             ",cyls=%d,heads=%d,secs=%d%s",
8687
 
                             optarg, 0, cyls, heads, secs,
 
8841
                             0, cyls, heads, secs,
8688
8842
                             translation == BIOS_ATA_TRANSLATION_LBA ?
8689
8843
                                 ",trans=lba" :
8690
8844
                             translation == BIOS_ATA_TRANSLATION_NONE ?
8693
8847
            case QEMU_OPTION_hdb:
8694
8848
            case QEMU_OPTION_hdc:
8695
8849
            case QEMU_OPTION_hdd:
8696
 
                drive_add(HD_ALIAS, optarg, popt->index - QEMU_OPTION_hda);
 
8850
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
8697
8851
                break;
8698
8852
            case QEMU_OPTION_drive:
8699
 
                drive_add("%s", optarg);
 
8853
                drive_add(NULL, "%s", optarg);
8700
8854
                break;
8701
8855
            case QEMU_OPTION_mtdblock:
8702
 
                drive_add(MTD_ALIAS, optarg);
 
8856
                drive_add(optarg, MTD_ALIAS);
8703
8857
                break;
8704
8858
            case QEMU_OPTION_sd:
8705
 
                drive_add("file=\"%s\"," SD_ALIAS, optarg);
 
8859
                drive_add(optarg, SD_ALIAS);
8706
8860
                break;
8707
8861
            case QEMU_OPTION_pflash:
8708
 
                drive_add(PFLASH_ALIAS, optarg);
 
8862
                drive_add(optarg, PFLASH_ALIAS);
8709
8863
                break;
8710
8864
            case QEMU_OPTION_snapshot:
8711
8865
                snapshot = 1;
8745
8899
                        exit(1);
8746
8900
                    }
8747
8901
                    if (hda_index != -1)
8748
 
                        snprintf(drives_opt[hda_index] +
8749
 
                                 strlen(drives_opt[hda_index]),
8750
 
                                 sizeof(drives_opt[0]) -
8751
 
                                 strlen(drives_opt[hda_index]),
8752
 
                                 ",cyls=%d,heads=%d,secs=%d%s",
8753
 
                                 cyls, heads, secs,
 
8902
                        snprintf(drives_opt[hda_index].opt,
 
8903
                                 sizeof(drives_opt[hda_index].opt),
 
8904
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
 
8905
                                 0, cyls, heads, secs,
8754
8906
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
8755
8907
                                    ",trans=lba" :
8756
8908
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
8773
8925
                kernel_cmdline = optarg;
8774
8926
                break;
8775
8927
            case QEMU_OPTION_cdrom:
8776
 
                drive_add("file=\"%s\"," CDROM_ALIAS, optarg);
 
8928
                drive_add(optarg, CDROM_ALIAS);
8777
8929
                break;
8778
8930
            case QEMU_OPTION_boot:
8779
8931
                boot_devices = optarg;
8808
8960
                break;
8809
8961
            case QEMU_OPTION_fda:
8810
8962
            case QEMU_OPTION_fdb:
8811
 
                drive_add("file=\"%s\"," FD_ALIAS, optarg,
8812
 
                          popt->index - QEMU_OPTION_fda);
 
8963
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
8813
8964
                break;
8814
8965
#ifdef TARGET_I386
8815
8966
            case QEMU_OPTION_no_fd_bootchk:
9043
9194
            case QEMU_OPTION_no_kvm:
9044
9195
                kvm_allowed = 0;
9045
9196
                break;
9046
 
            case QEMU_OPTION_no_kvm_irqchip:
 
9197
            case QEMU_OPTION_no_kvm_irqchip: {
 
9198
                extern int kvm_irqchip;
9047
9199
                kvm_irqchip = 0;
9048
9200
                break;
 
9201
            }
9049
9202
#endif
9050
9203
            case QEMU_OPTION_usb:
9051
9204
                usb_enabled = 1;
9100
9253
            case QEMU_OPTION_kvm_shadow_memory:
9101
9254
                kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
9102
9255
                break;
 
9256
            case QEMU_OPTION_hugetlbpath:
 
9257
                hugetlbpath = optarg;
 
9258
                break;
9103
9259
            case QEMU_OPTION_name:
9104
9260
                qemu_name = optarg;
9105
9261
                break;
9119
9275
#ifdef TARGET_ARM
9120
9276
            case QEMU_OPTION_old_param:
9121
9277
                old_param = 1;
 
9278
                break;
9122
9279
#endif
9123
9280
            case QEMU_OPTION_clock:
9124
9281
                configure_alarms(optarg);
9209
9366
#endif
9210
9367
 
9211
9368
#if USE_KVM
9212
 
    if (kvm_allowed) {
 
9369
    if (kvm_enabled()) {
9213
9370
        if (kvm_qemu_init() < 0) {
 
9371
            extern int kvm_allowed;
9214
9372
            fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
 
9373
#ifdef NO_CPU_EMULATION
 
9374
            fprintf(stderr, "Compiled with --disable-cpu-emulation, exiting.\n");
 
9375
            exit(1);
 
9376
#endif
9215
9377
            kvm_allowed = 0;
9216
9378
        }
9217
9379
    }
9311
9473
    /* init the memory */
9312
9474
    phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
9313
9475
 
9314
 
#if USE_KVM
9315
9476
    /* Initialize kvm */
9316
 
#ifdef TARGET_IA64
 
9477
#if defined(TARGET_I386) || defined(TARGET_X86_64)
 
9478
#define KVM_EXTRA_PAGES 3
 
9479
#else
9317
9480
#define KVM_EXTRA_PAGES 0
9318
 
#else
9319
 
#define KVM_EXTRA_PAGES 3
9320
9481
#endif
9321
 
    if (kvm_allowed) {
 
9482
    if (kvm_enabled()) {
9322
9483
            phys_ram_size += KVM_EXTRA_PAGES * TARGET_PAGE_SIZE;
9323
9484
            if (kvm_qemu_create_context() < 0) {
9324
9485
                    fprintf(stderr, "Could not create KVM context\n");
9330
9491
 
9331
9492
            ret = kvm_qemu_check_extension(KVM_CAP_USER_MEMORY);
9332
9493
            if (ret) {
9333
 
                phys_ram_base = qemu_vmalloc(phys_ram_size);
 
9494
                phys_ram_base = qemu_alloc_physram(phys_ram_size);
9334
9495
                if (!phys_ram_base) {
9335
9496
                        fprintf(stderr, "Could not allocate physical memory\n");
9336
9497
                        exit(1);
9345
9506
                    exit(1);
9346
9507
            }
9347
9508
    }
9348
 
#else
9349
 
    phys_ram_base = qemu_vmalloc(phys_ram_size);
9350
 
    if (!phys_ram_base) {
9351
 
        fprintf(stderr, "Could not allocate physical memory\n");
9352
 
        exit(1);
9353
 
    }
9354
 
#endif
9355
9509
 
9356
9510
    bdrv_init();
9357
9511
 
9358
9512
    /* we always create the cdrom drive, even if no disk is there */
9359
9513
 
9360
9514
    if (nb_drives_opt < MAX_DRIVES)
9361
 
        drive_add(CDROM_ALIAS);
 
9515
        drive_add(NULL, CDROM_ALIAS);
9362
9516
 
9363
9517
    /* we always create at least one floppy */
9364
9518
 
9365
9519
    if (nb_drives_opt < MAX_DRIVES)
9366
 
        drive_add(FD_ALIAS, 0);
 
9520
        drive_add(NULL, FD_ALIAS, 0);
9367
9521
 
9368
9522
    /* we always create one sd slot, even if no card is in it */
9369
9523
 
9370
9524
    if (nb_drives_opt < MAX_DRIVES)
9371
 
        drive_add(SD_ALIAS);
 
9525
        drive_add(NULL, SD_ALIAS);
9372
9526
 
9373
9527
    /* open the virtual block devices */
9374
9528
 
9375
9529
    for(i = 0; i < nb_drives_opt; i++)
9376
 
        if (drive_init(drives_opt[i], snapshot, machine) == -1)
 
9530
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
9377
9531
            exit(1);
9378
9532
 
9379
9533
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
9493
9647
        qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
9494
9648
    }
9495
9649
 
9496
 
#ifdef USE_KVM
9497
 
    if (kvm_allowed)
 
9650
    if (kvm_enabled())
9498
9651
        kvm_init_ap();
9499
 
#endif
9500
9652
 
9501
9653
#ifdef CONFIG_GDBSTUB
9502
9654
    if (use_gdbstub) {