2005-02-10 23:05:51 +01:00
|
|
|
/*
|
|
|
|
* KQEMU support
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2005-02-10 23:05:51 +01:00
|
|
|
* Copyright (c) 2005 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
2005-04-17 20:33:47 +02:00
|
|
|
#include <winioctl.h>
|
2005-02-10 23:05:51 +01:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
2005-04-17 20:33:47 +02:00
|
|
|
#include <sys/ioctl.h>
|
2005-02-10 23:05:51 +01:00
|
|
|
#endif
|
2007-01-18 00:31:19 +01:00
|
|
|
#ifdef HOST_SOLARIS
|
2007-03-30 21:20:50 +02:00
|
|
|
#include <sys/ioccom.h>
|
2007-01-18 00:31:19 +01:00
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "exec-all.h"
|
|
|
|
|
|
|
|
#ifdef USE_KQEMU
|
|
|
|
|
|
|
|
#define DEBUG
|
2005-08-21 11:30:12 +02:00
|
|
|
//#define PROFILE
|
2005-02-10 23:05:51 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2006-02-08 23:59:35 +01:00
|
|
|
#include "kqemu.h"
|
2005-02-10 23:05:51 +01:00
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
/* compatibility stuff */
|
|
|
|
#ifndef KQEMU_RET_SYSCALL
|
|
|
|
#define KQEMU_RET_SYSCALL 0x0300 /* syscall insn */
|
|
|
|
#endif
|
2005-08-21 11:30:12 +02:00
|
|
|
#ifndef KQEMU_MAX_RAM_PAGES_TO_UPDATE
|
|
|
|
#define KQEMU_MAX_RAM_PAGES_TO_UPDATE 512
|
|
|
|
#define KQEMU_RAM_PAGES_UPDATE_ALL (KQEMU_MAX_RAM_PAGES_TO_UPDATE + 1)
|
|
|
|
#endif
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifndef KQEMU_MAX_MODIFIED_RAM_PAGES
|
|
|
|
#define KQEMU_MAX_MODIFIED_RAM_PAGES 512
|
|
|
|
#endif
|
2005-04-23 19:45:43 +02:00
|
|
|
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define KQEMU_DEVICE "\\\\.\\kqemu"
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
#define KQEMU_DEVICE "/dev/kqemu"
|
2005-04-17 20:33:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define KQEMU_INVALID_FD INVALID_HANDLE_VALUE
|
|
|
|
HANDLE kqemu_fd = KQEMU_INVALID_FD;
|
|
|
|
#define kqemu_closefd(x) CloseHandle(x)
|
|
|
|
#else
|
|
|
|
#define KQEMU_INVALID_FD -1
|
|
|
|
int kqemu_fd = KQEMU_INVALID_FD;
|
|
|
|
#define kqemu_closefd(x) close(x)
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
/* 0 = not allowed
|
|
|
|
1 = user kqemu
|
|
|
|
2 = kernel kqemu
|
|
|
|
*/
|
2005-02-10 23:05:51 +01:00
|
|
|
int kqemu_allowed = 1;
|
|
|
|
unsigned long *pages_to_flush;
|
|
|
|
unsigned int nb_pages_to_flush;
|
2005-08-21 11:30:12 +02:00
|
|
|
unsigned long *ram_pages_to_update;
|
|
|
|
unsigned int nb_ram_pages_to_update;
|
2006-02-08 23:43:39 +01:00
|
|
|
unsigned long *modified_ram_pages;
|
|
|
|
unsigned int nb_modified_ram_pages;
|
|
|
|
uint8_t *modified_ram_pages_table;
|
2005-02-10 23:05:51 +01:00
|
|
|
extern uint32_t **l1_phys_map;
|
|
|
|
|
|
|
|
#define cpuid(index, eax, ebx, ecx, edx) \
|
|
|
|
asm volatile ("cpuid" \
|
|
|
|
: "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) \
|
|
|
|
: "0" (index))
|
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
static int is_cpuid_supported(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
static int is_cpuid_supported(void)
|
|
|
|
{
|
|
|
|
int v0, v1;
|
|
|
|
asm volatile ("pushf\n"
|
|
|
|
"popl %0\n"
|
|
|
|
"movl %0, %1\n"
|
|
|
|
"xorl $0x00200000, %0\n"
|
|
|
|
"pushl %0\n"
|
|
|
|
"popf\n"
|
|
|
|
"pushf\n"
|
|
|
|
"popl %0\n"
|
|
|
|
: "=a" (v0), "=d" (v1)
|
|
|
|
:
|
|
|
|
: "cc");
|
|
|
|
return (v0 != v1);
|
|
|
|
}
|
2005-04-23 19:45:43 +02:00
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
|
|
|
|
static void kqemu_update_cpuid(CPUState *env)
|
|
|
|
{
|
2006-07-10 21:53:43 +02:00
|
|
|
int critical_features_mask, features, ext_features, ext_features_mask;
|
2005-02-10 23:05:51 +01:00
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
|
|
|
|
/* the following features are kept identical on the host and
|
|
|
|
target cpus because they are important for user code. Strictly
|
|
|
|
speaking, only SSE really matters because the OS must support
|
|
|
|
it if the user code uses it. */
|
2007-09-16 23:08:06 +02:00
|
|
|
critical_features_mask =
|
|
|
|
CPUID_CMOV | CPUID_CX8 |
|
|
|
|
CPUID_FXSR | CPUID_MMX | CPUID_SSE |
|
2005-09-03 18:25:14 +02:00
|
|
|
CPUID_SSE2 | CPUID_SEP;
|
2006-07-10 21:53:43 +02:00
|
|
|
ext_features_mask = CPUID_EXT_SSE3 | CPUID_EXT_MONITOR;
|
2005-02-10 23:05:51 +01:00
|
|
|
if (!is_cpuid_supported()) {
|
|
|
|
features = 0;
|
2006-07-10 21:53:43 +02:00
|
|
|
ext_features = 0;
|
2005-02-10 23:05:51 +01:00
|
|
|
} else {
|
|
|
|
cpuid(1, eax, ebx, ecx, edx);
|
|
|
|
features = edx;
|
2006-07-10 21:53:43 +02:00
|
|
|
ext_features = ecx;
|
2005-02-10 23:05:51 +01:00
|
|
|
}
|
2005-09-03 18:25:14 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
/* NOTE: on x86_64 CPUs, SYSENTER is not supported in
|
|
|
|
compatibility mode, so in order to have the best performances
|
|
|
|
it is better not to use it */
|
|
|
|
features &= ~CPUID_SEP;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
env->cpuid_features = (env->cpuid_features & ~critical_features_mask) |
|
|
|
|
(features & critical_features_mask);
|
2006-07-10 21:53:43 +02:00
|
|
|
env->cpuid_ext_features = (env->cpuid_ext_features & ~ext_features_mask) |
|
|
|
|
(ext_features & ext_features_mask);
|
2005-02-10 23:05:51 +01:00
|
|
|
/* XXX: we could update more of the target CPUID state so that the
|
|
|
|
non accelerated code sees exactly the same CPU features as the
|
|
|
|
accelerated code */
|
|
|
|
}
|
|
|
|
|
|
|
|
int kqemu_init(CPUState *env)
|
|
|
|
{
|
|
|
|
struct kqemu_init init;
|
|
|
|
int ret, version;
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD temp;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
|
|
|
|
if (!kqemu_allowed)
|
|
|
|
return -1;
|
|
|
|
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
kqemu_fd = CreateFile(KQEMU_DEVICE, GENERIC_WRITE | GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
kqemu_fd = open(KQEMU_DEVICE, O_RDWR);
|
2005-04-17 20:33:47 +02:00
|
|
|
#endif
|
|
|
|
if (kqemu_fd == KQEMU_INVALID_FD) {
|
2007-03-25 18:14:07 +02:00
|
|
|
fprintf(stderr, "Could not open '%s' - QEMU acceleration layer not activated: %s\n",
|
|
|
|
KQEMU_DEVICE, strerror(errno));
|
2005-02-10 23:05:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
version = 0;
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
DeviceIoControl(kqemu_fd, KQEMU_GET_VERSION, NULL, 0,
|
|
|
|
&version, sizeof(version), &temp, NULL);
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
ioctl(kqemu_fd, KQEMU_GET_VERSION, &version);
|
2005-04-17 20:33:47 +02:00
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
if (version != KQEMU_VERSION) {
|
|
|
|
fprintf(stderr, "Version mismatch between kqemu module and qemu (%08x %08x) - disabling kqemu use\n",
|
|
|
|
version, KQEMU_VERSION);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
pages_to_flush = qemu_vmalloc(KQEMU_MAX_PAGES_TO_FLUSH *
|
2005-02-10 23:05:51 +01:00
|
|
|
sizeof(unsigned long));
|
|
|
|
if (!pages_to_flush)
|
|
|
|
goto fail;
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
ram_pages_to_update = qemu_vmalloc(KQEMU_MAX_RAM_PAGES_TO_UPDATE *
|
2005-08-21 11:30:12 +02:00
|
|
|
sizeof(unsigned long));
|
|
|
|
if (!ram_pages_to_update)
|
|
|
|
goto fail;
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
modified_ram_pages = qemu_vmalloc(KQEMU_MAX_MODIFIED_RAM_PAGES *
|
2006-02-08 23:43:39 +01:00
|
|
|
sizeof(unsigned long));
|
|
|
|
if (!modified_ram_pages)
|
|
|
|
goto fail;
|
|
|
|
modified_ram_pages_table = qemu_mallocz(phys_ram_size >> TARGET_PAGE_BITS);
|
|
|
|
if (!modified_ram_pages_table)
|
|
|
|
goto fail;
|
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
init.ram_base = phys_ram_base;
|
|
|
|
init.ram_size = phys_ram_size;
|
|
|
|
init.ram_dirty = phys_ram_dirty;
|
|
|
|
init.phys_to_ram_map = l1_phys_map;
|
|
|
|
init.pages_to_flush = pages_to_flush;
|
2005-08-21 11:30:12 +02:00
|
|
|
#if KQEMU_VERSION >= 0x010200
|
|
|
|
init.ram_pages_to_update = ram_pages_to_update;
|
|
|
|
#endif
|
2006-02-08 23:43:39 +01:00
|
|
|
#if KQEMU_VERSION >= 0x010300
|
|
|
|
init.modified_ram_pages = modified_ram_pages;
|
|
|
|
#endif
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
ret = DeviceIoControl(kqemu_fd, KQEMU_INIT, &init, sizeof(init),
|
|
|
|
NULL, 0, &temp, NULL) == TRUE ? 0 : -1;
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
ret = ioctl(kqemu_fd, KQEMU_INIT, &init);
|
2005-04-17 20:33:47 +02:00
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error %d while initializing QEMU acceleration layer - disabling it for now\n", ret);
|
|
|
|
fail:
|
2005-04-17 20:33:47 +02:00
|
|
|
kqemu_closefd(kqemu_fd);
|
|
|
|
kqemu_fd = KQEMU_INVALID_FD;
|
2005-02-10 23:05:51 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
kqemu_update_cpuid(env);
|
2006-02-08 23:43:39 +01:00
|
|
|
env->kqemu_enabled = kqemu_allowed;
|
2005-02-10 23:05:51 +01:00
|
|
|
nb_pages_to_flush = 0;
|
2005-08-21 11:30:12 +02:00
|
|
|
nb_ram_pages_to_update = 0;
|
2005-02-10 23:05:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kqemu_flush_page(CPUState *env, target_ulong addr)
|
|
|
|
{
|
2006-02-08 23:43:39 +01:00
|
|
|
#if defined(DEBUG)
|
2005-02-10 23:05:51 +01:00
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu_flush_page: addr=" TARGET_FMT_lx "\n", addr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (nb_pages_to_flush >= KQEMU_MAX_PAGES_TO_FLUSH)
|
|
|
|
nb_pages_to_flush = KQEMU_FLUSH_ALL;
|
|
|
|
else
|
|
|
|
pages_to_flush[nb_pages_to_flush++] = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kqemu_flush(CPUState *env, int global)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu_flush:\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
nb_pages_to_flush = KQEMU_FLUSH_ALL;
|
|
|
|
}
|
|
|
|
|
2005-08-21 11:30:12 +02:00
|
|
|
void kqemu_set_notdirty(CPUState *env, ram_addr_t ram_addr)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu_set_notdirty: addr=%08lx\n", ram_addr);
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-21 21:15:23 +02:00
|
|
|
/* we only track transitions to dirty state */
|
|
|
|
if (phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] != 0xff)
|
|
|
|
return;
|
2005-08-21 11:30:12 +02:00
|
|
|
if (nb_ram_pages_to_update >= KQEMU_MAX_RAM_PAGES_TO_UPDATE)
|
|
|
|
nb_ram_pages_to_update = KQEMU_RAM_PAGES_UPDATE_ALL;
|
|
|
|
else
|
|
|
|
ram_pages_to_update[nb_ram_pages_to_update++] = ram_addr;
|
|
|
|
}
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
static void kqemu_reset_modified_ram_pages(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long page_index;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
for(i = 0; i < nb_modified_ram_pages; i++) {
|
|
|
|
page_index = modified_ram_pages[i] >> TARGET_PAGE_BITS;
|
|
|
|
modified_ram_pages_table[page_index] = 0;
|
|
|
|
}
|
|
|
|
nb_modified_ram_pages = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kqemu_modify_page(CPUState *env, ram_addr_t ram_addr)
|
|
|
|
{
|
|
|
|
unsigned long page_index;
|
|
|
|
int ret;
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD temp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
page_index = ram_addr >> TARGET_PAGE_BITS;
|
|
|
|
if (!modified_ram_pages_table[page_index]) {
|
|
|
|
#if 0
|
|
|
|
printf("%d: modify_page=%08lx\n", nb_modified_ram_pages, ram_addr);
|
|
|
|
#endif
|
|
|
|
modified_ram_pages_table[page_index] = 1;
|
|
|
|
modified_ram_pages[nb_modified_ram_pages++] = ram_addr;
|
|
|
|
if (nb_modified_ram_pages >= KQEMU_MAX_MODIFIED_RAM_PAGES) {
|
|
|
|
/* flush */
|
|
|
|
#ifdef _WIN32
|
2007-09-16 23:08:06 +02:00
|
|
|
ret = DeviceIoControl(kqemu_fd, KQEMU_MODIFY_RAM_PAGES,
|
|
|
|
&nb_modified_ram_pages,
|
2006-02-08 23:43:39 +01:00
|
|
|
sizeof(nb_modified_ram_pages),
|
|
|
|
NULL, 0, &temp, NULL);
|
|
|
|
#else
|
2007-09-16 23:08:06 +02:00
|
|
|
ret = ioctl(kqemu_fd, KQEMU_MODIFY_RAM_PAGES,
|
2006-02-08 23:43:39 +01:00
|
|
|
&nb_modified_ram_pages);
|
|
|
|
#endif
|
|
|
|
kqemu_reset_modified_ram_pages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
struct fpstate {
|
|
|
|
uint16_t fpuc;
|
|
|
|
uint16_t dummy1;
|
|
|
|
uint16_t fpus;
|
|
|
|
uint16_t dummy2;
|
|
|
|
uint16_t fptag;
|
|
|
|
uint16_t dummy3;
|
|
|
|
|
|
|
|
uint32_t fpip;
|
|
|
|
uint32_t fpcs;
|
|
|
|
uint32_t fpoo;
|
|
|
|
uint32_t fpos;
|
|
|
|
uint8_t fpregs1[8 * 10];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fpxstate {
|
|
|
|
uint16_t fpuc;
|
|
|
|
uint16_t fpus;
|
|
|
|
uint16_t fptag;
|
|
|
|
uint16_t fop;
|
|
|
|
uint32_t fpuip;
|
|
|
|
uint16_t cs_sel;
|
|
|
|
uint16_t dummy0;
|
|
|
|
uint32_t fpudp;
|
|
|
|
uint16_t ds_sel;
|
|
|
|
uint16_t dummy1;
|
|
|
|
uint32_t mxcsr;
|
|
|
|
uint32_t mxcsr_mask;
|
|
|
|
uint8_t fpregs1[8 * 16];
|
2005-04-23 19:45:43 +02:00
|
|
|
uint8_t xmm_regs[16 * 16];
|
|
|
|
uint8_t dummy2[96];
|
2005-02-10 23:05:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct fpxstate fpx1 __attribute__((aligned(16)));
|
|
|
|
|
|
|
|
static void restore_native_fp_frstor(CPUState *env)
|
|
|
|
{
|
|
|
|
int fptag, i, j;
|
|
|
|
struct fpstate fp1, *fp = &fp1;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
fp->fpuc = env->fpuc;
|
|
|
|
fp->fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
|
|
|
|
fptag = 0;
|
|
|
|
for (i=7; i>=0; i--) {
|
|
|
|
fptag <<= 2;
|
|
|
|
if (env->fptags[i]) {
|
|
|
|
fptag |= 3;
|
|
|
|
} else {
|
|
|
|
/* the FPU automatically computes it */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fp->fptag = fptag;
|
|
|
|
j = env->fpstt;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
memcpy(&fp->fpregs1[i * 10], &env->fpregs[j].d, 10);
|
|
|
|
j = (j + 1) & 7;
|
|
|
|
}
|
|
|
|
asm volatile ("frstor %0" : "=m" (*fp));
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
static void save_native_fp_fsave(CPUState *env)
|
|
|
|
{
|
|
|
|
int fptag, i, j;
|
|
|
|
uint16_t fpuc;
|
|
|
|
struct fpstate fp1, *fp = &fp1;
|
|
|
|
|
|
|
|
asm volatile ("fsave %0" : : "m" (*fp));
|
|
|
|
env->fpuc = fp->fpuc;
|
|
|
|
env->fpstt = (fp->fpus >> 11) & 7;
|
|
|
|
env->fpus = fp->fpus & ~0x3800;
|
|
|
|
fptag = fp->fptag;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
env->fptags[i] = ((fptag & 3) == 3);
|
|
|
|
fptag >>= 2;
|
|
|
|
}
|
|
|
|
j = env->fpstt;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
memcpy(&env->fpregs[j].d, &fp->fpregs1[i * 10], 10);
|
|
|
|
j = (j + 1) & 7;
|
|
|
|
}
|
|
|
|
/* we must restore the default rounding state */
|
|
|
|
fpuc = 0x037f | (env->fpuc & (3 << 10));
|
|
|
|
asm volatile("fldcw %0" : : "m" (fpuc));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_native_fp_fxrstor(CPUState *env)
|
|
|
|
{
|
|
|
|
struct fpxstate *fp = &fpx1;
|
|
|
|
int i, j, fptag;
|
|
|
|
|
|
|
|
fp->fpuc = env->fpuc;
|
|
|
|
fp->fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
|
|
|
|
fptag = 0;
|
|
|
|
for(i = 0; i < 8; i++)
|
|
|
|
fptag |= (env->fptags[i] << i);
|
|
|
|
fp->fptag = fptag ^ 0xff;
|
|
|
|
|
|
|
|
j = env->fpstt;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
memcpy(&fp->fpregs1[i * 16], &env->fpregs[j].d, 10);
|
|
|
|
j = (j + 1) & 7;
|
|
|
|
}
|
|
|
|
if (env->cpuid_features & CPUID_SSE) {
|
|
|
|
fp->mxcsr = env->mxcsr;
|
|
|
|
/* XXX: check if DAZ is not available */
|
|
|
|
fp->mxcsr_mask = 0xffff;
|
2005-04-23 19:45:43 +02:00
|
|
|
memcpy(fp->xmm_regs, env->xmm_regs, CPU_NB_REGS * 16);
|
2005-02-10 23:05:51 +01:00
|
|
|
}
|
|
|
|
asm volatile ("fxrstor %0" : "=m" (*fp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save_native_fp_fxsave(CPUState *env)
|
|
|
|
{
|
|
|
|
struct fpxstate *fp = &fpx1;
|
|
|
|
int fptag, i, j;
|
|
|
|
uint16_t fpuc;
|
|
|
|
|
|
|
|
asm volatile ("fxsave %0" : : "m" (*fp));
|
|
|
|
env->fpuc = fp->fpuc;
|
|
|
|
env->fpstt = (fp->fpus >> 11) & 7;
|
|
|
|
env->fpus = fp->fpus & ~0x3800;
|
|
|
|
fptag = fp->fptag ^ 0xff;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
env->fptags[i] = (fptag >> i) & 1;
|
|
|
|
}
|
|
|
|
j = env->fpstt;
|
|
|
|
for(i = 0;i < 8; i++) {
|
|
|
|
memcpy(&env->fpregs[j].d, &fp->fpregs1[i * 16], 10);
|
|
|
|
j = (j + 1) & 7;
|
|
|
|
}
|
|
|
|
if (env->cpuid_features & CPUID_SSE) {
|
|
|
|
env->mxcsr = fp->mxcsr;
|
2005-04-23 19:45:43 +02:00
|
|
|
memcpy(env->xmm_regs, fp->xmm_regs, CPU_NB_REGS * 16);
|
2005-02-10 23:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we must restore the default rounding state */
|
|
|
|
asm volatile ("fninit");
|
|
|
|
fpuc = 0x037f | (env->fpuc & (3 << 10));
|
|
|
|
asm volatile("fldcw %0" : : "m" (fpuc));
|
|
|
|
}
|
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
static int do_syscall(CPUState *env,
|
|
|
|
struct kqemu_cpu_state *kenv)
|
|
|
|
{
|
|
|
|
int selector;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
selector = (env->star >> 32) & 0xffff;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
2006-10-02 19:58:33 +02:00
|
|
|
int code64;
|
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
env->regs[R_ECX] = kenv->next_eip;
|
|
|
|
env->regs[11] = env->eflags;
|
|
|
|
|
2006-10-02 19:58:33 +02:00
|
|
|
code64 = env->hflags & HF_CS64_MASK;
|
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
cpu_x86_set_cpl(env, 0);
|
2007-09-16 23:08:06 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
2007-02-01 23:12:43 +01:00
|
|
|
DESC_G_MASK | DESC_P_MASK |
|
2005-04-23 19:45:43 +02:00
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | DESC_L_MASK);
|
2007-09-16 23:08:06 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
|
2005-04-23 19:45:43 +02:00
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
env->eflags &= ~env->fmask;
|
2006-10-02 19:58:33 +02:00
|
|
|
if (code64)
|
2005-04-23 19:45:43 +02:00
|
|
|
env->eip = env->lstar;
|
|
|
|
else
|
|
|
|
env->eip = env->cstar;
|
2007-09-16 23:08:06 +02:00
|
|
|
} else
|
2005-04-23 19:45:43 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
env->regs[R_ECX] = (uint32_t)kenv->next_eip;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2005-04-23 19:45:43 +02:00
|
|
|
cpu_x86_set_cpl(env, 0);
|
2007-09-16 23:08:06 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
2005-04-23 19:45:43 +02:00
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
2007-09-16 23:08:06 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
|
2005-04-23 19:45:43 +02:00
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
|
|
|
|
env->eip = (uint32_t)env->star;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
2005-08-21 11:30:12 +02:00
|
|
|
|
|
|
|
#define PC_REC_SIZE 1
|
|
|
|
#define PC_REC_HASH_BITS 16
|
|
|
|
#define PC_REC_HASH_SIZE (1 << PC_REC_HASH_BITS)
|
|
|
|
|
|
|
|
typedef struct PCRecord {
|
|
|
|
unsigned long pc;
|
|
|
|
int64_t count;
|
|
|
|
struct PCRecord *next;
|
|
|
|
} PCRecord;
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
static PCRecord *pc_rec_hash[PC_REC_HASH_SIZE];
|
|
|
|
static int nb_pc_records;
|
2005-08-21 11:30:12 +02:00
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
static void kqemu_record_pc(unsigned long pc)
|
2005-08-21 11:30:12 +02:00
|
|
|
{
|
|
|
|
unsigned long h;
|
|
|
|
PCRecord **pr, *r;
|
|
|
|
|
|
|
|
h = pc / PC_REC_SIZE;
|
|
|
|
h = h ^ (h >> PC_REC_HASH_BITS);
|
|
|
|
h &= (PC_REC_HASH_SIZE - 1);
|
|
|
|
pr = &pc_rec_hash[h];
|
|
|
|
for(;;) {
|
|
|
|
r = *pr;
|
|
|
|
if (r == NULL)
|
|
|
|
break;
|
|
|
|
if (r->pc == pc) {
|
|
|
|
r->count++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pr = &r->next;
|
|
|
|
}
|
|
|
|
r = malloc(sizeof(PCRecord));
|
|
|
|
r->count = 1;
|
|
|
|
r->pc = pc;
|
|
|
|
r->next = NULL;
|
|
|
|
*pr = r;
|
|
|
|
nb_pc_records++;
|
|
|
|
}
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
static int pc_rec_cmp(const void *p1, const void *p2)
|
2005-08-21 11:30:12 +02:00
|
|
|
{
|
|
|
|
PCRecord *r1 = *(PCRecord **)p1;
|
|
|
|
PCRecord *r2 = *(PCRecord **)p2;
|
|
|
|
if (r1->count < r2->count)
|
|
|
|
return 1;
|
|
|
|
else if (r1->count == r2->count)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
static void kqemu_record_flush(void)
|
|
|
|
{
|
|
|
|
PCRecord *r, *r_next;
|
|
|
|
int h;
|
|
|
|
|
|
|
|
for(h = 0; h < PC_REC_HASH_SIZE; h++) {
|
|
|
|
for(r = pc_rec_hash[h]; r != NULL; r = r_next) {
|
|
|
|
r_next = r->next;
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
pc_rec_hash[h] = NULL;
|
|
|
|
}
|
|
|
|
nb_pc_records = 0;
|
|
|
|
}
|
|
|
|
|
2005-08-21 11:30:12 +02:00
|
|
|
void kqemu_record_dump(void)
|
|
|
|
{
|
|
|
|
PCRecord **pr, *r;
|
|
|
|
int i, h;
|
|
|
|
FILE *f;
|
|
|
|
int64_t total, sum;
|
|
|
|
|
|
|
|
pr = malloc(sizeof(PCRecord *) * nb_pc_records);
|
|
|
|
i = 0;
|
|
|
|
total = 0;
|
|
|
|
for(h = 0; h < PC_REC_HASH_SIZE; h++) {
|
|
|
|
for(r = pc_rec_hash[h]; r != NULL; r = r->next) {
|
|
|
|
pr[i++] = r;
|
|
|
|
total += r->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qsort(pr, nb_pc_records, sizeof(PCRecord *), pc_rec_cmp);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2005-08-21 11:30:12 +02:00
|
|
|
f = fopen("/tmp/kqemu.stats", "w");
|
|
|
|
if (!f) {
|
|
|
|
perror("/tmp/kqemu.stats");
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-06-25 20:15:32 +02:00
|
|
|
fprintf(f, "total: %" PRId64 "\n", total);
|
2005-08-21 11:30:12 +02:00
|
|
|
sum = 0;
|
|
|
|
for(i = 0; i < nb_pc_records; i++) {
|
|
|
|
r = pr[i];
|
|
|
|
sum += r->count;
|
2007-09-16 23:08:06 +02:00
|
|
|
fprintf(f, "%08lx: %" PRId64 " %0.2f%% %0.2f%%\n",
|
|
|
|
r->pc,
|
|
|
|
r->count,
|
2005-08-21 11:30:12 +02:00
|
|
|
(double)r->count / (double)total * 100.0,
|
|
|
|
(double)sum / (double)total * 100.0);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
free(pr);
|
2006-02-08 23:43:39 +01:00
|
|
|
|
|
|
|
kqemu_record_flush();
|
2005-08-21 11:30:12 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
int kqemu_cpu_exec(CPUState *env)
|
|
|
|
{
|
|
|
|
struct kqemu_cpu_state kcpu_state, *kenv = &kcpu_state;
|
2006-02-08 23:43:39 +01:00
|
|
|
int ret, cpl, i;
|
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
int64_t ti;
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD temp;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
ti = profile_getclock();
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu: cpu_exec: enter\n");
|
|
|
|
cpu_dump_state(env, logfile, fprintf, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
memcpy(kenv->regs, env->regs, sizeof(kenv->regs));
|
|
|
|
kenv->eip = env->eip;
|
|
|
|
kenv->eflags = env->eflags;
|
|
|
|
memcpy(&kenv->segs, &env->segs, sizeof(env->segs));
|
|
|
|
memcpy(&kenv->ldt, &env->ldt, sizeof(env->ldt));
|
|
|
|
memcpy(&kenv->tr, &env->tr, sizeof(env->tr));
|
|
|
|
memcpy(&kenv->gdt, &env->gdt, sizeof(env->gdt));
|
|
|
|
memcpy(&kenv->idt, &env->idt, sizeof(env->idt));
|
|
|
|
kenv->cr0 = env->cr[0];
|
|
|
|
kenv->cr2 = env->cr[2];
|
|
|
|
kenv->cr3 = env->cr[3];
|
|
|
|
kenv->cr4 = env->cr[4];
|
|
|
|
kenv->a20_mask = env->a20_mask;
|
2005-04-24 20:03:37 +02:00
|
|
|
#if KQEMU_VERSION >= 0x010100
|
2005-04-23 19:45:43 +02:00
|
|
|
kenv->efer = env->efer;
|
2006-02-08 23:43:39 +01:00
|
|
|
#endif
|
|
|
|
#if KQEMU_VERSION >= 0x010300
|
|
|
|
kenv->tsc_offset = 0;
|
|
|
|
kenv->star = env->star;
|
|
|
|
kenv->sysenter_cs = env->sysenter_cs;
|
|
|
|
kenv->sysenter_esp = env->sysenter_esp;
|
|
|
|
kenv->sysenter_eip = env->sysenter_eip;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
kenv->lstar = env->lstar;
|
|
|
|
kenv->cstar = env->cstar;
|
|
|
|
kenv->fmask = env->fmask;
|
|
|
|
kenv->kernelgsbase = env->kernelgsbase;
|
|
|
|
#endif
|
2005-04-23 19:45:43 +02:00
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
if (env->dr[7] & 0xff) {
|
|
|
|
kenv->dr7 = env->dr[7];
|
|
|
|
kenv->dr0 = env->dr[0];
|
|
|
|
kenv->dr1 = env->dr[1];
|
|
|
|
kenv->dr2 = env->dr[2];
|
|
|
|
kenv->dr3 = env->dr[3];
|
|
|
|
} else {
|
|
|
|
kenv->dr7 = 0;
|
|
|
|
}
|
|
|
|
kenv->dr6 = env->dr[6];
|
2006-02-08 23:43:39 +01:00
|
|
|
cpl = (env->hflags & HF_CPL_MASK);
|
|
|
|
kenv->cpl = cpl;
|
2005-02-10 23:05:51 +01:00
|
|
|
kenv->nb_pages_to_flush = nb_pages_to_flush;
|
2005-08-21 11:30:12 +02:00
|
|
|
#if KQEMU_VERSION >= 0x010200
|
2006-02-08 23:43:39 +01:00
|
|
|
kenv->user_only = (env->kqemu_enabled == 1);
|
2005-08-21 11:30:12 +02:00
|
|
|
kenv->nb_ram_pages_to_update = nb_ram_pages_to_update;
|
|
|
|
#endif
|
|
|
|
nb_ram_pages_to_update = 0;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
#if KQEMU_VERSION >= 0x010300
|
|
|
|
kenv->nb_modified_ram_pages = nb_modified_ram_pages;
|
|
|
|
#endif
|
|
|
|
kqemu_reset_modified_ram_pages();
|
|
|
|
|
|
|
|
if (env->cpuid_features & CPUID_FXSR)
|
|
|
|
restore_native_fp_fxrstor(env);
|
|
|
|
else
|
|
|
|
restore_native_fp_frstor(env);
|
2005-02-10 23:05:51 +01:00
|
|
|
|
2005-04-17 20:33:47 +02:00
|
|
|
#ifdef _WIN32
|
2005-09-03 19:55:47 +02:00
|
|
|
if (DeviceIoControl(kqemu_fd, KQEMU_EXEC,
|
|
|
|
kenv, sizeof(struct kqemu_cpu_state),
|
|
|
|
kenv, sizeof(struct kqemu_cpu_state),
|
|
|
|
&temp, NULL)) {
|
|
|
|
ret = kenv->retval;
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
2005-04-17 20:33:47 +02:00
|
|
|
#else
|
|
|
|
#if KQEMU_VERSION >= 0x010100
|
|
|
|
ioctl(kqemu_fd, KQEMU_EXEC, kenv);
|
|
|
|
ret = kenv->retval;
|
|
|
|
#else
|
2005-02-10 23:05:51 +01:00
|
|
|
ret = ioctl(kqemu_fd, KQEMU_EXEC, kenv);
|
2005-04-17 20:33:47 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2006-02-08 23:43:39 +01:00
|
|
|
if (env->cpuid_features & CPUID_FXSR)
|
|
|
|
save_native_fp_fxsave(env);
|
|
|
|
else
|
|
|
|
save_native_fp_fsave(env);
|
2005-02-10 23:05:51 +01:00
|
|
|
|
|
|
|
memcpy(env->regs, kenv->regs, sizeof(env->regs));
|
|
|
|
env->eip = kenv->eip;
|
|
|
|
env->eflags = kenv->eflags;
|
|
|
|
memcpy(env->segs, kenv->segs, sizeof(env->segs));
|
2006-02-08 23:43:39 +01:00
|
|
|
cpu_x86_set_cpl(env, kenv->cpl);
|
|
|
|
memcpy(&env->ldt, &kenv->ldt, sizeof(env->ldt));
|
2005-02-10 23:05:51 +01:00
|
|
|
#if 0
|
|
|
|
/* no need to restore that */
|
|
|
|
memcpy(env->tr, kenv->tr, sizeof(env->tr));
|
|
|
|
memcpy(env->gdt, kenv->gdt, sizeof(env->gdt));
|
|
|
|
memcpy(env->idt, kenv->idt, sizeof(env->idt));
|
|
|
|
env->a20_mask = kenv->a20_mask;
|
|
|
|
#endif
|
2006-02-08 23:43:39 +01:00
|
|
|
env->cr[0] = kenv->cr0;
|
|
|
|
env->cr[4] = kenv->cr4;
|
|
|
|
env->cr[3] = kenv->cr3;
|
2005-02-10 23:05:51 +01:00
|
|
|
env->cr[2] = kenv->cr2;
|
|
|
|
env->dr[6] = kenv->dr6;
|
2006-02-08 23:43:39 +01:00
|
|
|
#if KQEMU_VERSION >= 0x010300
|
|
|
|
#ifdef __x86_64__
|
|
|
|
env->kernelgsbase = kenv->kernelgsbase;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* flush pages as indicated by kqemu */
|
|
|
|
if (kenv->nb_pages_to_flush >= KQEMU_FLUSH_ALL) {
|
|
|
|
tlb_flush(env, 1);
|
|
|
|
} else {
|
|
|
|
for(i = 0; i < kenv->nb_pages_to_flush; i++) {
|
|
|
|
tlb_flush_page(env, pages_to_flush[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nb_pages_to_flush = 0;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
kqemu_time += profile_getclock() - ti;
|
|
|
|
kqemu_exec_count++;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
|
2005-08-21 11:30:12 +02:00
|
|
|
#if KQEMU_VERSION >= 0x010200
|
|
|
|
if (kenv->nb_ram_pages_to_update > 0) {
|
|
|
|
cpu_tlb_update_dirty(env);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-08 23:43:39 +01:00
|
|
|
#if KQEMU_VERSION >= 0x010300
|
|
|
|
if (kenv->nb_modified_ram_pages > 0) {
|
|
|
|
for(i = 0; i < kenv->nb_modified_ram_pages; i++) {
|
|
|
|
unsigned long addr;
|
|
|
|
addr = modified_ram_pages[i];
|
|
|
|
tb_invalidate_phys_page_range(addr, addr + TARGET_PAGE_SIZE, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-21 11:30:12 +02:00
|
|
|
/* restore the hidden flags */
|
|
|
|
{
|
|
|
|
unsigned int new_hflags;
|
|
|
|
#ifdef TARGET_X86_64
|
2007-09-16 23:08:06 +02:00
|
|
|
if ((env->hflags & HF_LMA_MASK) &&
|
2005-08-21 11:30:12 +02:00
|
|
|
(env->segs[R_CS].flags & DESC_L_MASK)) {
|
|
|
|
/* long mode */
|
|
|
|
new_hflags = HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* legacy / compatibility case */
|
|
|
|
new_hflags = (env->segs[R_CS].flags & DESC_B_MASK)
|
|
|
|
>> (DESC_B_SHIFT - HF_CS32_SHIFT);
|
|
|
|
new_hflags |= (env->segs[R_SS].flags & DESC_B_MASK)
|
|
|
|
>> (DESC_B_SHIFT - HF_SS32_SHIFT);
|
2007-09-16 23:08:06 +02:00
|
|
|
if (!(env->cr[0] & CR0_PE_MASK) ||
|
2005-08-21 11:30:12 +02:00
|
|
|
(env->eflags & VM_MASK) ||
|
|
|
|
!(env->hflags & HF_CS32_MASK)) {
|
|
|
|
/* XXX: try to avoid this test. The problem comes from the
|
|
|
|
fact that is real mode or vm86 mode we only modify the
|
|
|
|
'base' and 'selector' fields of the segment cache to go
|
|
|
|
faster. A solution may be to force addseg to one in
|
|
|
|
translate-i386.c. */
|
|
|
|
new_hflags |= HF_ADDSEG_MASK;
|
|
|
|
} else {
|
2007-09-16 23:08:06 +02:00
|
|
|
new_hflags |= ((env->segs[R_DS].base |
|
2005-08-21 11:30:12 +02:00
|
|
|
env->segs[R_ES].base |
|
2007-09-16 23:08:06 +02:00
|
|
|
env->segs[R_SS].base) != 0) <<
|
2005-08-21 11:30:12 +02:00
|
|
|
HF_ADDSEG_SHIFT;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
env->hflags = (env->hflags &
|
2005-08-21 11:30:12 +02:00
|
|
|
~(HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)) |
|
|
|
|
new_hflags;
|
|
|
|
}
|
2006-02-08 23:43:39 +01:00
|
|
|
/* update FPU flags */
|
|
|
|
env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
|
|
|
|
((env->cr[0] << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
|
|
|
|
if (env->cr[4] & CR4_OSFXSR_MASK)
|
|
|
|
env->hflags |= HF_OSFXSR_MASK;
|
|
|
|
else
|
|
|
|
env->hflags &= ~HF_OSFXSR_MASK;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu: kqemu_cpu_exec: ret=0x%x\n", ret);
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-23 19:45:43 +02:00
|
|
|
if (ret == KQEMU_RET_SYSCALL) {
|
|
|
|
/* syscall instruction */
|
|
|
|
return do_syscall(env, kenv);
|
2007-09-16 23:08:06 +02:00
|
|
|
} else
|
2005-02-10 23:05:51 +01:00
|
|
|
if ((ret & 0xff00) == KQEMU_RET_INT) {
|
|
|
|
env->exception_index = ret & 0xff;
|
|
|
|
env->error_code = 0;
|
|
|
|
env->exception_is_int = 1;
|
|
|
|
env->exception_next_eip = kenv->next_eip;
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
kqemu_ret_int_count++;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
#ifdef DEBUG
|
2005-04-23 19:45:43 +02:00
|
|
|
if (loglevel & CPU_LOG_INT) {
|
2007-09-16 23:08:06 +02:00
|
|
|
fprintf(logfile, "kqemu: interrupt v=%02x:\n",
|
2005-04-23 19:45:43 +02:00
|
|
|
env->exception_index);
|
|
|
|
cpu_dump_state(env, logfile, fprintf, 0);
|
|
|
|
}
|
2005-02-10 23:05:51 +01:00
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
} else if ((ret & 0xff00) == KQEMU_RET_EXCEPTION) {
|
|
|
|
env->exception_index = ret & 0xff;
|
|
|
|
env->error_code = kenv->error_code;
|
|
|
|
env->exception_is_int = 0;
|
|
|
|
env->exception_next_eip = 0;
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
kqemu_ret_excp_count++;
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
fprintf(logfile, "kqemu: exception v=%02x e=%04x:\n",
|
|
|
|
env->exception_index, env->error_code);
|
|
|
|
cpu_dump_state(env, logfile, fprintf, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
} else if (ret == KQEMU_RET_INTR) {
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
kqemu_ret_intr_count++;
|
|
|
|
#endif
|
2005-04-24 20:03:37 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
cpu_dump_state(env, logfile, fprintf, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
return 0;
|
2007-09-16 23:08:06 +02:00
|
|
|
} else if (ret == KQEMU_RET_SOFTMMU) {
|
2006-02-08 23:43:39 +01:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
{
|
|
|
|
unsigned long pc = env->eip + env->segs[R_CS].base;
|
|
|
|
kqemu_record_pc(pc);
|
|
|
|
}
|
2005-08-21 11:30:12 +02:00
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (loglevel & CPU_LOG_INT) {
|
|
|
|
cpu_dump_state(env, logfile, fprintf, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2005-02-10 23:05:51 +01:00
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
cpu_dump_state(env, stderr, fprintf, 0);
|
|
|
|
fprintf(stderr, "Unsupported return value: 0x%x\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-03 19:55:47 +02:00
|
|
|
void kqemu_cpu_interrupt(CPUState *env)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && KQEMU_VERSION >= 0x010101
|
2007-09-16 23:08:06 +02:00
|
|
|
/* cancelling the I/O request causes KQEMU to finish executing the
|
2005-09-03 19:55:47 +02:00
|
|
|
current block and successfully returning. */
|
|
|
|
CancelIo(kqemu_fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-02-10 23:05:51 +01:00
|
|
|
#endif
|