target/i386: implement SYSCALL/SYSRET in 32-bit emulators

AMD supports both 32-bit and 64-bit SYSCALL/SYSRET, but the TCG only
exposes it for 64-bit targets.  For system emulation just reuse the
helper; for user-mode emulation the ABI is the same as "int $80".

The BSDs does not support any fast system call mechanism in 32-bit
mode so add to bsd-user the same stub that FreeBSD has for 64-bit
compatibility mode.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
master
Paolo Bonzini 2023-06-17 01:01:29 +02:00
parent 6750485bf4
commit 63fd8ef080
8 changed files with 20 additions and 17 deletions

View File

@ -164,6 +164,10 @@ static inline void target_cpu_loop(CPUX86State *env)
} }
break; break;
case EXCP_SYSCALL:
/* doesn't do anything */
break;
case EXCP_INTERRUPT: case EXCP_INTERRUPT:
/* just indicate that signals should be handled asap */ /* just indicate that signals should be handled asap */
break; break;

View File

@ -211,6 +211,9 @@ void cpu_loop(CPUX86State *env)
switch(trapnr) { switch(trapnr) {
case 0x80: case 0x80:
#ifndef TARGET_X86_64
case EXCP_SYSCALL:
#endif
/* linux syscall from int $0x80 */ /* linux syscall from int $0x80 */
ret = do_syscall(env, ret = do_syscall(env,
env->regs[R_EAX], env->regs[R_EAX],
@ -227,9 +230,9 @@ void cpu_loop(CPUX86State *env)
env->regs[R_EAX] = ret; env->regs[R_EAX] = ret;
} }
break; break;
#ifndef TARGET_ABI32 #ifdef TARGET_X86_64
case EXCP_SYSCALL: case EXCP_SYSCALL:
/* linux syscall from syscall instruction */ /* linux syscall from syscall instruction. */
ret = do_syscall(env, ret = do_syscall(env,
env->regs[R_EAX], env->regs[R_EAX],
env->regs[R_EDI], env->regs[R_EDI],
@ -245,8 +248,6 @@ void cpu_loop(CPUX86State *env)
env->regs[R_EAX] = ret; env->regs[R_EAX] = ret;
} }
break; break;
#endif
#ifdef TARGET_X86_64
case EXCP_VSYSCALL: case EXCP_VSYSCALL:
emulate_vsyscall(env); emulate_vsyscall(env);
break; break;

View File

@ -637,7 +637,7 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER */ CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER */
#ifdef TARGET_X86_64 #ifdef TARGET_X86_64
#define TCG_EXT2_X86_64_FEATURES (CPUID_EXT2_SYSCALL | CPUID_EXT2_LM) #define TCG_EXT2_X86_64_FEATURES CPUID_EXT2_LM
#else #else
#define TCG_EXT2_X86_64_FEATURES 0 #define TCG_EXT2_X86_64_FEATURES 0
#endif #endif
@ -645,7 +645,7 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
#define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \ #define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \
CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \ CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \
CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_PDPE1GB | \ CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_PDPE1GB | \
TCG_EXT2_X86_64_FEATURES) CPUID_EXT2_SYSCALL | TCG_EXT2_X86_64_FEATURES)
#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \ #define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \
CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A | \ CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A | \
CPUID_EXT3_3DNOWPREFETCH) CPUID_EXT3_3DNOWPREFETCH)

View File

@ -51,10 +51,8 @@ DEF_HELPER_FLAGS_2(get_dr, TCG_CALL_NO_WG, tl, env, int)
DEF_HELPER_1(sysenter, void, env) DEF_HELPER_1(sysenter, void, env)
DEF_HELPER_2(sysexit, void, env, int) DEF_HELPER_2(sysexit, void, env, int)
#ifdef TARGET_X86_64
DEF_HELPER_2(syscall, void, env, int) DEF_HELPER_2(syscall, void, env, int)
DEF_HELPER_2(sysret, void, env, int) DEF_HELPER_2(sysret, void, env, int)
#endif
DEF_HELPER_FLAGS_2(pause, TCG_CALL_NO_WG, noreturn, env, int) DEF_HELPER_FLAGS_2(pause, TCG_CALL_NO_WG, noreturn, env, int)
DEF_HELPER_FLAGS_3(raise_interrupt, TCG_CALL_NO_WG, noreturn, env, int, int) DEF_HELPER_FLAGS_3(raise_interrupt, TCG_CALL_NO_WG, noreturn, env, int, int)
DEF_HELPER_FLAGS_2(raise_exception, TCG_CALL_NO_WG, noreturn, env, int) DEF_HELPER_FLAGS_2(raise_exception, TCG_CALL_NO_WG, noreturn, env, int)

View File

@ -977,6 +977,7 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
e2); e2);
env->eip = offset; env->eip = offset;
} }
#endif /* TARGET_X86_64 */
void helper_sysret(CPUX86State *env, int dflag) void helper_sysret(CPUX86State *env, int dflag)
{ {
@ -990,6 +991,7 @@ void helper_sysret(CPUX86State *env, int dflag)
raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC()); raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
} }
selector = (env->star >> 48) & 0xffff; selector = (env->star >> 48) & 0xffff;
#ifdef TARGET_X86_64
if (env->hflags & HF_LMA_MASK) { if (env->hflags & HF_LMA_MASK) {
cpu_load_eflags(env, (uint32_t)(env->regs[11]), TF_MASK | AC_MASK cpu_load_eflags(env, (uint32_t)(env->regs[11]), TF_MASK | AC_MASK
| ID_MASK | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | | ID_MASK | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK |
@ -1015,7 +1017,9 @@ void helper_sysret(CPUX86State *env, int dflag)
DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
DESC_S_MASK | (3 << DESC_DPL_SHIFT) | DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
DESC_W_MASK | DESC_A_MASK); DESC_W_MASK | DESC_A_MASK);
} else { } else
#endif
{
env->eflags |= IF_MASK; env->eflags |= IF_MASK;
cpu_x86_load_seg_cache(env, R_CS, selector | 3, cpu_x86_load_seg_cache(env, R_CS, selector | 3,
0, 0xffffffff, 0, 0xffffffff,
@ -1030,7 +1034,6 @@ void helper_sysret(CPUX86State *env, int dflag)
DESC_W_MASK | DESC_A_MASK); DESC_W_MASK | DESC_A_MASK);
} }
} }
#endif /* TARGET_X86_64 */
/* real mode interrupt */ /* real mode interrupt */
static void do_interrupt_real(CPUX86State *env, int intno, int is_int, static void do_interrupt_real(CPUX86State *env, int intno, int is_int,

View File

@ -26,7 +26,6 @@
#include "tcg/helper-tcg.h" #include "tcg/helper-tcg.h"
#include "../seg_helper.h" #include "../seg_helper.h"
#ifdef TARGET_X86_64
void helper_syscall(CPUX86State *env, int next_eip_addend) void helper_syscall(CPUX86State *env, int next_eip_addend)
{ {
int selector; int selector;
@ -35,6 +34,7 @@ void helper_syscall(CPUX86State *env, int next_eip_addend)
raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC()); raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
} }
selector = (env->star >> 32) & 0xffff; selector = (env->star >> 32) & 0xffff;
#ifdef TARGET_X86_64
if (env->hflags & HF_LMA_MASK) { if (env->hflags & HF_LMA_MASK) {
int code64; int code64;
@ -61,7 +61,9 @@ void helper_syscall(CPUX86State *env, int next_eip_addend)
} else { } else {
env->eip = env->cstar; env->eip = env->cstar;
} }
} else { } else
#endif
{
env->regs[R_ECX] = (uint32_t)(env->eip + next_eip_addend); env->regs[R_ECX] = (uint32_t)(env->eip + next_eip_addend);
env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK); env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
@ -78,7 +80,6 @@ void helper_syscall(CPUX86State *env, int next_eip_addend)
env->eip = (uint32_t)env->star; env->eip = (uint32_t)env->star;
} }
} }
#endif /* TARGET_X86_64 */
void handle_even_inj(CPUX86State *env, int intno, int is_int, void handle_even_inj(CPUX86State *env, int intno, int is_int,
int error_code, int is_hw, int rm) int error_code, int is_hw, int rm)

View File

@ -5704,7 +5704,6 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)
s->base.is_jmp = DISAS_EOB_ONLY; s->base.is_jmp = DISAS_EOB_ONLY;
} }
break; break;
#ifdef TARGET_X86_64
case 0x105: /* syscall */ case 0x105: /* syscall */
/* For Intel SYSCALL is only valid in long mode */ /* For Intel SYSCALL is only valid in long mode */
if (!LMA(s) && env->cpuid_vendor1 == CPUID_VENDOR_INTEL_1) { if (!LMA(s) && env->cpuid_vendor1 == CPUID_VENDOR_INTEL_1) {
@ -5738,7 +5737,6 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)
gen_eob_worker(s, false, true); gen_eob_worker(s, false, true);
} }
break; break;
#endif
case 0x1a2: /* cpuid */ case 0x1a2: /* cpuid */
gen_update_cc_op(s); gen_update_cc_op(s);
gen_update_eip_cur(s); gen_update_eip_cur(s);

View File

@ -26,7 +26,6 @@
#include "tcg/helper-tcg.h" #include "tcg/helper-tcg.h"
#include "tcg/seg_helper.h" #include "tcg/seg_helper.h"
#ifdef TARGET_X86_64
void helper_syscall(CPUX86State *env, int next_eip_addend) void helper_syscall(CPUX86State *env, int next_eip_addend)
{ {
CPUState *cs = env_cpu(env); CPUState *cs = env_cpu(env);
@ -36,7 +35,6 @@ void helper_syscall(CPUX86State *env, int next_eip_addend)
env->exception_next_eip = env->eip + next_eip_addend; env->exception_next_eip = env->eip + next_eip_addend;
cpu_loop_exit(cs); cpu_loop_exit(cs);
} }
#endif /* TARGET_X86_64 */
/* /*
* fake user mode interrupt. is_int is TRUE if coming from the int * fake user mode interrupt. is_int is TRUE if coming from the int