2008-05-28 14:51:20 +02:00
|
|
|
/*
|
2012-04-29 18:39:13 +02:00
|
|
|
* x86 segmentation related helpers:
|
|
|
|
* TSS, interrupts, system calls, jumps and call/task gates, descriptors
|
2008-05-28 14:51:20 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2003 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
|
2009-07-16 22:47:01 +02:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2008-05-28 14:51:20 +02:00
|
|
|
*/
|
2010-06-29 09:58:49 +02:00
|
|
|
|
2011-07-13 14:44:15 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "qemu-log.h"
|
|
|
|
#include "helper.h"
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2011-07-13 14:44:15 +02:00
|
|
|
//#define DEBUG_PCALL
|
2009-01-15 22:48:06 +01:00
|
|
|
|
2012-04-29 22:35:48 +02:00
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
#include "softmmu_exec.h"
|
|
|
|
#endif /* !defined(CONFIG_USER_ONLY) */
|
|
|
|
|
2009-01-15 22:48:06 +01:00
|
|
|
#ifdef DEBUG_PCALL
|
2012-04-28 17:33:48 +02:00
|
|
|
# define LOG_PCALL(...) qemu_log_mask(CPU_LOG_PCALL, ## __VA_ARGS__)
|
|
|
|
# define LOG_PCALL_STATE(env) \
|
2012-10-05 16:04:43 +02:00
|
|
|
log_cpu_state_mask(CPU_LOG_PCALL, (env), CPU_DUMP_CCOP)
|
2009-01-15 22:48:06 +01:00
|
|
|
#else
|
2012-04-28 17:33:48 +02:00
|
|
|
# define LOG_PCALL(...) do { } while (0)
|
|
|
|
# define LOG_PCALL_STATE(env) do { } while (0)
|
2009-01-15 22:48:06 +01:00
|
|
|
#endif
|
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
/* return non zero if error */
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline int load_segment(CPUX86State *env, uint32_t *e1_ptr,
|
|
|
|
uint32_t *e2_ptr, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
int index;
|
|
|
|
target_ulong ptr;
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (selector & 0x4) {
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->ldt;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->gdt;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
index = selector & ~7;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((index + 7) > dt->limit) {
|
2008-05-28 14:51:20 +02:00
|
|
|
return -1;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + index;
|
2012-04-29 21:11:01 +02:00
|
|
|
*e1_ptr = cpu_ldl_kernel(env, ptr);
|
|
|
|
*e2_ptr = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
|
|
|
|
{
|
|
|
|
unsigned int limit;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
limit = (e1 & 0xffff) | (e2 & 0x000f0000);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (e2 & DESC_G_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
limit = (limit << 12) | 0xfff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2)
|
|
|
|
{
|
2012-04-28 17:33:48 +02:00
|
|
|
return (e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1,
|
|
|
|
uint32_t e2)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
sc->base = get_seg_base(e1, e2);
|
|
|
|
sc->limit = get_seg_limit(e1, e2);
|
|
|
|
sc->flags = e2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init the segment cache in vm86 mode. */
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline void load_seg_vm(CPUX86State *env, int seg, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
selector &= 0xffff;
|
|
|
|
cpu_x86_load_seg_cache(env, seg, selector,
|
|
|
|
(selector << 4), 0xffff, 0);
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
|
2008-05-28 14:51:20 +02:00
|
|
|
uint32_t *esp_ptr, int dpl)
|
|
|
|
{
|
|
|
|
int type, index, shift;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < env->tr.limit; i++) {
|
2008-05-28 14:51:20 +02:00
|
|
|
printf("%02x ", env->tr.base[i]);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((i & 7) == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(env->tr.flags & DESC_P_MASK)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_abort(env, "invalid tss");
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((type & 7) != 1) {
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_abort(env, "invalid tss type");
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
shift = type >> 3;
|
|
|
|
index = (dpl * 4 + 2) << shift;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (index + (4 << shift) - 1 > env->tr.limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (shift == 0) {
|
2012-04-29 21:11:01 +02:00
|
|
|
*esp_ptr = cpu_lduw_kernel(env, env->tr.base + index);
|
|
|
|
*ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 2);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
2012-04-29 21:11:01 +02:00
|
|
|
*esp_ptr = cpu_ldl_kernel(env, env->tr.base + index);
|
|
|
|
*ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: merge with load_seg() */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void tss_load_seg(CPUX86State *env, int seg_reg, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t e1, e2;
|
|
|
|
int rpl, dpl, cpl;
|
|
|
|
|
|
|
|
if ((selector & 0xfffc) != 0) {
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (seg_reg == R_CS) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_CS_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
/* XXX: is it correct? */
|
|
|
|
if (dpl != rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if ((e2 & DESC_C_MASK) && dpl > rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else if (seg_reg == R_SS) {
|
|
|
|
/* SS must be writable data */
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (dpl != cpl || dpl != rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* not readable code */
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* if data or non conforming code, checks the rights */
|
|
|
|
if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, selector,
|
2012-04-28 17:33:48 +02:00
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (seg_reg == R_SS || seg_reg == R_CS) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SWITCH_TSS_JMP 0
|
|
|
|
#define SWITCH_TSS_IRET 1
|
|
|
|
#define SWITCH_TSS_CALL 2
|
|
|
|
|
|
|
|
/* XXX: restore CPU state in registers (PowerPC case) */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void switch_tss(CPUX86State *env, int tss_selector,
|
2008-05-28 14:51:20 +02:00
|
|
|
uint32_t e1, uint32_t e2, int source,
|
|
|
|
uint32_t next_eip)
|
|
|
|
{
|
|
|
|
int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
|
|
|
|
target_ulong tss_base;
|
|
|
|
uint32_t new_regs[8], new_segs[6];
|
|
|
|
uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
|
|
|
|
uint32_t old_eflags, eflags_mask;
|
|
|
|
SegmentCache *dt;
|
|
|
|
int index;
|
|
|
|
target_ulong ptr;
|
|
|
|
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type,
|
|
|
|
source);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* if task gate, we read the TSS segment and we load it */
|
|
|
|
if (type == 5) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
tss_selector = e1 >> 16;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (tss_selector & 4) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, tss_selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (e2 & DESC_S_MASK) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((type & 7) != 1) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (type & 8) {
|
2008-05-28 14:51:20 +02:00
|
|
|
tss_limit_max = 103;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
tss_limit_max = 43;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
tss_limit = get_seg_limit(e1, e2);
|
|
|
|
tss_base = get_seg_base(e1, e2);
|
|
|
|
if ((tss_selector & 4) != 0 ||
|
2012-04-28 17:33:48 +02:00
|
|
|
tss_limit < tss_limit_max) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (old_type & 8) {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_tss_limit_max = 103;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_tss_limit_max = 43;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* read all the registers from the new TSS */
|
|
|
|
if (type & 8) {
|
|
|
|
/* 32 bit */
|
2012-04-29 21:11:01 +02:00
|
|
|
new_cr3 = cpu_ldl_kernel(env, tss_base + 0x1c);
|
|
|
|
new_eip = cpu_ldl_kernel(env, tss_base + 0x20);
|
|
|
|
new_eflags = cpu_ldl_kernel(env, tss_base + 0x24);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 8; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
new_regs[i] = cpu_ldl_kernel(env, tss_base + (0x28 + i * 4));
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < 6; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x48 + i * 4));
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:11:01 +02:00
|
|
|
new_ldt = cpu_lduw_kernel(env, tss_base + 0x60);
|
|
|
|
new_trap = cpu_ldl_kernel(env, tss_base + 0x64);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* 16 bit */
|
|
|
|
new_cr3 = 0;
|
2012-04-29 21:11:01 +02:00
|
|
|
new_eip = cpu_lduw_kernel(env, tss_base + 0x0e);
|
|
|
|
new_eflags = cpu_lduw_kernel(env, tss_base + 0x10);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 8; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
new_regs[i] = cpu_lduw_kernel(env, tss_base + (0x12 + i * 2)) |
|
|
|
|
0xffff0000;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < 4; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x22 + i * 4));
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:11:01 +02:00
|
|
|
new_ldt = cpu_lduw_kernel(env, tss_base + 0x2a);
|
2008-05-28 14:51:20 +02:00
|
|
|
new_segs[R_FS] = 0;
|
|
|
|
new_segs[R_GS] = 0;
|
|
|
|
new_trap = 0;
|
|
|
|
}
|
2010-10-13 20:38:08 +02:00
|
|
|
/* XXX: avoid a compiler warning, see
|
|
|
|
http://support.amd.com/us/Processor_TechDocs/24593.pdf
|
|
|
|
chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
|
|
|
|
(void)new_trap;
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* NOTE: we must avoid memory exceptions during the task switch,
|
|
|
|
so we make dummy accesses before */
|
|
|
|
/* XXX: it can still fail in some cases, so a bigger hack is
|
|
|
|
necessary to valid the TLB after having done the accesses */
|
|
|
|
|
2012-04-29 21:11:01 +02:00
|
|
|
v1 = cpu_ldub_kernel(env, env->tr.base);
|
|
|
|
v2 = cpu_ldub_kernel(env, env->tr.base + old_tss_limit_max);
|
|
|
|
cpu_stb_kernel(env, env->tr.base, v1);
|
|
|
|
cpu_stb_kernel(env, env->tr.base + old_tss_limit_max, v2);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* clear busy bit (it is restartable) */
|
|
|
|
if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
|
|
|
|
target_ulong ptr;
|
|
|
|
uint32_t e2;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = env->gdt.base + (env->tr.selector & ~7);
|
2012-04-29 21:11:01 +02:00
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
e2 &= ~DESC_TSS_BUSY_MASK;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, ptr + 4, e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
old_eflags = cpu_compute_eflags(env);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (source == SWITCH_TSS_IRET) {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eflags &= ~NT_MASK;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* save the current state in the old TSS */
|
|
|
|
if (type & 8) {
|
|
|
|
/* 32 bit */
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, env->tr.base + 0x20, next_eip);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + 0x24, old_eflags);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 0 * 4), EAX);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 1 * 4), ECX);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 2 * 4), EDX);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 3 * 4), EBX);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 4 * 4), ESP);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 5 * 4), EBP);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 6 * 4), ESI);
|
|
|
|
cpu_stl_kernel(env, env->tr.base + (0x28 + 7 * 4), EDI);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 6; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x48 + i * 4),
|
|
|
|
env->segs[i].selector);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* 16 bit */
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_kernel(env, env->tr.base + 0x0e, next_eip);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + 0x10, old_eflags);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 0 * 2), EAX);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 1 * 2), ECX);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 2 * 2), EDX);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 3 * 2), EBX);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 4 * 2), ESP);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 5 * 2), EBP);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 6 * 2), ESI);
|
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x12 + 7 * 2), EDI);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 4; i++) {
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_kernel(env, env->tr.base + (0x22 + i * 4),
|
|
|
|
env->segs[i].selector);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now if an exception occurs, it will occurs in the next task
|
|
|
|
context */
|
|
|
|
|
|
|
|
if (source == SWITCH_TSS_CALL) {
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_kernel(env, tss_base, env->tr.selector);
|
2008-05-28 14:51:20 +02:00
|
|
|
new_eflags |= NT_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set busy bit */
|
|
|
|
if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
|
|
|
|
target_ulong ptr;
|
|
|
|
uint32_t e2;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = env->gdt.base + (tss_selector & ~7);
|
2012-04-29 21:11:01 +02:00
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
e2 |= DESC_TSS_BUSY_MASK;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, ptr + 4, e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the new CPU state */
|
|
|
|
/* from this point, any exception which occurs can give problems */
|
|
|
|
env->cr[0] |= CR0_TS_MASK;
|
|
|
|
env->hflags |= HF_TS_MASK;
|
|
|
|
env->tr.selector = tss_selector;
|
|
|
|
env->tr.base = tss_base;
|
|
|
|
env->tr.limit = tss_limit;
|
|
|
|
env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
|
|
|
|
|
|
|
|
if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
|
|
|
|
cpu_x86_update_cr3(env, new_cr3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load all registers without an exception, then reload them with
|
|
|
|
possible exception */
|
|
|
|
env->eip = new_eip;
|
|
|
|
eflags_mask = TF_MASK | AC_MASK | ID_MASK |
|
|
|
|
IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(type & 8)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
eflags_mask &= 0xffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, new_eflags, eflags_mask);
|
2012-04-28 17:33:48 +02:00
|
|
|
/* XXX: what to do in 16 bit case? */
|
2008-05-28 14:51:20 +02:00
|
|
|
EAX = new_regs[0];
|
|
|
|
ECX = new_regs[1];
|
|
|
|
EDX = new_regs[2];
|
|
|
|
EBX = new_regs[3];
|
|
|
|
ESP = new_regs[4];
|
|
|
|
EBP = new_regs[5];
|
|
|
|
ESI = new_regs[6];
|
|
|
|
EDI = new_regs[7];
|
|
|
|
if (new_eflags & VM_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 6; i++) {
|
2012-04-29 21:47:06 +02:00
|
|
|
load_seg_vm(env, i, new_segs[i]);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* in vm86, CPL is always 3 */
|
|
|
|
cpu_x86_set_cpl(env, 3);
|
|
|
|
} else {
|
|
|
|
/* CPL is set the RPL of CS */
|
|
|
|
cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
|
|
|
|
/* first just selectors as the rest may trigger exceptions */
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 6; i++) {
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
env->ldt.selector = new_ldt & ~4;
|
|
|
|
env->ldt.base = 0;
|
|
|
|
env->ldt.limit = 0;
|
|
|
|
env->ldt.flags = 0;
|
|
|
|
|
|
|
|
/* load the LDT */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (new_ldt & 4) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
if ((new_ldt & 0xfffc) != 0) {
|
|
|
|
dt = &env->gdt;
|
|
|
|
index = new_ldt & ~7;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((index + 7) > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + index;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
load_seg_cache_raw_dt(&env->ldt, e1, e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load the segments */
|
|
|
|
if (!(new_eflags & VM_MASK)) {
|
2012-04-29 21:47:06 +02:00
|
|
|
tss_load_seg(env, R_CS, new_segs[R_CS]);
|
|
|
|
tss_load_seg(env, R_SS, new_segs[R_SS]);
|
|
|
|
tss_load_seg(env, R_ES, new_segs[R_ES]);
|
|
|
|
tss_load_seg(env, R_DS, new_segs[R_DS]);
|
|
|
|
tss_load_seg(env, R_FS, new_segs[R_FS]);
|
|
|
|
tss_load_seg(env, R_GS, new_segs[R_GS]);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check that EIP is in the CS segment limits */
|
|
|
|
if (new_eip > env->segs[R_CS].limit) {
|
2012-04-28 17:33:48 +02:00
|
|
|
/* XXX: different exception if CALL? */
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2008-11-18 22:08:15 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
/* reset local breakpoints */
|
|
|
|
if (env->dr[7] & 0x55) {
|
|
|
|
for (i = 0; i < 4; i++) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (hw_breakpoint_enabled(env->dr[7], i) == 0x1) {
|
2008-11-18 22:08:15 +01:00
|
|
|
hw_breakpoint_remove(env, i);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-11-18 22:08:15 +01:00
|
|
|
}
|
|
|
|
env->dr[7] &= ~0x55;
|
|
|
|
}
|
|
|
|
#endif
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int get_sp_mask(unsigned int e2)
|
|
|
|
{
|
2012-04-28 17:33:48 +02:00
|
|
|
if (e2 & DESC_B_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
return 0xffffffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
return 0xffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
static int exception_has_error_code(int intno)
|
2009-04-22 22:20:07 +02:00
|
|
|
{
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (intno) {
|
|
|
|
case 8:
|
|
|
|
case 10:
|
|
|
|
case 11:
|
|
|
|
case 12:
|
|
|
|
case 13:
|
|
|
|
case 14:
|
|
|
|
case 17:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-04-22 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
#define SET_ESP(val, sp_mask) \
|
|
|
|
do { \
|
|
|
|
if ((sp_mask) == 0xffff) { \
|
|
|
|
ESP = (ESP & ~0xffff) | ((val) & 0xffff); \
|
|
|
|
} else if ((sp_mask) == 0xffffffffLL) { \
|
|
|
|
ESP = (uint32_t)(val); \
|
|
|
|
} else { \
|
|
|
|
ESP = (val); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2008-05-28 14:51:20 +02:00
|
|
|
#else
|
2012-04-28 17:33:48 +02:00
|
|
|
#define SET_ESP(val, sp_mask) \
|
|
|
|
do { \
|
|
|
|
ESP = (ESP & ~(sp_mask)) | ((val) & (sp_mask)); \
|
|
|
|
} while (0)
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
|
|
|
|
2008-09-09 16:49:02 +02:00
|
|
|
/* in 64-bit machines, this can overflow. So this segment addition macro
|
|
|
|
* can be used to trim the value to 32-bit whenever needed */
|
|
|
|
#define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
|
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
/* XXX: add a is_user flag to have proper security support */
|
2012-04-29 21:11:01 +02:00
|
|
|
#define PUSHW(ssp, sp, sp_mask, val) \
|
|
|
|
{ \
|
|
|
|
sp -= 2; \
|
|
|
|
cpu_stw_kernel(env, (ssp) + (sp & (sp_mask)), (val)); \
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
#define PUSHL(ssp, sp, sp_mask, val) \
|
|
|
|
{ \
|
|
|
|
sp -= 4; \
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val)); \
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-29 21:11:01 +02:00
|
|
|
#define POPW(ssp, sp, sp_mask, val) \
|
|
|
|
{ \
|
|
|
|
val = cpu_lduw_kernel(env, (ssp) + (sp & (sp_mask))); \
|
|
|
|
sp += 2; \
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-29 21:11:01 +02:00
|
|
|
#define POPL(ssp, sp, sp_mask, val) \
|
|
|
|
{ \
|
|
|
|
val = (uint32_t)cpu_ldl_kernel(env, SEG_ADDL(ssp, sp, sp_mask)); \
|
|
|
|
sp += 4; \
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* protected mode interrupt */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, unsigned int next_eip,
|
|
|
|
int is_hw)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
target_ulong ptr, ssp;
|
|
|
|
int type, dpl, selector, ss_dpl, cpl;
|
|
|
|
int has_error_code, new_stack, shift;
|
2009-01-14 20:27:02 +01:00
|
|
|
uint32_t e1, e2, offset, ss = 0, esp, ss_e1 = 0, ss_e2 = 0;
|
2008-05-28 14:51:20 +02:00
|
|
|
uint32_t old_eip, sp_mask;
|
|
|
|
|
|
|
|
has_error_code = 0;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!is_int && !is_hw) {
|
|
|
|
has_error_code = exception_has_error_code(intno);
|
|
|
|
}
|
|
|
|
if (is_int) {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = next_eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = env->eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
dt = &env->idt;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (intno * 8 + 7 > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + intno * 8;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
/* check gate type */
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 5: /* task gate */
|
|
|
|
/* must do that check here to return the correct error code */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
|
2008-05-28 14:51:20 +02:00
|
|
|
if (has_error_code) {
|
|
|
|
int type;
|
|
|
|
uint32_t mask;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
/* push the error code */
|
|
|
|
type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
|
|
|
|
shift = type >> 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->segs[R_SS].flags & DESC_B_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
mask = 0xffffffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
mask = 0xffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
esp = (ESP - (2 << shift)) & mask;
|
|
|
|
ssp = env->segs[R_SS].base + esp;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (shift) {
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, ssp, error_code);
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_kernel(env, ssp, error_code);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
SET_ESP(esp, mask);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 6: /* 286 interrupt gate */
|
|
|
|
case 7: /* 286 trap gate */
|
|
|
|
case 14: /* 386 interrupt gate */
|
|
|
|
case 15: /* 386 trap gate */
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
|
2008-05-28 14:51:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
2008-06-03 21:51:57 +02:00
|
|
|
/* check privilege if software int */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (is_int && dpl < cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* check valid bit */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
selector = e1 >> 16;
|
|
|
|
offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(e2 & DESC_C_MASK) && dpl < cpl) {
|
|
|
|
/* to inner privilege */
|
2012-04-29 21:47:06 +02:00
|
|
|
get_ss_esp_from_tss(env, &ss, &esp, dpl);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((ss & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if ((ss & 3) != dpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (ss_dpl != dpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(ss_e2 & DESC_S_MASK) ||
|
|
|
|
(ss_e2 & DESC_CS_MASK) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
!(ss_e2 & DESC_W_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(ss_e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 1;
|
|
|
|
sp_mask = get_sp_mask(ss_e2);
|
|
|
|
ssp = get_seg_base(ss_e1, ss_e2);
|
|
|
|
} else if ((e2 & DESC_C_MASK) || dpl == cpl) {
|
|
|
|
/* to same privilege */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->eflags & VM_MASK) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 0;
|
|
|
|
sp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
esp = ESP;
|
|
|
|
dpl = cpl;
|
|
|
|
} else {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 0; /* avoid warning */
|
|
|
|
sp_mask = 0; /* avoid warning */
|
|
|
|
ssp = 0; /* avoid warning */
|
|
|
|
esp = 0; /* avoid warning */
|
|
|
|
}
|
|
|
|
|
|
|
|
shift = type >> 3;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX: check that enough room is available */
|
|
|
|
push_size = 6 + (new_stack << 2) + (has_error_code << 1);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->eflags & VM_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
push_size += 8;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
push_size <<= shift;
|
|
|
|
#endif
|
|
|
|
if (shift == 1) {
|
|
|
|
if (new_stack) {
|
|
|
|
if (env->eflags & VM_MASK) {
|
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
|
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
|
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
|
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
|
|
|
|
}
|
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
|
|
|
|
PUSHL(ssp, esp, sp_mask, ESP);
|
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
PUSHL(ssp, esp, sp_mask, cpu_compute_eflags(env));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHL(ssp, esp, sp_mask, old_eip);
|
|
|
|
if (has_error_code) {
|
|
|
|
PUSHL(ssp, esp, sp_mask, error_code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (new_stack) {
|
|
|
|
if (env->eflags & VM_MASK) {
|
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
|
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
|
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
|
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
|
|
|
|
}
|
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
|
|
|
|
PUSHW(ssp, esp, sp_mask, ESP);
|
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
PUSHW(ssp, esp, sp_mask, cpu_compute_eflags(env));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHW(ssp, esp, sp_mask, old_eip);
|
|
|
|
if (has_error_code) {
|
|
|
|
PUSHW(ssp, esp, sp_mask, error_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_stack) {
|
|
|
|
if (env->eflags & VM_MASK) {
|
|
|
|
cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0);
|
|
|
|
cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0);
|
|
|
|
cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0);
|
|
|
|
cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
ss = (ss & ~3) | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, ss,
|
|
|
|
ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
|
|
|
|
}
|
|
|
|
SET_ESP(esp, sp_mask);
|
|
|
|
|
|
|
|
selector = (selector & ~3) | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
cpu_x86_set_cpl(env, dpl);
|
|
|
|
env->eip = offset;
|
|
|
|
|
|
|
|
/* interrupt gate clear IF mask */
|
|
|
|
if ((type & 1) == 0) {
|
|
|
|
env->eflags &= ~IF_MASK;
|
|
|
|
}
|
|
|
|
env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
#define PUSHQ(sp, val) \
|
|
|
|
{ \
|
|
|
|
sp -= 8; \
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stq_kernel(env, sp, (val)); \
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
#define POPQ(sp, val) \
|
|
|
|
{ \
|
2012-04-29 21:11:01 +02:00
|
|
|
val = cpu_ldq_kernel(env, sp); \
|
2012-04-28 17:33:48 +02:00
|
|
|
sp += 8; \
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("TR: base=" TARGET_FMT_lx " limit=%x\n",
|
|
|
|
env->tr.base, env->tr.limit);
|
|
|
|
#endif
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(env->tr.flags & DESC_P_MASK)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_abort(env, "invalid tss");
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
index = 8 * level + 4;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((index + 7) > env->tr.limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:11:01 +02:00
|
|
|
return cpu_ldq_kernel(env, env->tr.base + index);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 64 bit interrupt */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void do_interrupt64(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, target_ulong next_eip, int is_hw)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
target_ulong ptr;
|
|
|
|
int type, dpl, selector, cpl, ist;
|
|
|
|
int has_error_code, new_stack;
|
|
|
|
uint32_t e1, e2, e3, ss;
|
|
|
|
target_ulong old_eip, esp, offset;
|
|
|
|
|
|
|
|
has_error_code = 0;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!is_int && !is_hw) {
|
|
|
|
has_error_code = exception_has_error_code(intno);
|
|
|
|
}
|
|
|
|
if (is_int) {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = next_eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = env->eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
dt = &env->idt;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (intno * 16 + 15 > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + intno * 16;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
|
|
|
e3 = cpu_ldl_kernel(env, ptr + 8);
|
2008-05-28 14:51:20 +02:00
|
|
|
/* check gate type */
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 14: /* 386 interrupt gate */
|
|
|
|
case 15: /* 386 trap gate */
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
|
2008-05-28 14:51:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
2008-06-03 21:51:57 +02:00
|
|
|
/* check privilege if software int */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (is_int && dpl < cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* check valid bit */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, intno * 16 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
selector = e1 >> 16;
|
|
|
|
offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
|
|
|
|
ist = e2 & 7;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) {
|
|
|
|
/* to inner privilege */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (ist != 0) {
|
2012-04-29 21:47:06 +02:00
|
|
|
esp = get_rsp_from_tss(env, ist + 3);
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2012-04-29 21:47:06 +02:00
|
|
|
esp = get_rsp_from_tss(env, dpl);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
esp &= ~0xfLL; /* align stack */
|
|
|
|
ss = 0;
|
|
|
|
new_stack = 1;
|
|
|
|
} else if ((e2 & DESC_C_MASK) || dpl == cpl) {
|
|
|
|
/* to same privilege */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->eflags & VM_MASK) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 0;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (ist != 0) {
|
2012-04-29 21:47:06 +02:00
|
|
|
esp = get_rsp_from_tss(env, ist + 3);
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
esp = ESP;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
esp &= ~0xfLL; /* align stack */
|
|
|
|
dpl = cpl;
|
|
|
|
} else {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 0; /* avoid warning */
|
|
|
|
esp = 0; /* avoid warning */
|
|
|
|
}
|
|
|
|
|
|
|
|
PUSHQ(esp, env->segs[R_SS].selector);
|
|
|
|
PUSHQ(esp, ESP);
|
2012-04-29 17:01:21 +02:00
|
|
|
PUSHQ(esp, cpu_compute_eflags(env));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHQ(esp, env->segs[R_CS].selector);
|
|
|
|
PUSHQ(esp, old_eip);
|
|
|
|
if (has_error_code) {
|
|
|
|
PUSHQ(esp, error_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_stack) {
|
|
|
|
ss = 0 | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0);
|
|
|
|
}
|
|
|
|
ESP = esp;
|
|
|
|
|
|
|
|
selector = (selector & ~3) | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
cpu_x86_set_cpl(env, dpl);
|
|
|
|
env->eip = offset;
|
|
|
|
|
|
|
|
/* interrupt gate clear IF mask */
|
|
|
|
if ((type & 1) == 0) {
|
|
|
|
env->eflags &= ~IF_MASK;
|
|
|
|
}
|
|
|
|
env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
#ifdef TARGET_X86_64
|
2008-05-28 14:51:20 +02:00
|
|
|
#if defined(CONFIG_USER_ONLY)
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_syscall(CPUX86State *env, int next_eip_addend)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
env->exception_index = EXCP_SYSCALL;
|
|
|
|
env->exception_next_eip = env->eip + next_eip_addend;
|
2011-05-14 14:52:35 +02:00
|
|
|
cpu_loop_exit(env);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
#else
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_syscall(CPUX86State *env, int next_eip_addend)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int selector;
|
|
|
|
|
|
|
|
if (!(env->efer & MSR_EFER_SCE)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP06_ILLOP, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
selector = (env->star >> 32) & 0xffff;
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
int code64;
|
|
|
|
|
|
|
|
ECX = env->eip + next_eip_addend;
|
2012-04-29 17:01:21 +02:00
|
|
|
env->regs[11] = cpu_compute_eflags(env);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
code64 = env->hflags & HF_CS64_MASK;
|
|
|
|
|
|
|
|
cpu_x86_set_cpl(env, 0);
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
2012-04-28 17:33:48 +02:00
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
|
|
|
|
DESC_L_MASK);
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
env->eflags &= ~env->fmask;
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, env->eflags, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (code64) {
|
2008-05-28 14:51:20 +02:00
|
|
|
env->eip = env->lstar;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
env->eip = env->cstar;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
ECX = (uint32_t)(env->eip + next_eip_addend);
|
|
|
|
|
|
|
|
cpu_x86_set_cpl(env, 0);
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
#endif
|
2008-05-28 14:51:20 +02:00
|
|
|
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
#ifdef TARGET_X86_64
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_sysret(CPUX86State *env, int dflag)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int cpl, selector;
|
|
|
|
|
|
|
|
if (!(env->efer & MSR_EFER_SCE)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP06_ILLOP, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
selector = (env->star >> 48) & 0xffff;
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
if (dflag == 2) {
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
|
|
|
|
DESC_L_MASK);
|
|
|
|
env->eip = ECX;
|
|
|
|
} else {
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector | 3,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
|
|
|
env->eip = (uint32_t)ECX;
|
|
|
|
}
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, selector + 8,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, (uint32_t)(env->regs[11]), TF_MASK | AC_MASK
|
|
|
|
| ID_MASK | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK |
|
|
|
|
NT_MASK);
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_set_cpl(env, 3);
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector | 3,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
|
|
|
env->eip = (uint32_t)ECX;
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, selector + 8,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
env->eflags |= IF_MASK;
|
|
|
|
cpu_x86_set_cpl(env, 3);
|
|
|
|
}
|
|
|
|
}
|
x86 cleanup
Remove some unnecessary includes, add needed includes, move prototypes to
cpu.h to suppress missing prototype warnings.
Remove unused functions and prototypes (cpu_x86_flush_tlb, cpu_lock,
cpu_unlock, restore_native_fp_state, save_native_fp_state).
Make some functions and data static (f15rk, parity_table, rclw_table,
rclb_table, raise_interrupt, fpu_raise_exception), they are not used
outside op_helper.c anymore.
Make some x86_64 and user only code conditional to avoid warnings.
Document where each function is implemented in cpu.h and exec.h.
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6005 c046a42c-6fe2-441c-8c8c-71466251a162
2008-12-13 12:49:17 +01:00
|
|
|
#endif
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* real mode interrupt */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void do_interrupt_real(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, unsigned int next_eip)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
target_ulong ptr, ssp;
|
|
|
|
int selector;
|
|
|
|
uint32_t offset, esp;
|
|
|
|
uint32_t old_cs, old_eip;
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
/* real mode (simpler!) */
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->idt;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (intno * 4 + 3 > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + intno * 4;
|
2012-04-29 21:11:01 +02:00
|
|
|
offset = cpu_lduw_kernel(env, ptr);
|
|
|
|
selector = cpu_lduw_kernel(env, ptr + 2);
|
2008-05-28 14:51:20 +02:00
|
|
|
esp = ESP;
|
|
|
|
ssp = env->segs[R_SS].base;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (is_int) {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = next_eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
old_eip = env->eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
old_cs = env->segs[R_CS].selector;
|
2012-04-28 17:33:48 +02:00
|
|
|
/* XXX: use SS segment size? */
|
2012-04-29 17:01:21 +02:00
|
|
|
PUSHW(ssp, esp, 0xffff, cpu_compute_eflags(env));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHW(ssp, esp, 0xffff, old_cs);
|
|
|
|
PUSHW(ssp, esp, 0xffff, old_eip);
|
|
|
|
|
|
|
|
/* update processor state */
|
|
|
|
ESP = (ESP & ~0xffff) | (esp & 0xffff);
|
|
|
|
env->eip = offset;
|
|
|
|
env->segs[R_CS].selector = selector;
|
|
|
|
env->segs[R_CS].base = (selector << 4);
|
|
|
|
env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
|
|
|
|
}
|
|
|
|
|
2011-05-16 21:38:48 +02:00
|
|
|
#if defined(CONFIG_USER_ONLY)
|
2008-05-28 14:51:20 +02:00
|
|
|
/* fake user mode interrupt */
|
2012-04-29 21:47:06 +02:00
|
|
|
static void do_interrupt_user(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, target_ulong next_eip)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
target_ulong ptr;
|
|
|
|
int dpl, cpl, shift;
|
|
|
|
uint32_t e2;
|
|
|
|
|
|
|
|
dt = &env->idt;
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
shift = 4;
|
|
|
|
} else {
|
|
|
|
shift = 3;
|
|
|
|
}
|
|
|
|
ptr = dt->base + (intno << shift);
|
2012-04-29 21:11:01 +02:00
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
2008-06-03 21:51:57 +02:00
|
|
|
/* check privilege if software int */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (is_int && dpl < cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, (intno << shift) + 2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* Since we emulate only user space, we cannot do more than
|
|
|
|
exiting the emulation with the suitable exception and error
|
|
|
|
code */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (is_int) {
|
2008-05-28 14:51:20 +02:00
|
|
|
EIP = next_eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 21:38:48 +02:00
|
|
|
#else
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
static void handle_even_inj(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, int is_hw, int rm)
|
2009-04-22 22:20:07 +02:00
|
|
|
{
|
2012-04-28 17:33:48 +02:00
|
|
|
uint32_t event_inj = ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
|
|
|
|
control.event_inj));
|
|
|
|
|
2009-04-22 22:20:07 +02:00
|
|
|
if (!(event_inj & SVM_EVTINJ_VALID)) {
|
2012-04-28 17:33:48 +02:00
|
|
|
int type;
|
|
|
|
|
|
|
|
if (is_int) {
|
|
|
|
type = SVM_EVTINJ_TYPE_SOFT;
|
|
|
|
} else {
|
|
|
|
type = SVM_EVTINJ_TYPE_EXEPT;
|
|
|
|
}
|
|
|
|
event_inj = intno | type | SVM_EVTINJ_VALID;
|
|
|
|
if (!rm && exception_has_error_code(intno)) {
|
|
|
|
event_inj |= SVM_EVTINJ_VALID_ERR;
|
|
|
|
stl_phys(env->vm_vmcb + offsetof(struct vmcb,
|
|
|
|
control.event_inj_err),
|
|
|
|
error_code);
|
|
|
|
}
|
|
|
|
stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj),
|
|
|
|
event_inj);
|
2009-04-22 22:20:07 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-23 15:16:56 +02:00
|
|
|
#endif
|
2009-04-22 22:20:07 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
/*
|
|
|
|
* Begin execution of an interruption. is_int is TRUE if coming from
|
|
|
|
* the int instruction. next_eip is the EIP value AFTER the interrupt
|
|
|
|
* instruction. It is only relevant if is_int is TRUE.
|
|
|
|
*/
|
2012-04-29 21:47:06 +02:00
|
|
|
static void do_interrupt_all(CPUX86State *env, int intno, int is_int,
|
|
|
|
int error_code, target_ulong next_eip, int is_hw)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
2009-01-15 23:36:53 +01:00
|
|
|
if (qemu_loglevel_mask(CPU_LOG_INT)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
if ((env->cr[0] & CR0_PE_MASK)) {
|
|
|
|
static int count;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
|
|
|
qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
|
|
|
|
" pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx,
|
|
|
|
count, intno, error_code, is_int,
|
|
|
|
env->hflags & HF_CPL_MASK,
|
|
|
|
env->segs[R_CS].selector, EIP,
|
|
|
|
(int)env->segs[R_CS].base + EIP,
|
|
|
|
env->segs[R_SS].selector, ESP);
|
2008-05-28 14:51:20 +02:00
|
|
|
if (intno == 0x0e) {
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log(" CR2=" TARGET_FMT_lx, env->cr[2]);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log(" EAX=" TARGET_FMT_lx, EAX);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log("\n");
|
2012-10-05 16:04:43 +02:00
|
|
|
log_cpu_state(env, CPU_DUMP_CCOP);
|
2008-05-28 14:51:20 +02:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
int i;
|
2010-04-01 23:46:20 +02:00
|
|
|
target_ulong ptr;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log(" code=");
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = env->segs[R_CS].base + env->eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = 0; i < 16; i++) {
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log(" %02x", ldub(ptr + i));
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log("\n");
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (env->cr[0] & CR0_PE_MASK) {
|
2009-04-23 15:16:56 +02:00
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_SVMI_MASK) {
|
2012-04-29 21:47:06 +02:00
|
|
|
handle_even_inj(env, intno, is_int, error_code, is_hw, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2009-04-23 15:16:56 +02:00
|
|
|
#endif
|
2008-09-06 19:47:39 +02:00
|
|
|
#ifdef TARGET_X86_64
|
2008-05-28 14:51:20 +02:00
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt64(env, intno, is_int, error_code, next_eip, is_hw);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt_protected(env, intno, is_int, error_code, next_eip,
|
|
|
|
is_hw);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-23 15:16:56 +02:00
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_SVMI_MASK) {
|
2012-04-29 21:47:06 +02:00
|
|
|
handle_even_inj(env, intno, is_int, error_code, is_hw, 1);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2009-04-23 15:16:56 +02:00
|
|
|
#endif
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt_real(env, intno, is_int, error_code, next_eip);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2009-04-22 22:20:07 +02:00
|
|
|
|
2009-04-23 15:16:56 +02:00
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
2009-04-22 22:20:07 +02:00
|
|
|
if (env->hflags & HF_SVMI_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
uint32_t event_inj = ldl_phys(env->vm_vmcb +
|
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.event_inj));
|
|
|
|
|
|
|
|
stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj),
|
|
|
|
event_inj & ~SVM_EVTINJ_VALID);
|
2009-04-22 22:20:07 +02:00
|
|
|
}
|
2009-04-23 15:16:56 +02:00
|
|
|
#endif
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void do_interrupt(CPUX86State *env)
|
2011-05-16 21:38:48 +02:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_USER_ONLY)
|
|
|
|
/* if user mode only, we simulate a fake exception
|
|
|
|
which will be handled outside the cpu execution
|
|
|
|
loop */
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt_user(env, env->exception_index,
|
2011-05-16 21:38:48 +02:00
|
|
|
env->exception_is_int,
|
|
|
|
env->error_code,
|
|
|
|
env->exception_next_eip);
|
|
|
|
/* successfully delivered */
|
|
|
|
env->old_exception = -1;
|
|
|
|
#else
|
|
|
|
/* simulate a real cpu exception. On i386, it can
|
|
|
|
trigger new exceptions, but we do not handle
|
|
|
|
double or triple faults yet. */
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt_all(env, env->exception_index,
|
2011-05-16 21:38:48 +02:00
|
|
|
env->exception_is_int,
|
|
|
|
env->error_code,
|
|
|
|
env->exception_next_eip, 0);
|
|
|
|
/* successfully delivered */
|
|
|
|
env->old_exception = -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void do_interrupt_x86_hardirq(CPUX86State *env, int intno, int is_hw)
|
2011-05-16 21:38:48 +02:00
|
|
|
{
|
2012-04-29 21:47:06 +02:00
|
|
|
do_interrupt_all(env, intno, 0, 0, 0, is_hw);
|
2011-05-16 21:38:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_enter_level(CPUX86State *env, int level, int data32,
|
|
|
|
target_ulong t1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
target_ulong ssp;
|
|
|
|
uint32_t esp_mask, esp, ebp;
|
|
|
|
|
|
|
|
esp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
ebp = EBP;
|
|
|
|
esp = ESP;
|
|
|
|
if (data32) {
|
|
|
|
/* 32 bit */
|
|
|
|
esp -= 4;
|
|
|
|
while (--level) {
|
|
|
|
esp -= 4;
|
|
|
|
ebp -= 4;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_data(env, ssp + (esp & esp_mask),
|
|
|
|
cpu_ldl_data(env, ssp + (ebp & esp_mask)));
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
esp -= 4;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_data(env, ssp + (esp & esp_mask), t1);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* 16 bit */
|
|
|
|
esp -= 2;
|
|
|
|
while (--level) {
|
|
|
|
esp -= 2;
|
|
|
|
ebp -= 2;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_data(env, ssp + (esp & esp_mask),
|
|
|
|
cpu_lduw_data(env, ssp + (ebp & esp_mask)));
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
esp -= 2;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_data(env, ssp + (esp & esp_mask), t1);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_enter64_level(CPUX86State *env, int level, int data64,
|
|
|
|
target_ulong t1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
target_ulong esp, ebp;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
ebp = EBP;
|
|
|
|
esp = ESP;
|
|
|
|
|
|
|
|
if (data64) {
|
|
|
|
/* 64 bit */
|
|
|
|
esp -= 8;
|
|
|
|
while (--level) {
|
|
|
|
esp -= 8;
|
|
|
|
ebp -= 8;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stq_data(env, esp, cpu_ldq_data(env, ebp));
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
esp -= 8;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stq_data(env, esp, t1);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* 16 bit */
|
|
|
|
esp -= 2;
|
|
|
|
while (--level) {
|
|
|
|
esp -= 2;
|
|
|
|
ebp -= 2;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_data(env, esp, cpu_lduw_data(env, ebp));
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
esp -= 2;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stw_data(env, esp, t1);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_lldt(CPUX86State *env, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
uint32_t e1, e2;
|
|
|
|
int index, entry_limit;
|
|
|
|
target_ulong ptr;
|
|
|
|
|
|
|
|
selector &= 0xffff;
|
|
|
|
if ((selector & 0xfffc) == 0) {
|
|
|
|
/* XXX: NULL selector case: invalid LDT */
|
|
|
|
env->ldt.base = 0;
|
|
|
|
env->ldt.limit = 0;
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (selector & 0x4) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->gdt;
|
|
|
|
index = selector & ~7;
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
entry_limit = 15;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
2008-05-28 14:51:20 +02:00
|
|
|
entry_limit = 7;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if ((index + entry_limit) > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + index;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
uint32_t e3;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2012-04-29 21:11:01 +02:00
|
|
|
e3 = cpu_ldl_kernel(env, ptr + 8);
|
2008-05-28 14:51:20 +02:00
|
|
|
load_seg_cache_raw_dt(&env->ldt, e1, e2);
|
|
|
|
env->ldt.base |= (target_ulong)e3 << 32;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
load_seg_cache_raw_dt(&env->ldt, e1, e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
env->ldt.selector = selector;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_ltr(CPUX86State *env, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
SegmentCache *dt;
|
|
|
|
uint32_t e1, e2;
|
|
|
|
int index, type, entry_limit;
|
|
|
|
target_ulong ptr;
|
|
|
|
|
|
|
|
selector &= 0xffff;
|
|
|
|
if ((selector & 0xfffc) == 0) {
|
|
|
|
/* NULL selector case: invalid TR */
|
|
|
|
env->tr.base = 0;
|
|
|
|
env->tr.limit = 0;
|
|
|
|
env->tr.flags = 0;
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (selector & 0x4) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->gdt;
|
|
|
|
index = selector & ~7;
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
entry_limit = 15;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
2008-05-28 14:51:20 +02:00
|
|
|
entry_limit = 7;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if ((index + entry_limit) > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + index;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
|
|
|
if ((e2 & DESC_S_MASK) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
(type != 1 && type != 9)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
uint32_t e3, e4;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2012-04-29 21:11:01 +02:00
|
|
|
e3 = cpu_ldl_kernel(env, ptr + 8);
|
|
|
|
e4 = cpu_ldl_kernel(env, ptr + 12);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e4 >> DESC_TYPE_SHIFT) & 0xf) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
load_seg_cache_raw_dt(&env->tr, e1, e2);
|
|
|
|
env->tr.base |= (target_ulong)e3 << 32;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
load_seg_cache_raw_dt(&env->tr, e1, e2);
|
|
|
|
}
|
|
|
|
e2 |= DESC_TSS_BUSY_MASK;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, ptr + 4, e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
env->tr.selector = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only works if protected mode and not VM86. seg_reg must be != R_CS */
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_load_seg(CPUX86State *env, int seg_reg, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t e1, e2;
|
|
|
|
int cpl, dpl, rpl;
|
|
|
|
SegmentCache *dt;
|
|
|
|
int index;
|
|
|
|
target_ulong ptr;
|
|
|
|
|
|
|
|
selector &= 0xffff;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if ((selector & 0xfffc) == 0) {
|
|
|
|
/* null selector case */
|
|
|
|
if (seg_reg == R_SS
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
&& (!(env->hflags & HF_CS64_MASK) || cpl == 3)
|
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (selector & 0x4) {
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->ldt;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2008-05-28 14:51:20 +02:00
|
|
|
dt = &env->gdt;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
index = selector & ~7;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((index + 7) > dt->limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ptr = dt->base + index;
|
2012-04-29 21:11:01 +02:00
|
|
|
e1 = cpu_ldl_kernel(env, ptr);
|
|
|
|
e2 = cpu_ldl_kernel(env, ptr + 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_S_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
if (seg_reg == R_SS) {
|
|
|
|
/* must be writable segment */
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (rpl != cpl || dpl != cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* must be readable segment */
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
|
|
|
|
/* if not conforming code, test rights */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (seg_reg == R_SS) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0C_STACK, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
} else {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the access bit if not already set */
|
|
|
|
if (!(e2 & DESC_A_MASK)) {
|
|
|
|
e2 |= DESC_A_MASK;
|
2012-04-29 21:11:01 +02:00
|
|
|
cpu_stl_kernel(env, ptr + 4, e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, selector,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
#if 0
|
2009-01-15 23:34:14 +01:00
|
|
|
qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
|
2008-05-28 14:51:20 +02:00
|
|
|
selector, (unsigned long)sc->base, sc->limit, sc->flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protected mode jump */
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_ljmp_protected(CPUX86State *env, int new_cs, target_ulong new_eip,
|
2008-05-28 14:51:20 +02:00
|
|
|
int next_eip_addend)
|
|
|
|
{
|
|
|
|
int gate_cs, type;
|
|
|
|
uint32_t e1, e2, cpl, dpl, rpl, limit;
|
|
|
|
target_ulong next_eip;
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((new_cs & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, new_cs) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (e2 & DESC_S_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_CS_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
if (e2 & DESC_C_MASK) {
|
|
|
|
/* conforming code segment */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* non conforming code segment */
|
|
|
|
rpl = new_cs & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (rpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (dpl != cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
limit = get_seg_limit(e1, e2);
|
|
|
|
if (new_eip > limit &&
|
2012-04-28 17:33:48 +02:00
|
|
|
!(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
|
|
|
|
get_seg_base(e1, e2), limit, e2);
|
|
|
|
EIP = new_eip;
|
|
|
|
} else {
|
|
|
|
/* jump to call or task gate */
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
rpl = new_cs & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 1: /* 286 TSS */
|
|
|
|
case 9: /* 386 TSS */
|
|
|
|
case 5: /* task gate */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
next_eip = env->eip + next_eip_addend;
|
2012-04-29 21:47:06 +02:00
|
|
|
switch_tss(env, new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
|
2008-05-28 14:51:20 +02:00
|
|
|
CC_OP = CC_OP_EFLAGS;
|
|
|
|
break;
|
|
|
|
case 4: /* 286 call gate */
|
|
|
|
case 12: /* 386 call gate */
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((dpl < cpl) || (dpl < rpl)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
gate_cs = e1 >> 16;
|
|
|
|
new_eip = (e1 & 0xffff);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (type == 12) {
|
2008-05-28 14:51:20 +02:00
|
|
|
new_eip |= (e2 & 0xffff0000);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, gate_cs) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
/* must be code segment */
|
|
|
|
if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
|
2012-04-28 17:33:48 +02:00
|
|
|
(DESC_S_MASK | DESC_CS_MASK))) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
(!(e2 & DESC_C_MASK) && (dpl != cpl))) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
limit = get_seg_limit(e1, e2);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (new_eip > limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
|
|
|
|
get_seg_base(e1, e2), limit, e2);
|
|
|
|
EIP = new_eip;
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2008-05-28 14:51:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* real mode call */
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_lcall_real(CPUX86State *env, int new_cs, target_ulong new_eip1,
|
2008-05-28 14:51:20 +02:00
|
|
|
int shift, int next_eip)
|
|
|
|
{
|
|
|
|
int new_eip;
|
|
|
|
uint32_t esp, esp_mask;
|
|
|
|
target_ulong ssp;
|
|
|
|
|
|
|
|
new_eip = new_eip1;
|
|
|
|
esp = ESP;
|
|
|
|
esp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
if (shift) {
|
|
|
|
PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHL(ssp, esp, esp_mask, next_eip);
|
|
|
|
} else {
|
|
|
|
PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHW(ssp, esp, esp_mask, next_eip);
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_ESP(esp, esp_mask);
|
|
|
|
env->eip = new_eip;
|
|
|
|
env->segs[R_CS].selector = new_cs;
|
|
|
|
env->segs[R_CS].base = (new_cs << 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protected mode call */
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_lcall_protected(CPUX86State *env, int new_cs, target_ulong new_eip,
|
2008-05-28 14:51:20 +02:00
|
|
|
int shift, int next_eip_addend)
|
|
|
|
{
|
|
|
|
int new_stack, i;
|
|
|
|
uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
|
2009-01-14 20:27:02 +01:00
|
|
|
uint32_t ss = 0, ss_e1 = 0, ss_e2 = 0, sp, type, ss_dpl, sp_mask;
|
2008-05-28 14:51:20 +02:00
|
|
|
uint32_t val, limit, old_sp_mask;
|
|
|
|
target_ulong ssp, old_ssp, next_eip;
|
|
|
|
|
|
|
|
next_eip = env->eip + next_eip_addend;
|
2009-01-15 22:48:06 +01:00
|
|
|
LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs, (uint32_t)new_eip, shift);
|
|
|
|
LOG_PCALL_STATE(env);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((new_cs & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, new_cs) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
2009-01-15 22:48:06 +01:00
|
|
|
LOG_PCALL("desc=%08x:%08x\n", e1, e2);
|
2008-05-28 14:51:20 +02:00
|
|
|
if (e2 & DESC_S_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_CS_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
if (e2 & DESC_C_MASK) {
|
|
|
|
/* conforming code segment */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
|
|
|
/* non conforming code segment */
|
|
|
|
rpl = new_cs & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (rpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (dpl != cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
/* XXX: check 16/32 bit cases in long mode */
|
|
|
|
if (shift == 2) {
|
|
|
|
target_ulong rsp;
|
2012-04-28 17:33:48 +02:00
|
|
|
|
2008-05-28 14:51:20 +02:00
|
|
|
/* 64 bit case */
|
|
|
|
rsp = ESP;
|
|
|
|
PUSHQ(rsp, env->segs[R_CS].selector);
|
|
|
|
PUSHQ(rsp, next_eip);
|
|
|
|
/* from this point, not restartable */
|
|
|
|
ESP = rsp;
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2), e2);
|
|
|
|
EIP = new_eip;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sp = ESP;
|
|
|
|
sp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
if (shift) {
|
|
|
|
PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHL(ssp, sp, sp_mask, next_eip);
|
|
|
|
} else {
|
|
|
|
PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHW(ssp, sp, sp_mask, next_eip);
|
|
|
|
}
|
|
|
|
|
|
|
|
limit = get_seg_limit(e1, e2);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (new_eip > limit) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* from this point, not restartable */
|
|
|
|
SET_ESP(sp, sp_mask);
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
|
|
|
|
get_seg_base(e1, e2), limit, e2);
|
|
|
|
EIP = new_eip;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* check gate type */
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
rpl = new_cs & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 1: /* available 286 TSS */
|
|
|
|
case 9: /* available 386 TSS */
|
|
|
|
case 5: /* task gate */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
switch_tss(env, new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
|
2008-05-28 14:51:20 +02:00
|
|
|
CC_OP = CC_OP_EFLAGS;
|
|
|
|
return;
|
|
|
|
case 4: /* 286 call gate */
|
|
|
|
case 12: /* 386 call gate */
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2008-05-28 14:51:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
shift = type >> 3;
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
/* check valid bit */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
selector = e1 >> 16;
|
|
|
|
offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
|
|
|
|
param_count = e2 & 0x1f;
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
if (!(e2 & DESC_C_MASK) && dpl < cpl) {
|
|
|
|
/* to inner privilege */
|
2012-04-29 21:47:06 +02:00
|
|
|
get_ss_esp_from_tss(env, &ss, &sp, dpl);
|
2012-04-28 17:33:48 +02:00
|
|
|
LOG_PCALL("new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx
|
|
|
|
"\n",
|
|
|
|
ss, sp, param_count, ESP);
|
|
|
|
if ((ss & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if ((ss & 3) != dpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (ss_dpl != dpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(ss_e2 & DESC_S_MASK) ||
|
|
|
|
(ss_e2 & DESC_CS_MASK) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
!(ss_e2 & DESC_W_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(ss_e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
/* push_size = ((param_count * 2) + 8) << shift; */
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
old_ssp = env->segs[R_SS].base;
|
|
|
|
|
|
|
|
sp_mask = get_sp_mask(ss_e2);
|
|
|
|
ssp = get_seg_base(ss_e1, ss_e2);
|
|
|
|
if (shift) {
|
|
|
|
PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
|
|
|
|
PUSHL(ssp, sp, sp_mask, ESP);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = param_count - 1; i >= 0; i--) {
|
2012-04-29 21:11:01 +02:00
|
|
|
val = cpu_ldl_kernel(env, old_ssp + ((ESP + i * 4) &
|
|
|
|
old_sp_mask));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHL(ssp, sp, sp_mask, val);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
|
|
|
|
PUSHW(ssp, sp, sp_mask, ESP);
|
2012-04-28 17:33:48 +02:00
|
|
|
for (i = param_count - 1; i >= 0; i--) {
|
2012-04-29 21:11:01 +02:00
|
|
|
val = cpu_lduw_kernel(env, old_ssp + ((ESP + i * 2) &
|
|
|
|
old_sp_mask));
|
2008-05-28 14:51:20 +02:00
|
|
|
PUSHW(ssp, sp, sp_mask, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_stack = 1;
|
|
|
|
} else {
|
|
|
|
/* to same privilege */
|
|
|
|
sp = ESP;
|
|
|
|
sp_mask = get_sp_mask(env->segs[R_SS].flags);
|
|
|
|
ssp = env->segs[R_SS].base;
|
2012-04-28 17:33:48 +02:00
|
|
|
/* push_size = (4 << shift); */
|
2008-05-28 14:51:20 +02:00
|
|
|
new_stack = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift) {
|
|
|
|
PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHL(ssp, sp, sp_mask, next_eip);
|
|
|
|
} else {
|
|
|
|
PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
|
|
|
|
PUSHW(ssp, sp, sp_mask, next_eip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* from this point, not restartable */
|
|
|
|
|
|
|
|
if (new_stack) {
|
|
|
|
ss = (ss & ~3) | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, ss,
|
|
|
|
ssp,
|
|
|
|
get_seg_limit(ss_e1, ss_e2),
|
|
|
|
ss_e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
selector = (selector & ~3) | dpl;
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, selector,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
cpu_x86_set_cpl(env, dpl);
|
|
|
|
SET_ESP(sp, sp_mask);
|
|
|
|
EIP = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* real and vm86 mode iret */
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_iret_real(CPUX86State *env, int shift)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
|
|
|
|
target_ulong ssp;
|
|
|
|
int eflags_mask;
|
|
|
|
|
2012-04-28 17:33:48 +02:00
|
|
|
sp_mask = 0xffff; /* XXXX: use SS segment size? */
|
2008-05-28 14:51:20 +02:00
|
|
|
sp = ESP;
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
if (shift == 1) {
|
|
|
|
/* 32 bits */
|
|
|
|
POPL(ssp, sp, sp_mask, new_eip);
|
|
|
|
POPL(ssp, sp, sp_mask, new_cs);
|
|
|
|
new_cs &= 0xffff;
|
|
|
|
POPL(ssp, sp, sp_mask, new_eflags);
|
|
|
|
} else {
|
|
|
|
/* 16 bits */
|
|
|
|
POPW(ssp, sp, sp_mask, new_eip);
|
|
|
|
POPW(ssp, sp, sp_mask, new_cs);
|
|
|
|
POPW(ssp, sp, sp_mask, new_eflags);
|
|
|
|
}
|
|
|
|
ESP = (ESP & ~sp_mask) | (sp & sp_mask);
|
2008-10-02 22:02:27 +02:00
|
|
|
env->segs[R_CS].selector = new_cs;
|
|
|
|
env->segs[R_CS].base = (new_cs << 4);
|
2008-05-28 14:51:20 +02:00
|
|
|
env->eip = new_eip;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->eflags & VM_MASK) {
|
|
|
|
eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK |
|
|
|
|
NT_MASK;
|
|
|
|
} else {
|
|
|
|
eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK |
|
|
|
|
RF_MASK | NT_MASK;
|
|
|
|
}
|
|
|
|
if (shift == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
eflags_mask &= 0xffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, new_eflags, eflags_mask);
|
2008-06-04 19:02:19 +02:00
|
|
|
env->hflags2 &= ~HF2_NMI_MASK;
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int dpl;
|
|
|
|
uint32_t e2;
|
|
|
|
|
|
|
|
/* XXX: on x86_64, we do not want to nullify FS and GS because
|
|
|
|
they may still contain a valid base. I would be interested to
|
|
|
|
know how a real x86_64 CPU behaves */
|
|
|
|
if ((seg_reg == R_FS || seg_reg == R_GS) &&
|
2012-04-28 17:33:48 +02:00
|
|
|
(env->segs[seg_reg].selector & 0xfffc) == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
return;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
e2 = env->segs[seg_reg].flags;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
|
|
|
|
/* data or non conforming code segment */
|
|
|
|
if (dpl < cpl) {
|
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protected mode iret */
|
2012-04-29 21:47:06 +02:00
|
|
|
static inline void helper_ret_protected(CPUX86State *env, int shift,
|
|
|
|
int is_iret, int addend)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t new_cs, new_eflags, new_ss;
|
|
|
|
uint32_t new_es, new_ds, new_fs, new_gs;
|
|
|
|
uint32_t e1, e2, ss_e1, ss_e2;
|
|
|
|
int cpl, dpl, rpl, eflags_mask, iopl;
|
|
|
|
target_ulong ssp, sp, new_eip, new_esp, sp_mask;
|
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
if (shift == 2) {
|
2008-05-28 14:51:20 +02:00
|
|
|
sp_mask = -1;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
2008-05-28 14:51:20 +02:00
|
|
|
sp_mask = get_sp_mask(env->segs[R_SS].flags);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
sp = ESP;
|
|
|
|
ssp = env->segs[R_SS].base;
|
|
|
|
new_eflags = 0; /* avoid warning */
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (shift == 2) {
|
|
|
|
POPQ(sp, new_eip);
|
|
|
|
POPQ(sp, new_cs);
|
|
|
|
new_cs &= 0xffff;
|
|
|
|
if (is_iret) {
|
|
|
|
POPQ(sp, new_eflags);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
|
|
|
if (shift == 1) {
|
|
|
|
/* 32 bits */
|
|
|
|
POPL(ssp, sp, sp_mask, new_eip);
|
|
|
|
POPL(ssp, sp, sp_mask, new_cs);
|
|
|
|
new_cs &= 0xffff;
|
|
|
|
if (is_iret) {
|
|
|
|
POPL(ssp, sp, sp_mask, new_eflags);
|
|
|
|
if (new_eflags & VM_MASK) {
|
|
|
|
goto return_to_vm86;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 16 bits */
|
|
|
|
POPW(ssp, sp, sp_mask, new_eip);
|
|
|
|
POPW(ssp, sp, sp_mask, new_cs);
|
|
|
|
if (is_iret) {
|
|
|
|
POPW(ssp, sp, sp_mask, new_eflags);
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-15 22:48:06 +01:00
|
|
|
LOG_PCALL("lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
|
|
|
|
new_cs, new_eip, shift, addend);
|
|
|
|
LOG_PCALL_STATE(env);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((new_cs & 0xfffc) == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, new_cs) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(e2 & DESC_S_MASK) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
!(e2 & DESC_CS_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
rpl = new_cs & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (rpl < cpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
if (e2 & DESC_C_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl > rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl != rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
sp += addend;
|
|
|
|
if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
|
|
|
|
((env->hflags & HF_CS64_MASK) && !is_iret))) {
|
2008-06-03 21:51:57 +02:00
|
|
|
/* return to same privilege level */
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, new_cs,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
} else {
|
|
|
|
/* return to different privilege level */
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (shift == 2) {
|
|
|
|
POPQ(sp, new_esp);
|
|
|
|
POPQ(sp, new_ss);
|
|
|
|
new_ss &= 0xffff;
|
|
|
|
} else
|
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
|
|
|
if (shift == 1) {
|
|
|
|
/* 32 bits */
|
|
|
|
POPL(ssp, sp, sp_mask, new_esp);
|
|
|
|
POPL(ssp, sp, sp_mask, new_ss);
|
|
|
|
new_ss &= 0xffff;
|
|
|
|
} else {
|
|
|
|
/* 16 bits */
|
|
|
|
POPW(ssp, sp, sp_mask, new_esp);
|
|
|
|
POPW(ssp, sp, sp_mask, new_ss);
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2009-01-15 22:48:06 +01:00
|
|
|
LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx "\n",
|
2012-04-28 17:33:48 +02:00
|
|
|
new_ss, new_esp);
|
2008-05-28 14:51:20 +02:00
|
|
|
if ((new_ss & 0xfffc) == 0) {
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
/* NULL ss is allowed in long mode if cpl != 3 */
|
|
|
|
/* XXX: test CS64? */
|
2008-05-28 14:51:20 +02:00
|
|
|
if ((env->hflags & HF_LMA_MASK) && rpl != 3) {
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, new_ss,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
2012-04-28 17:33:48 +02:00
|
|
|
ss_e2 = DESC_B_MASK; /* XXX: should not be needed? */
|
2008-05-28 14:51:20 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((new_ss & 3) != rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &ss_e1, &ss_e2, new_ss) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(ss_e2 & DESC_S_MASK) ||
|
|
|
|
(ss_e2 & DESC_CS_MASK) ||
|
2012-04-28 17:33:48 +02:00
|
|
|
!(ss_e2 & DESC_W_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl != rpl) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(ss_e2 & DESC_P_MASK)) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0B_NOSEG, new_ss & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, new_ss,
|
|
|
|
get_seg_base(ss_e1, ss_e2),
|
|
|
|
get_seg_limit(ss_e1, ss_e2),
|
|
|
|
ss_e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, new_cs,
|
|
|
|
get_seg_base(e1, e2),
|
|
|
|
get_seg_limit(e1, e2),
|
|
|
|
e2);
|
|
|
|
cpu_x86_set_cpl(env, rpl);
|
|
|
|
sp = new_esp;
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_CS64_MASK) {
|
2008-05-28 14:51:20 +02:00
|
|
|
sp_mask = -1;
|
2012-04-28 17:33:48 +02:00
|
|
|
} else
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
2012-04-28 17:33:48 +02:00
|
|
|
{
|
2008-05-28 14:51:20 +02:00
|
|
|
sp_mask = get_sp_mask(ss_e2);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
/* validate data segments */
|
2012-04-29 21:47:06 +02:00
|
|
|
validate_seg(env, R_ES, rpl);
|
|
|
|
validate_seg(env, R_DS, rpl);
|
|
|
|
validate_seg(env, R_FS, rpl);
|
|
|
|
validate_seg(env, R_GS, rpl);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
sp += addend;
|
|
|
|
}
|
|
|
|
SET_ESP(sp, sp_mask);
|
|
|
|
env->eip = new_eip;
|
|
|
|
if (is_iret) {
|
|
|
|
/* NOTE: 'cpl' is the _old_ CPL */
|
|
|
|
eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (cpl == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
eflags_mask |= IOPL_MASK;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
iopl = (env->eflags >> IOPL_SHIFT) & 3;
|
2012-04-28 17:33:48 +02:00
|
|
|
if (cpl <= iopl) {
|
2008-05-28 14:51:20 +02:00
|
|
|
eflags_mask |= IF_MASK;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (shift == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
eflags_mask &= 0xffff;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, new_eflags, eflags_mask);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
return_to_vm86:
|
|
|
|
POPL(ssp, sp, sp_mask, new_esp);
|
|
|
|
POPL(ssp, sp, sp_mask, new_ss);
|
|
|
|
POPL(ssp, sp, sp_mask, new_es);
|
|
|
|
POPL(ssp, sp, sp_mask, new_ds);
|
|
|
|
POPL(ssp, sp, sp_mask, new_fs);
|
|
|
|
POPL(ssp, sp, sp_mask, new_gs);
|
|
|
|
|
|
|
|
/* modify processor state */
|
2012-04-29 17:01:21 +02:00
|
|
|
cpu_load_eflags(env, new_eflags, TF_MASK | AC_MASK | ID_MASK |
|
|
|
|
IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK |
|
|
|
|
VIP_MASK);
|
2012-04-29 21:47:06 +02:00
|
|
|
load_seg_vm(env, R_CS, new_cs & 0xffff);
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_set_cpl(env, 3);
|
2012-04-29 21:47:06 +02:00
|
|
|
load_seg_vm(env, R_SS, new_ss & 0xffff);
|
|
|
|
load_seg_vm(env, R_ES, new_es & 0xffff);
|
|
|
|
load_seg_vm(env, R_DS, new_ds & 0xffff);
|
|
|
|
load_seg_vm(env, R_FS, new_fs & 0xffff);
|
|
|
|
load_seg_vm(env, R_GS, new_gs & 0xffff);
|
2008-05-28 14:51:20 +02:00
|
|
|
|
|
|
|
env->eip = new_eip & 0xffff;
|
|
|
|
ESP = new_esp;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_iret_protected(CPUX86State *env, int shift, int next_eip)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int tss_selector, type;
|
|
|
|
uint32_t e1, e2;
|
|
|
|
|
|
|
|
/* specific case for TSS */
|
|
|
|
if (env->eflags & NT_MASK) {
|
|
|
|
#ifdef TARGET_X86_64
|
2012-04-28 17:33:48 +02:00
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
#endif
|
2012-04-29 21:11:01 +02:00
|
|
|
tss_selector = cpu_lduw_kernel(env, env->tr.base + 0);
|
2012-04-28 17:33:48 +02:00
|
|
|
if (tss_selector & 4) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, tss_selector) != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
|
|
|
|
/* NOTE: we check both segment and busy TSS */
|
2012-04-28 17:33:48 +02:00
|
|
|
if (type != 3) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
switch_tss(env, tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
|
2008-05-28 14:51:20 +02:00
|
|
|
} else {
|
2012-04-29 21:47:06 +02:00
|
|
|
helper_ret_protected(env, shift, 1, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
2008-06-04 19:02:19 +02:00
|
|
|
env->hflags2 &= ~HF2_NMI_MASK;
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_lret_protected(CPUX86State *env, int shift, int addend)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
2012-04-29 21:47:06 +02:00
|
|
|
helper_ret_protected(env, shift, 0, addend);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_sysenter(CPUX86State *env)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
if (env->sysenter_cs == 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
|
|
|
|
cpu_x86_set_cpl(env, 0);
|
2008-09-25 20:16:18 +02:00
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (env->hflags & HF_LMA_MASK) {
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
2012-04-28 17:33:48 +02:00
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
|
|
|
|
DESC_L_MASK);
|
2008-09-25 20:16:18 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc,
|
|
|
|
0, 0xffffffff,
|
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
ESP = env->sysenter_esp;
|
|
|
|
EIP = env->sysenter_eip;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_sysexit(CPUX86State *env, int dflag)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
int cpl;
|
|
|
|
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (env->sysenter_cs == 0 || cpl != 0) {
|
2012-04-28 21:35:10 +02:00
|
|
|
raise_exception_err(env, EXCP0D_GPF, 0);
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
cpu_x86_set_cpl(env, 3);
|
2008-09-25 20:16:18 +02:00
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
if (dflag == 2) {
|
2012-04-28 17:33:48 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 32) & 0xfffc) |
|
|
|
|
3, 0, 0xffffffff,
|
2008-09-25 20:16:18 +02:00
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
2012-04-28 17:33:48 +02:00
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
|
|
|
|
DESC_L_MASK);
|
|
|
|
cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 40) & 0xfffc) |
|
|
|
|
3, 0, 0xffffffff,
|
2008-09-25 20:16:18 +02:00
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-28 17:33:48 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) |
|
|
|
|
3, 0, 0xffffffff,
|
2008-09-25 20:16:18 +02:00
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
|
2012-04-28 17:33:48 +02:00
|
|
|
cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) |
|
|
|
|
3, 0, 0xffffffff,
|
2008-09-25 20:16:18 +02:00
|
|
|
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
|
|
|
|
DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
|
|
|
|
DESC_W_MASK | DESC_A_MASK);
|
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
ESP = ECX;
|
|
|
|
EIP = EDX;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
target_ulong helper_lsl(CPUX86State *env, target_ulong selector1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
unsigned int limit;
|
|
|
|
uint32_t e1, e2, eflags, selector;
|
|
|
|
int rpl, dpl, cpl, type;
|
|
|
|
|
|
|
|
selector = selector1 & 0xffff;
|
2012-04-29 14:45:34 +02:00
|
|
|
eflags = cpu_cc_compute_all(env, CC_OP);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2009-03-20 17:13:41 +01:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (e2 & DESC_S_MASK) {
|
|
|
|
if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
|
|
|
|
/* conforming */
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 9:
|
|
|
|
case 11:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (dpl < cpl || dpl < rpl) {
|
|
|
|
fail:
|
|
|
|
CC_SRC = eflags & ~CC_Z;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
limit = get_seg_limit(e1, e2);
|
|
|
|
CC_SRC = eflags | CC_Z;
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
target_ulong helper_lar(CPUX86State *env, target_ulong selector1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t e1, e2, eflags, selector;
|
|
|
|
int rpl, dpl, cpl, type;
|
|
|
|
|
|
|
|
selector = selector1 & 0xffff;
|
2012-04-29 14:45:34 +02:00
|
|
|
eflags = cpu_cc_compute_all(env, CC_OP);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (e2 & DESC_S_MASK) {
|
|
|
|
if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
|
|
|
|
/* conforming */
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
|
2012-04-28 17:33:48 +02:00
|
|
|
switch (type) {
|
2008-05-28 14:51:20 +02:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 9:
|
|
|
|
case 11:
|
|
|
|
case 12:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (dpl < cpl || dpl < rpl) {
|
|
|
|
fail:
|
|
|
|
CC_SRC = eflags & ~CC_Z;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CC_SRC = eflags | CC_Z;
|
|
|
|
return e2 & 0x00f0ff00;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_verr(CPUX86State *env, target_ulong selector1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t e1, e2, eflags, selector;
|
|
|
|
int rpl, dpl, cpl;
|
|
|
|
|
|
|
|
selector = selector1 & 0xffff;
|
2012-04-29 14:45:34 +02:00
|
|
|
eflags = cpu_cc_compute_all(env, CC_OP);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (e2 & DESC_CS_MASK) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (!(e2 & DESC_R_MASK)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(e2 & DESC_C_MASK)) {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dpl < cpl || dpl < rpl) {
|
|
|
|
fail:
|
|
|
|
CC_SRC = eflags & ~CC_Z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CC_SRC = eflags | CC_Z;
|
|
|
|
}
|
|
|
|
|
2012-04-29 21:47:06 +02:00
|
|
|
void helper_verw(CPUX86State *env, target_ulong selector1)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
|
|
|
uint32_t e1, e2, eflags, selector;
|
|
|
|
int rpl, dpl, cpl;
|
|
|
|
|
|
|
|
selector = selector1 & 0xffff;
|
2012-04-29 14:45:34 +02:00
|
|
|
eflags = cpu_cc_compute_all(env, CC_OP);
|
2012-04-28 17:33:48 +02:00
|
|
|
if ((selector & 0xfffc) == 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2012-04-29 21:47:06 +02:00
|
|
|
if (load_segment(env, &e1, &e2, selector) != 0) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
|
|
|
if (!(e2 & DESC_S_MASK)) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
rpl = selector & 3;
|
|
|
|
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
|
|
|
|
cpl = env->hflags & HF_CPL_MASK;
|
|
|
|
if (e2 & DESC_CS_MASK) {
|
|
|
|
goto fail;
|
|
|
|
} else {
|
2012-04-28 17:33:48 +02:00
|
|
|
if (dpl < cpl || dpl < rpl) {
|
2008-05-28 14:51:20 +02:00
|
|
|
goto fail;
|
2012-04-28 17:33:48 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
if (!(e2 & DESC_W_MASK)) {
|
|
|
|
fail:
|
|
|
|
CC_SRC = eflags & ~CC_Z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CC_SRC = eflags | CC_Z;
|
|
|
|
}
|
|
|
|
|
2012-04-28 22:21:41 +02:00
|
|
|
#if defined(CONFIG_USER_ONLY)
|
2012-04-29 21:47:06 +02:00
|
|
|
void cpu_x86_load_seg(CPUX86State *env, int seg_reg, int selector)
|
2008-05-28 14:51:20 +02:00
|
|
|
{
|
2012-04-28 22:21:41 +02:00
|
|
|
if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
|
|
|
|
selector &= 0xffff;
|
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, selector,
|
|
|
|
(selector << 4), 0xffff, 0);
|
|
|
|
} else {
|
2012-04-29 21:47:06 +02:00
|
|
|
helper_load_seg(env, seg_reg, selector);
|
2011-04-20 13:04:23 +02:00
|
|
|
}
|
2008-05-28 14:51:20 +02:00
|
|
|
}
|
|
|
|
#endif
|