2
2
* QEMU System Emulator
4
* Copyright (c) 2003-2007 Fabrice Bellard
4
* Copyright (c) 2003-2008 Fabrice Bellard
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
250
251
const char *prom_envs[MAX_PROM_ENVS];
252
253
int nb_drives_opt;
253
char drives_opt[MAX_DRIVES][1024];
257
} drives_opt[MAX_DRIVES];
255
259
static CPUState *cur_cpu;
256
260
static CPUState *next_cpu;
261
265
void decorate_application_name(char *appname, int max_len)
266
269
int remain = max_len - strlen(appname) - 1;
269
272
strncat(appname, "/KVM", remain);
274
276
/***********************************************************/
1065
1068
ts->expire_time = expire_time;
1066
1069
ts->next = *pt;
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);
1070
1078
int qemu_timer_pending(QEMUTimer *ts)
1220
1227
CPUState *env = next_cpu;
1229
alarm_timer->flags |= ALARM_FLAG_EXPIRED;
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)
1340
unsigned long current_rtc_freq = 0;
1332
1342
TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1333
1343
if (rtc_fd < 0)
1335
if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1345
ioctl(rtc_fd, RTC_IRQP_READ, ¤t_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");
2088
static void fd_chr_close(struct CharDriverState *chr)
2090
FDCharDriver *s = chr->opaque;
2092
if (s->fd_in >= 0) {
2093
if (nographic && s->fd_in == 0) {
2095
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2076
2102
/* open a character device to a unix fd */
2077
2103
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
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;
2182
2210
static void term_exit(void)
2208
2236
tcsetattr (0, TCSANOW, &tty);
2238
if (!term_atexit_done++)
2212
2241
fcntl(0, F_SETFL, O_NONBLOCK);
2244
static void qemu_chr_close_stdio(struct CharDriverState *chr)
2248
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2215
2252
static CharDriverState *qemu_chr_open_stdio(void)
2217
2254
CharDriverState *chr;
2219
2256
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
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++;
2264
2302
tcgetattr (fd, &tty);
2305
if (speed <= 50 * MARGIN)
2307
else if (speed <= 75 * MARGIN)
2309
else if (speed <= 300 * MARGIN)
2311
else if (speed <= 600 * MARGIN)
2313
else if (speed <= 1200 * MARGIN)
2315
else if (speed <= 2400 * MARGIN)
2317
else if (speed <= 4800 * MARGIN)
2319
else if (speed <= 9600 * MARGIN)
2321
else if (speed <= 19200 * MARGIN)
2323
else if (speed <= 38400 * MARGIN)
2325
else if (speed <= 57600 * MARGIN)
2327
else if (speed <= 115200 * MARGIN)
2306
2332
cfsetispeed(&tty, spd);
2307
2333
cfsetospeed(&tty, spd);
3798
3825
char smb_dir[1024];
3800
static void smb_exit(void)
3827
static void erase_dir(char *dir_name)
3803
3830
struct dirent *de;
3804
3831
char filename[1024];
3806
3833
/* erase all the files in the directory */
3807
d = opendir(smb_dir);
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);
3834
if ((d = opendir(dir_name)) != 0) {
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);
3852
/* automatic user mode samba server configuration */
3853
static void smb_exit(void)
3823
3858
/* automatic user mode samba server configuration */
4634
static const char *get_word(char *buf, int buf_size, const char *p)
4669
static const char *get_opt_name(char *buf, int buf_size, const char *p)
4674
while (*p != '\0' && *p != '=') {
4675
if (q && (q - buf) < buf_size - 1)
4685
static const char *get_opt_value(char *buf, int buf_size, const char *p)
4641
4690
while (*p != '\0') {
4692
if (*(p + 1) != ',')
4646
} else if (*p == '\"') {
4647
substring = !substring;
4650
} else if (!substring && (*p == ',' || *p == '='))
4652
4696
if (q && (q - buf) < buf_size - 1)
4670
p = get_word(option, sizeof(option), p);
4714
p = get_opt_name(option, sizeof(option), 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);
4678
p = get_word(NULL, 0, p);
4722
p = get_opt_value(NULL, 0, p);
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"
4867
4911
#define CDROM_ALIAS "index=2,media=cdrom"
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"
4874
static int drive_add(const char *fmt, ...)
4918
static int drive_add(const char *file, const char *fmt, ...)
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);
4887
4933
return nb_drives_opt++;
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 };
5120
get_param_value(file, sizeof(file), "file", str);
5168
if (arg->file == NULL)
5169
get_param_value(file, sizeof(file), "file", str);
5171
pstrcpy(file, sizeof(file), arg->file);
5122
5173
/* compute bus and unit according index */
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);
6585
6636
qemu_put_be32s(f, &env->smbase);
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]);
6592
6642
qemu_put_be64s(f, &env->tsc);
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);
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;
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) {
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 */
7090
7134
ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
7221
7265
target_ulong addr;
7223
7267
for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
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 */
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)
7245
7287
for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
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 */
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) {
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 */
7325
7363
if (ram_decompress_buf(s, buf, 1) < 0) {
7326
7364
fprintf(stderr, "Error while reading ram block header\n");
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);
7792
if (!ioh->fd_read_poll || ioh->fd_read_poll(ioh->opaque))
7795
FD_CLR(ioh->fd, &rfds);
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));
7841
if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
7842
alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
7843
qemu_rearm_alarm_timer(alarm_timer);
7800
7846
/* Check bottom-halves last in case any of the earlier events triggered
7802
7848
qemu_bh_poll();
7898
7941
static void help(int exitcode)
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"
7904
7947
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
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"
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"
8039
8086
"During emulation, the following keys are useful:\n"
8040
8087
"ctrl-alt-f toggle full screen\n"
8211
8260
{ "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
8263
#ifndef NO_CPU_EMULATION
8214
8264
{ "no-kvm", 0, QEMU_OPTION_no_kvm },
8215
8266
{ "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip },
8217
8268
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
8265
8316
{ "clock", HAS_ARG, QEMU_OPTION_clock },
8266
8317
{ "startdate", HAS_ARG, QEMU_OPTION_startdate },
8318
{ "hugetlb-path", HAS_ARG, QEMU_OPTION_hugetlbpath },
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);
8335
8391
#elif defined(TARGET_ARM)
8336
8392
qemu_register_machine(&integratorcp_machine);
8501
8567
*opt_incoming = incoming;
8571
static int gethugepagesize(void)
8575
char *needle = "Hugepagesize:";
8577
unsigned long hugepagesize;
8579
fd = open("/proc/meminfo", O_RDONLY);
8585
ret = read(fd, buf, sizeof(buf));
8591
size = strstr(buf, needle);
8594
size += strlen(needle);
8595
hugepagesize = strtol(size, NULL, 0);
8596
return hugepagesize;
8599
void cleanup_hugetlb(void)
8602
unlink(hugetlbfile);
8605
void *alloc_huge_area(unsigned long memory, const char *path)
8610
char *tmpfile = "/kvm.XXXXXX";
8612
filename = qemu_malloc(4096);
8616
memset(filename, 0, 4096);
8617
strncpy(filename, path, 4096 - strlen(tmpfile) - 1);
8618
strcat(filename, tmpfile);
8620
hpagesize = gethugepagesize() * 1024;
8625
fd = open(filename, O_RDWR);
8631
memory = (memory+hpagesize-1) & ~(hpagesize-1);
8633
area = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
8634
if (area == MAP_FAILED) {
8640
hugetlbfile = filename;
8641
atexit(cleanup_hugetlb);
8646
void *qemu_alloc_physram(unsigned long memory)
8651
area = alloc_huge_area(memory, hugetlbpath);
8653
area = qemu_vmalloc(memory);
8504
8658
int main(int argc, char **argv)
8506
8660
#ifdef CONFIG_GDBSTUB
8681
8835
case QEMU_OPTION_hda:
8683
hda_index = drive_add(HD_ALIAS, optarg, 0);
8837
hda_index = drive_add(optarg, HD_ALIAS, 0);
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 ?
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);
8698
8852
case QEMU_OPTION_drive:
8699
drive_add("%s", optarg);
8853
drive_add(NULL, "%s", optarg);
8701
8855
case QEMU_OPTION_mtdblock:
8702
drive_add(MTD_ALIAS, optarg);
8856
drive_add(optarg, MTD_ALIAS);
8704
8858
case QEMU_OPTION_sd:
8705
drive_add("file=\"%s\"," SD_ALIAS, optarg);
8859
drive_add(optarg, SD_ALIAS);
8707
8861
case QEMU_OPTION_pflash:
8708
drive_add(PFLASH_ALIAS, optarg);
8862
drive_add(optarg, PFLASH_ALIAS);
8710
8864
case QEMU_OPTION_snapshot:
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",
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 ?
8756
8908
translation == BIOS_ATA_TRANSLATION_NONE ?
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);
8814
8965
#ifdef TARGET_I386
8815
8966
case QEMU_OPTION_no_fd_bootchk:
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");
9215
9377
kvm_allowed = 0;
9311
9473
/* init the memory */
9312
9474
phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
9315
9476
/* Initialize kvm */
9477
#if defined(TARGET_I386) || defined(TARGET_X86_64)
9478
#define KVM_EXTRA_PAGES 3
9317
9480
#define KVM_EXTRA_PAGES 0
9319
#define KVM_EXTRA_PAGES 3
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");
9331
9492
ret = kvm_qemu_check_extension(KVM_CAP_USER_MEMORY);
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");
9349
phys_ram_base = qemu_vmalloc(phys_ram_size);
9350
if (!phys_ram_base) {
9351
fprintf(stderr, "Could not allocate physical memory\n");
9358
9512
/* we always create the cdrom drive, even if no disk is there */
9360
9514
if (nb_drives_opt < MAX_DRIVES)
9361
drive_add(CDROM_ALIAS);
9515
drive_add(NULL, CDROM_ALIAS);
9363
9517
/* we always create at least one floppy */
9365
9519
if (nb_drives_opt < MAX_DRIVES)
9366
drive_add(FD_ALIAS, 0);
9520
drive_add(NULL, FD_ALIAS, 0);
9368
9522
/* we always create one sd slot, even if no card is in it */
9370
9524
if (nb_drives_opt < MAX_DRIVES)
9371
drive_add(SD_ALIAS);
9525
drive_add(NULL, SD_ALIAS);
9373
9527
/* open the virtual block devices */
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)
9379
9533
register_savevm("timer", 0, 2, timer_save, timer_load, NULL);