target/i386: add a few simple features
PATCH v2 2/10 target/i386: TCG supports 3DNow! prefetch(w)
In reply to Paolo BonziniThe AMD prefetch(w) instructions have not been deprecated together with the rest of 3DNow!, and in fact are even supported by newer Intel processor. Mark them as supported by TCG, as it supports all of 3DNow!.
--- target/i386/cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/cpu.c +2 -1
@@ -647,7 +647,8 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_PDPE1GB | \ TCG_EXT2_X86_64_FEATURES)#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)#define TCG_EXT4_FEATURES 0#define TCG_SVM_FEATURES (CPUID_SVM_NPT | CPUID_SVM_VGIF | \ CPUID_SVM_SVME_ADDR_CHK)
Signature · 1 line
2.40.1
PATCH v2 9/10 target/i386: implement 32-bit SYSENTER for linux-user
In reply to Paolo BonziniTCG reports the SEP feature (SYSENTER/SYSEXIT) in user mode emulation, but does not plumb it into the linux-user run loop. Split the helper into system emulation and user-mode emulation cases and implement the latter.
SYSENTER does not have the best design for a kernel-mode entry instruction, and therefore Linux always makes it return to the vsyscall page. Because QEMU does not provide the _contents_ of the vsyscall page, the instructions executed after SYSEXIT have to be emulated by hand until the first RET.
Some corner cases, such as restarting the system call after the system call has rewritten the SYSENTER instruction, are not emulated correctly. On Linux, the system call restart uses the SYSENTER call in the vsyscall page, while on QEMU it uses the emulated program's instruction.
--- linux-user/i386/cpu_loop.c | 51 +++++++++++++++++++++++++++-- target/i386/cpu.c | 9 ++++- target/i386/cpu.h | 1 + target/i386/helper.h | 2 +- target/i386/tcg/seg_helper.c | 33 ------------------- target/i386/tcg/sysemu/seg_helper.c | 33 +++++++++++++++++++ target/i386/tcg/translate.c | 2 +- target/i386/tcg/user/seg_helper.c | 16 +++++++++ 8 files changed, 109 insertions(+), 38 deletions(-)
linux-user/i386/cpu_loop.c +49 -2
@@ -197,6 +197,41 @@ static bool maybe_handle_vm86_trap(CPUX86State *env, int trapnr)return false; } +static void emulate_vsyscall_sysexit (CPUX86State *env) +{ +/* +* Emulate the pop and ret instructions after the sysenter instruction +* in the vsyscall page. Any sysenter returns there, because sysenter +* does not save the old EIP! +*/ +abi_ulong word; +if (get_user_ual (word, env->regs[R_ESP ])) { + goto segv; + } + env->regs[R_EBP ] = word; + env->regs[R_ESP ] +=sizeof (target_ulong); +if (get_user_ual (word, env->regs[R_ESP ])) { + goto segv; + } + env->regs[R_EDX ] = word; + env->regs[R_ESP ] +=sizeof (target_ulong); +if (get_user_ual (word, env->regs[R_ESP ])) { + goto segv; + } + env->regs[R_ECX ] = word; + env->regs[R_ESP ] +=sizeof (target_ulong); +if (get_user_ual (word, env->regs[R_ESP ])) { + goto segv; + } + env->eip = word; + env->regs[R_ESP ] +=sizeof (target_ulong); +return ; + +segv: + env->error_code =PG_ERROR_W_MASK |PG_ERROR_U_MASK ; +force_sig_fault (TARGET_SIGSEGV ,TARGET_SEGV_MAPERR , env->regs[R_ESP ]); +} +void cpu_loop (CPUX86State *env) {CPUState *cs =env_cpu (env);
@@ -213,6 +248,7 @@ void cpu_loop(CPUX86State *env)case 0x80 :#ifdef TARGET_ABI32 case EXCP_SYSCALL : +case EXCP_SYSENTER :#endif /* linux syscall from int $0x80 */ ret = do_syscall(env,
@@ -226,12 +262,18 @@ void cpu_loop(CPUX86State *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->eip -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { +break ; + } +if (ret != -QEMU_ESIGRETURN ) { env->regs[R_EAX ] = ret; } +if (trapnr ==EXCP_SYSENTER ) { +emulate_vsyscall_sysexit (env); + }break ;#ifndef TARGET_ABI32 case EXCP_SYSCALL : +case EXCP_SYSENTER :/* linux syscall from syscall instruction */ ret = do_syscall(env, env->regs[R_EAX],
@@ -244,9 +286,14 @@ void cpu_loop(CPUX86State *env) 0, 0);if (ret == -QEMU_ERESTARTSYS ) { env->eip -=2 ; - }else if (ret != -QEMU_ESIGRETURN ) { +break ; + } +if (ret != -QEMU_ESIGRETURN ) { env->regs[R_EAX ] = ret; } +if (trapnr ==EXCP_SYSENTER ) { +emulate_vsyscall_sysexit (env); + }break ;#endif #ifdef TARGET_X86_64
target/i386/cpu.c +8 -1
@@ -614,11 +614,18 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, CPUID_PAT | CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2 | \ CPUID_PAE | CPUID_SEP | CPUID_APIC) +#if definedCONFIG_SOFTMMU || definedCONFIG_LINUX_USER +#define TCG_NOBSD_FEATURES CPUID_SEP +#else +#define TCG_NOBSD_FEATURES 0 +#endif +#define TCG_FEATURES (CPUID_FP87 | CPUID_PSE | CPUID_TSC | CPUID_MSR | \ CPUID_PAE | CPUID_MCE | CPUID_CX8 | CPUID_APIC | CPUID_SEP | \ CPUID_MTRR | CPUID_PGE | CPUID_MCA | CPUID_CMOV | CPUID_PAT | \ CPUID_PSE36 | CPUID_CLFLUSH | CPUID_ACPI | CPUID_MMX | \ - CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS | CPUID_DE) + CPUID_FXSR | CPUID_SSE | CPUID_SSE2 | CPUID_SS | CPUID_DE | \ + TCG_NOBSD_FEATURES)/* partly implemented: CPUID_MTRR, CPUID_MCA, CPUID_CLFLUSH (needed for Win64) */ /* missing:
target/i386/cpu.h +1 -0
@@ -1185,6 +1185,7 @@ uint64_t x86_cpu_get_supported_feature_word(FeatureWord w,#define EXCP_VMEXIT 0x100/* only for system emulation */ #define EXCP_SYSCALL 0x101/* only for user emulation */ #define EXCP_VSYSCALL 0x102/* only for user emulation */ +#define EXCP_SYSENTER 0x103/* only for user emulation */ /* i386-specific interrupt pending bits. */ #define CPU_INTERRUPT_POLL CPU_INTERRUPT_TGT_EXT_1
target/i386/helper.h +1 -1
@@ -49,7 +49,7 @@ DEF_HELPER_FLAGS_3(set_dr, TCG_CALL_NO_WG, void, env, int, tl) DEF_HELPER_FLAGS_2(get_dr, TCG_CALL_NO_WG, tl, env, int)#endif /* !CONFIG_USER_ONLY */ -DEF_HELPER_1(sysenter, void, env) +DEF_HELPER_2(sysenter, void, env, int) DEF_HELPER_2(sysexit, void, env, int) DEF_HELPER_2(syscall, void, env, int)#ifdef TARGET_X86_64
target/i386/tcg/seg_helper.c +0 -33
@@ -2147,39 +2147,6 @@ void helper_lret_protected(CPUX86State *env, int shift, int addend)helper_ret_protected (env, shift,0 , addend,GETPC ()); } -void helper_sysenter (CPUX86State *env) -{ -if (env->sysenter_cs ==0 ) { -raise_exception_err_ra (env,EXCP0D_GPF ,0 ,GETPC ()); - } - env->eflags &= ~(VM_MASK |IF_MASK |RF_MASK ); - -#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 | -DESC_CS_MASK |DESC_R_MASK |DESC_A_MASK | -DESC_L_MASK ); - } 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 ); - } -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 ); - env->regs[R_ESP ] = env->sysenter_esp; - env->eip = env->sysenter_eip; -} -void helper_sysexit (CPUX86State *env,int dflag) {int cpl;
target/i386/tcg/sysemu/seg_helper.c +33 -0
@@ -215,3 +215,36 @@ void helper_check_io(CPUX86State *env, uint32_t addr, uint32_t size)raise_exception_err_ra (env,EXCP0D_GPF ,0 , retaddr); } } + +void helper_sysenter (CPUX86State *env,int next_eip_addend) +{ +if (env->sysenter_cs ==0 ) { +raise_exception_err_ra (env,EXCP0D_GPF ,0 ,GETPC ()); + } + env->eflags &= ~(VM_MASK |IF_MASK |RF_MASK ); + +#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 | +DESC_CS_MASK |DESC_R_MASK |DESC_A_MASK | +DESC_L_MASK ); + } 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 ); + } +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 ); + env->regs[R_ESP ] = env->sysenter_esp; + env->eip = env->sysenter_eip; +}
target/i386/tcg/translate.c +1 -1
@@ -5667,7 +5667,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)if (!PE (s)) {gen_exception_gpf (s); }else { -gen_helper_sysenter (cpu_env); +gen_helper_sysenter (cpu_env,cur_insn_len_i32 (s)); s->base.is_jmp =DISAS_EOB_ONLY ; }break ;
target/i386/tcg/user/seg_helper.c +16 -0
@@ -36,6 +36,22 @@ void helper_syscall(CPUX86State *env, int next_eip_addend)cpu_loop_exit (cs); } +void helper_sysenter (CPUX86State *env,int next_eip_addend) +{ +CPUState *cs =env_cpu (env); + +/* +* sysenter returns to the landing pad of the vDSO, which pops +* ebp/edx/ecx before executing a "ret". +*/ + cs->exception_index =EXCP_SYSENTER ; + env->exception_is_int =0 ; + +/* Used for ERESTARTSYS. */ + env->exception_next_eip = env->eip + next_eip_addend; +cpu_loop_exit (cs); +} +/* * fake user mode interrupt. is_int is TRUE if coming from the int * instruction. next_eip is the env->eip value AFTER the interrupt
Signature · 1 line
2.40.1
PATCH v2 4/10 target/i386: TCG supports XSAVEERPTR
In reply to Paolo BonziniXSAVEERPTR is actually a fix for an errata; TCG does not have the issue.
--- target/i386/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
target/i386/cpu.c +3 -1
@@ -678,6 +678,8 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_SGX_12_0_EBX_FEATURES 0#define TCG_SGX_12_1_EAX_FEATURES 0 +#define TCG_8000_0008_EBX CPUID_8000_0008_EBX_XSAVEERPTR +FeatureWordInfo feature_word_info[FEATURE_WORDS ] = { [FEAT_1_EDX ] = { .type =CPUID_FEATURE_WORD ,
@@ -939,7 +941,7 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = { "amd-psfd", NULL, NULL, NULL, }, .cpuid = { .eax = 0x80000008, .reg = R_EBX, }, - .tcg_features =0 , + .tcg_features =TCG_8000_0008_EBX , .unmigratable_flags = 0, }, [FEAT_8000_0021_EAX] = {
Signature · 1 line
2.40.1
v2 cover target/i386: add a few simple features
TCG is not reporting a few features that are actually already implemented, or that are easy to implement. Add them.
Paolo
v1->v2:
- fix WBNOINVD vmexit
- correctly implement 32-bit SYSCALL and SYSENTER
- do not include linux-user/ from target/
Paolo Bonzini (10):
target/i386: fix INVD vmexit
target/i386: TCG supports 3DNow! prefetch(w) target/i386: TCG supports RDSEED target/i386: TCG supports XSAVEERPTR target/i386: TCG supports WBNOINVD target/i386: Intel only supports SYSCALL in long mode target/i386: sysret and sysexit are privileged target/i386: implement 32-bit SYSCALL for linux-user target/i386: implement 32-bit SYSENTER for linux-user target/i386: implement RDPID in TCG
linux-user/i386/cpu_loop.c | 54 +++++++++++++++++++++++++++-- meson.build | 1 + target/i386/cpu.c | 44 ++++++++++++++++++----- target/i386/cpu.h | 1 + target/i386/helper.h | 6 ++-- target/i386/tcg/misc_helper.c | 23 ++++++++---- target/i386/tcg/seg_helper.c | 33 ------------------ target/i386/tcg/sysemu/seg_helper.c | 33 ++++++++++++++++++ target/i386/tcg/translate.c | 32 ++++++++++++----- target/i386/tcg/user/seg_helper.c | 18 ++++++++-- 10 files changed, 182 insertions(+), 63 deletions(-)
Signature · 1 line
2.40.1
PATCH v2 8/10 target/i386: implement 32-bit SYSCALL for linux-user
TCG supports both 32-bit and 64-bit SYSCALL, but the linux-user code only exposes it for 64-bit. The ABI is the same as "int $80", so expose it even for 32-bit emulators, where it can be used if the vendor is specified as AMD.
--- linux-user/i386/cpu_loop.c | 3 +++ target/i386/cpu.c | 8 +++++++- target/i386/helper.h | 2 +- target/i386/tcg/translate.c | 2 +- target/i386/tcg/user/seg_helper.c | 2 -- 5 files changed, 12 insertions(+), 5 deletions(-)
linux-user/i386/cpu_loop.c +3 -0
@@ -211,6 +211,9 @@ void cpu_loop(CPUX86State *env)switch (trapnr) {case 0x80 : +#ifdef TARGET_ABI32 +case EXCP_SYSCALL : +#endif /* linux syscall from int $0x80 */ ret = do_syscall(env, env->regs[R_EAX],
target/i386/cpu.c +7 -1
@@ -642,10 +642,16 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_EXT2_X86_64_FEATURES 0#endif +#if definedCONFIG_SOFTMMU || definedCONFIG_LINUX_USER +#define TCG_EXT2_NOBSD_FEATURES CPUID_EXT2_SYSCALL +#else +#define TCG_EXT2_NOBSD_FEATURES 0 +#endif +#define TCG_EXT2_FEATURES ((TCG_FEATURES & CPUID_EXT2_AMD_ALIASES) | \ CPUID_EXT2_NX | CPUID_EXT2_MMXEXT | CPUID_EXT2_RDTSCP | \ CPUID_EXT2_3DNOW | CPUID_EXT2_3DNOWEXT | CPUID_EXT2_PDPE1GB | \ - TCG_EXT2_X86_64_FEATURES) + TCG_EXT2_NOBSD_FEATURES | TCG_EXT2_X86_64_FEATURES)#define TCG_EXT3_FEATURES (CPUID_EXT3_LAHF_LM | CPUID_EXT3_SVM | \ CPUID_EXT3_CR8LEG | CPUID_EXT3_ABM | CPUID_EXT3_SSE4A | \ CPUID_EXT3_3DNOWPREFETCH)
target/i386/helper.h +1 -1
@@ -51,8 +51,8 @@ DEF_HELPER_FLAGS_2(get_dr, TCG_CALL_NO_WG, tl, env, int) DEF_HELPER_1(sysenter, void, env) DEF_HELPER_2(sysexit, void, env, int) -#ifdef TARGET_X86_64 DEF_HELPER_2(syscall, void, env, int) +#ifdef TARGET_X86_64 DEF_HELPER_2(sysret, void, env, int)#endif DEF_HELPER_FLAGS_2 (pause,TCG_CALL_NO_WG , noreturn, env, int)
target/i386/tcg/translate.c +1 -1
@@ -5682,7 +5682,6 @@ static bool disas_insn(DisasContext *s, CPUState *cpu) s->base.is_jmp =DISAS_EOB_ONLY ; }break ; -#ifdef TARGET_X86_64 case 0x105 :/* syscall */ /* For Intel SYSCALL is only valid in 64-bit */ if (!LMA (s) && env->cpuid_vendor1 ==CPUID_VENDOR_INTEL_1 ) {
@@ -5696,6 +5695,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)generated after one has entered CPL0 if TF is set in FMASK. */ gen_eob_worker (s, false, true);break ; +#ifdef TARGET_X86_64 case 0x107 :/* sysret */ if (!PE (s) ||CPL (s) !=0 ) {gen_exception_gpf (s);
target/i386/tcg/user/seg_helper.c +0 -2
@@ -26,7 +26,6 @@#include "tcg/helper-tcg.h" #include "tcg/seg_helper.h" -#ifdef TARGET_X86_64 void helper_syscall (CPUX86State *env,int next_eip_addend) {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;cpu_loop_exit (cs); } -#endif /* TARGET_X86_64 */ /* * fake user mode interrupt. is_int is TRUE if coming from the int
Signature · 1 line
2.40.1
PATCH v2 6/10 target/i386: Intel only supports SYSCALL in long mode
In reply to Paolo Bonzini--- target/i386/cpu.c | 4 ++++ target/i386/tcg/translate.c | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-)
target/i386/cpu.c +4 -0
@@ -6238,6 +6238,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx |=1 <<1 ;/* CmpLegacy bit */ } } +if (tcg_enabled () && env->cpuid_vendor1 ==CPUID_VENDOR_INTEL_1 && + !(env->hflags &HF_LMA_MASK )) { + *edx &= ~CPUID_EXT2_SYSCALL ; + }break ;case 0x80000002 :case 0x80000003 :
target/i386/tcg/translate.c +4 -1
@@ -5684,7 +5684,10 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)break ;#ifdef TARGET_X86_64 case 0x105 :/* syscall */ -/* XXX: is it usable in real mode ? */ +/* For Intel SYSCALL is only valid in long mode */ +if (!LMA (s) && env->cpuid_vendor1 ==CPUID_VENDOR_INTEL_1 ) { + goto illegal_op; + }gen_update_cc_op (s);gen_update_eip_cur (s);gen_helper_syscall (cpu_env,cur_insn_len_i32 (s));
Signature · 1 line
2.40.1
PATCH v2 10/10 target/i386: implement RDPID in TCG
In reply to Paolo BonziniRDPID corresponds to a RDMSR(TSC_AUX); however, it is unprivileged so for user-mode emulation we must provide the value that the kernel places in the MSR. For Linux, it is a combination of the current CPU and the current NUMA node, both of which can be retrieved with getcpu(2). For BSD, just return 0.
RDTSCP is reimplemented as RDTSC + RDPID ECX; the differences in terms of serializability are not relevant to QEMU.
--- meson.build | 1 + target/i386/cpu.c | 10 +++++++++- target/i386/helper.h | 2 +- target/i386/tcg/misc_helper.c | 23 +++++++++++++++++------ target/i386/tcg/translate.c | 15 +++++++++++++-- 5 files changed, 41 insertions(+), 10 deletions(-)
meson.build +1 -0
@@ -2230,6 +2230,7 @@ config_host_data.set('CONFIG_CLOCK_ADJTIME', cc.has_function('clock_adjtime')) config_host_data.set('CONFIG_DUP3', cc.has_function('dup3')) config_host_data.set('CONFIG_FALLOCATE', cc.has_function('fallocate')) config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate')) +config_host_data.set('CONFIG_GETCPU', cc.has_function('getcpu', prefix: gnu_source_prefix)) # Note that we need to specify prefix: here to avoid incorrectly # thinking that Windows has posix_memalign() config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include <stdlib.h>'))
target/i386/cpu.c +9 -1
@@ -674,9 +674,17 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,/* missing: CPUID_7_0_EBX_HLE CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM */ + +#if definedCONFIG_SOFTMMU || definedCONFIG_LINUX +#define TCG_7_0_ECX_RDPID CPUID_7_0_ECX_RDPID +#else +#define TCG_7_0_ECX_RDPID 0 +#endif #define TCG_7_0_ECX_FEATURES (CPUID_7_0_ECX_UMIP | CPUID_7_0_ECX_PKU | \ /* CPUID_7_0_ECX_OSPKE is dynamic */ \ -CPUID_7_0_ECX_LA57 | CPUID_7_0_ECX_PKS | CPUID_7_0_ECX_VAES) +CPUID_7_0_ECX_LA57 | CPUID_7_0_ECX_PKS | CPUID_7_0_ECX_VAES | \ + TCG_7_0_ECX_RDPID) +#define TCG_7_0_EDX_FEATURES CPUID_7_0_EDX_FSRM#define TCG_7_1_EAX_FEATURES (CPUID_7_1_EAX_FZRM | CPUID_7_1_EAX_FSRS | \ CPUID_7_1_EAX_FSRC)
target/i386/helper.h +1 -1
@@ -69,8 +69,8 @@ DEF_HELPER_2(into, void, env, int) DEF_HELPER_FLAGS_1(single_step, TCG_CALL_NO_WG, noreturn, env) DEF_HELPER_1(rechecking_single_step, void, env) DEF_HELPER_1(cpuid, void, env) +DEF_HELPER_FLAGS_1(rdpid, TCG_CALL_NO_WG, tl, env) DEF_HELPER_1(rdtsc, void, env) -DEF_HELPER_1(rdtscp, void, env) DEF_HELPER_FLAGS_1(rdpmc, TCG_CALL_NO_WG, noreturn, env)#ifndef CONFIG_USER_ONLY
target/i386/tcg/misc_helper.c +13 -6
@@ -75,12 +79,6 @@ void helper_rdtsc(CPUX86State *env) env->regs[R_EDX ] = (uint32_t )(val >>32 ); } -void helper_rdtscp (CPUX86State *env) -{ -helper_rdtsc (env); - env->regs[R_ECX ] = (uint32_t )(env->tsc_aux); -} - G_NORETURN void helper_rdpmc(CPUX86State *env) { if (((env->cr[4] & CR4_PCE_MASK) == 0 ) &&
@@ -137,3 +135,16 @@ void helper_wrpkru(CPUX86State *env, uint32_t ecx, uint64_t val) env->pkru = val;tlb_flush (cs); } + +target_ulong HELPER (rdpid )(CPUX86State *env) +{ +#if definedCONFIG_SOFTMMU +return env->tsc_aux; +#elif definedCONFIG_LINUX && definedCONFIG_GETCPU +unsigned cpu, node; +getcpu (&cpu, &node); +return (node <<12 ) | (cpu &0xfff ); +#else +return 0 ; +#endif +}
target/i386/tcg/translate.c +13 -2
@@ -3924,7 +3924,16 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)gen_cmpxchg8b (s, env, modrm);break ; -case 7 :/* RDSEED */ +case 7 :/* RDSEED, RDPID with f3 prefix */ +if (mod ==3 && !(s->prefix &PREFIX_LOCK ) && + (s->prefix &PREFIX_REPZ ) && + (s->cpuid_ext_features &CPUID_7_0_ECX_RDPID )) { +gen_helper_rdpid (s->T0, cpu_env); + rm = (modrm &7 ) |REX_B (s); +gen_op_mov_reg_v (s, dflag, rm, s->T0); +break ; + } +/* fallthrough */ case 6 :/* RDRAND */ if (mod != 3 || (s->prefix & (PREFIX_LOCK | PREFIX_REPZ | PREFIX_REPNZ)) ||
@@ -6111,7 +6120,9 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)gen_update_cc_op (s);gen_update_eip_cur (s);translator_io_start (&s->base); -gen_helper_rdtscp (cpu_env); +gen_helper_rdtsc (cpu_env); +gen_helper_rdpid (s->T0, cpu_env); +gen_op_mov_reg_v (s, dflag,R_ECX , s->T0);break ;default :
Signature · 1 line
2.40.1
PATCH v2 1/10 target/i386: fix INVD vmexit
In reply to Paolo BonziniDue to a typo or perhaps a brain fart, the INVD vmexit was never generated. Fix it (but not that fixing just the typo would break both INVD and WBINVD, due to a case of two wrongs making a right).
--- target/i386/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/translate.c +1 -1
@@ -6119,7 +6119,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)case 0x108 :/* invd */ case 0x109 :/* wbinvd */ if (check_cpl0 (s)) { -gen_svm_check_intercept (s, (b &2 ) ?SVM_EXIT_INVD :SVM_EXIT_WBINVD ); +gen_svm_check_intercept (s, (b &1 ) ?SVM_EXIT_WBINVD :SVM_EXIT_INVD );/* nothing to do */ }break ;
Signature · 1 line
2.40.1
PATCH v2 5/10 target/i386: TCG supports WBNOINVD
In reply to Paolo BonziniWBNOINVD is the same as INVD or WBINVD as far as TCG is concerned, since there is no cache in TCG and therefore no invalidation side effect in WBNOINVD.
With respect to SVM emulation, processors that do not support WBNOINVD will ignore the prefix and treat it as WBINVD, while those that support it will generate exactly the same vmexit.
--- target/i386/cpu.c | 3 ++- target/i386/tcg/translate.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
target/i386/cpu.c +2 -1
@@ -678,7 +678,8 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_SGX_12_0_EBX_FEATURES 0#define TCG_SGX_12_1_EAX_FEATURES 0 -#define TCG_8000_0008_EBX CPUID_8000_0008_EBX_XSAVEERPTR +#define TCG_8000_0008_EBX (CPUID_8000_0008_EBX_XSAVEERPTR | \ + CPUID_8000_0008_EBX_WBNOINVD)FeatureWordInfo feature_word_info[FEATURE_WORDS ] = { [FEAT_1_EDX ] = {
target/i386/tcg/translate.c +1 -1
@@ -6117,7 +6117,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)break ;case 0x108 :/* invd */ -case 0x109 :/* wbinvd */ +case 0x109 :/* wbinvd; wbnoinvd with REPZ prefix */ if (check_cpl0 (s)) {gen_svm_check_intercept (s, (b &1 ) ?SVM_EXIT_WBINVD :SVM_EXIT_INVD );/* nothing to do */
Signature · 1 line
2.40.1
PATCH v2 7/10 target/i386: sysret and sysexit are privileged
In reply to Paolo Bonzini--- target/i386/tcg/translate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/tcg/translate.c +2 -2
@@ -5675,7 +5675,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)/* For Intel SYSEXIT is valid on 64-bit */ if (CODE64 (s) && env->cpuid_vendor1 !=CPUID_VENDOR_INTEL_1 ) goto illegal_op; -if (!PE (s)) { +if (!PE (s) ||CPL (s) !=0 ) {gen_exception_gpf (s); }else {gen_helper_sysexit (cpu_env,tcg_constant_i32 (dflag -1 ));
@@ -5697,7 +5697,7 @@ static bool disas_insn(DisasContext *s, CPUState *cpu)gen_eob_worker (s, false, true);break ;case 0x107 :/* sysret */ -if (!PE (s)) { +if (!PE (s) ||CPL (s) !=0 ) {gen_exception_gpf (s); }else {gen_helper_sysret (cpu_env,tcg_constant_i32 (dflag -1 ));
Signature · 1 line
2.40.1
PATCH v2 3/10 target/i386: TCG supports RDSEED
In reply to Paolo BonziniTCG implements RDSEED, and in fact uses qcrypto_random_bytes which is secure enough to match hardware behavior. Expose it to guests.
--- target/i386/cpu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
target/i386/cpu.c +2 -3
@@ -657,11 +657,10 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX | \ CPUID_7_0_EBX_PCOMMIT | CPUID_7_0_EBX_CLFLUSHOPT | \ CPUID_7_0_EBX_CLWB | CPUID_7_0_EBX_MPX | CPUID_7_0_EBX_FSGSBASE | \ - CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2) + CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_RDSEED)/* missing: CPUID_7_0_EBX_HLE -CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM, -CPUID_7_0_EBX_RDSEED */ +CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM */ #define TCG_7_0_ECX_FEATURES (CPUID_7_0_ECX_UMIP | CPUID_7_0_ECX_PKU | \ /* CPUID_7_0_ECX_OSPKE is dynamic */ \CPUID_7_0_ECX_LA57 | CPUID_7_0_ECX_PKS | CPUID_7_0_ECX_VAES)
Signature · 1 line
2.40.1
Re: [PATCH v2 06/10] target/i386: Intel only supports SYSCALL in long mode
In reply to Paolo BonziniQuoted 5 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini<[email protected]>--- target/i386/cpu.c | 4 ++++ target/i386/tcg/translate.c | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-)
r~
Re: [PATCH v2 07/10] target/i386: sysret and sysexit are privileged
In reply to Paolo BonziniQuoted 4 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini<[email protected]>--- target/i386/tcg/translate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
r~
Re: [PATCH v2 08/10] target/i386: implement 32-bit SYSCALL for linux-user
In reply to Paolo Bonzinilinux-user/i386/cpu_loop.c void cpu_loop(CPUX86State *env) · line 217
Quoted 8 lines · Paolo Bonzini
linux-user/i386/cpu_loop.c +3
-0@@ -211,6 +211,9 @@ void cpu_loop(CPUX86State *env)switch (trapnr) {case 0x80 : +#ifdef TARGET_ABI32 +case EXCP_SYSCALL : +#endif /* linux syscall from int $0x80 */
ABI32 includes x32.
I think you want TARGET_I386.
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, · line 649
Quoted 5 lines
target/i386/cpu.c +5
-0+#if definedCONFIG_SOFTMMU || definedCONFIG_LINUX_USER +#define TCG_EXT2_NOBSD_FEATURES CPUID_EXT2_SYSCALL +#else +#define TCG_EXT2_NOBSD_FEATURES 0 +#endif
Missing TARGET_I386 test per above.
Better named TCG_EXT2_USERONLY_FEATURES.
Warner, if I'm reading freebsd-src correctly, sys/x86 (aka 32-bit) does not use sysenter/sysret? I only see that under sys/amd64 (where fast_syscall32 is a cya nop).
r~
Re: [PATCH v2 09/10] target/i386: implement 32-bit SYSENTER for linux-user
In reply to Paolo BonziniQuoted 17 lines · Paolo Bonzini
TCG reports the SEP feature (SYSENTER/SYSEXIT) in user mode emulation, but does not plumb it into the linux-user run loop. Split the helper into system emulation and user-mode emulation cases and implement the latter.
SYSENTER does not have the best design for a kernel-mode entry instruction, and therefore Linux always makes it return to the vsyscall page. Because QEMU does not provide
the_contents_of the vsyscall page, the instructions executed after SYSEXIT have to be emulated by hand until the first RET.Some corner cases, such as restarting the system call after the system call has rewritten the SYSENTER instruction, are not emulated correctly. On Linux, the system call restart uses the SYSENTER call in the vsyscall page, while on QEMU it uses the emulated program's instruction.
Signed-off-by Paolo Bonzini<[email protected]>
Quoted 10 lines
--- linux-user/i386/cpu_loop.c | 51 +++++++++++++++++++++++++++-- target/i386/cpu.c | 9 ++++- target/i386/cpu.h | 1 + target/i386/helper.h | 2 +- target/i386/tcg/seg_helper.c | 33 ------------------- target/i386/tcg/sysemu/seg_helper.c | 33 +++++++++++++++++++ target/i386/tcg/translate.c | 2 +- target/i386/tcg/user/seg_helper.c | 16 +++++++++ 8 files changed, 109 insertions(+), 38 deletions(-)
I'm not keen on this.
This belongs with the rest of the vdso (see patches posted years ago; committing binary blobs rejected, still waiting on a decent way to invoke cross-compilers to build them).
Further, this shouldn't ever be reachable, because AT_SYSINFO won't be present to give the guest libc the location of the vdso routine to call.
r~
Re: [PATCH v2 10/10] target/i386: implement RDPID in TCG
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
RDPID corresponds to a
RDMSR(TSC_AUX); however, it is unprivileged so for user-mode emulation we must provide the value that the kernel places in the MSR. For Linux, it is a combination of the current CPU and the current NUMA node, both of which can be retrieved with getcpu(2). For BSD, just return 0.RDTSCP is reimplemented as RDTSC + RDPID ECX; the differences in terms of serializability are not relevant to QEMU.
Quoted 8 lines
Signed-off-by Paolo Bonzini<[email protected]>--- meson.build | 1 + target/i386/cpu.c | 10 +++++++++- target/i386/helper.h | 2 +- target/i386/tcg/misc_helper.c | 23 +++++++++++++++++------ target/i386/tcg/translate.c | 15 +++++++++++++-- 5 files changed, 41 insertions(+), 10 deletions(-)
r~
Re: [PATCH v2 03/10] target/i386: TCG supports RDSEED
In reply to Paolo BonziniQuoted 17 lines · Paolo Bonzini
TCG implements RDSEED, and in fact uses
qcrypto_random_byteswhich is secure enough to match hardware behavior. Expose it to guests.Reviewed-by Richard Henderson <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- target/i386/cpu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)target/i386/cpu.c +0
-0@@ -657,11 +657,10 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,CPUID_7_0_EBX_BMI1 |CPUID_7_0_EBX_BMI2 |CPUID_7_0_EBX_ADX | \CPUID_7_0_EBX_PCOMMIT |CPUID_7_0_EBX_CLFLUSHOPT | \ CPUID_7_0_EBX_CLWB | CPUID_7_0_EBX_MPX | CPUID_7_0_EBX_FSGSBASE | \
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, · line 666
Quoted 10 lines
target/i386/cpu.c +2
-3- CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2) + CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2 | CPUID_7_0_EBX_RDSEED)/* missing: CPUID_7_0_EBX_HLE -CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM, -CPUID_7_0_EBX_RDSEED */ +CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM */ #define TCG_7_0_ECX_FEATURES (CPUID_7_0_ECX_UMIP | CPUID_7_0_ECX_PKU | \ /* CPUID_7_0_ECX_OSPKE is dynamic */ \CPUID_7_0_ECX_LA57 | CPUID_7_0_ECX_PKS | CPUID_7_0_ECX_VAES)
Still missing the check for CPUID_7_0_EBX_RDSEED at the RDSEED insn.
r~
Re: [PATCH v2 09/10] target/i386: implement 32-bit SYSENTER for linux-user
In reply to Richard HendersonQuoted 10 lines · Richard Henderson
Quoted 3 lines · Paolo Bonzini
TCG reports the SEP feature (SYSENTER/SYSEXIT) in user mode emulation, but does not plumb it into the linux-user run loop. Split the helper into system emulation and user-mode emulation cases and implement the latter.
I'm not keen on this.
This belongs with the rest of the vdso (see patches posted years ago; committing binary blobs rejected, still waiting on a decent way to invoke cross-compilers to build them).
As we discussed in Dublin, that should be doable by reusing the tests/tcg logic in configure (though we would likely commit the binary blobs as well). You could do it in your sleep. ;)
Quoted 2 lines
Further, this shouldn't ever be reachable, because
AT_SYSINFOwon't be present to give the guest libc the location of the vdso routine to call.
Even without AT_SYSINFO the program should be able to do SYSENTER and 'trust" the kernel not to change the epilog of the routine.
To be honest I don't like it particularly either; but I also didn't like that SEP is reported but doesn't work (and the purpose of these patches is to allow using named CPU models in linux-user)... I can certainly drop the patch since it's been like this for ages.
Paolo
Re: [PATCH v2 03/10] target/i386: TCG supports RDSEED
In reply to Richard HendersonIl mar 20 giu 2023, 18:24 Richard Henderson <[email protected]> ha scritto:
Quoted 31 lines · Paolo Bonzini
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t
Quoted 14 lines
TCG implements RDSEED, and in fact uses
qcrypto_random_byteswhich is secure enough to match hardware behavior. Expose it to guests.Reviewed-by Richard Henderson <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- target/i386/cpu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)target/i386/cpu.c +0
-0@@ -657,11 +657,10 @@ void x86_cpu_vendor_words2str(char *dst, uint32_tvendor1,
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t · line 657
Quoted 1 line
target/i386/cpu.c +0
-0CPUID_7_0_EBX_BMI1 |CPUID_7_0_EBX_BMI2 |CPUID_7_0_EBX_ADX | \
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t · line 658
Quoted 1 line
target/i386/cpu.c +0
-0CPUID_7_0_EBX_PCOMMIT | CPUID_7_0_EBX_CLFLUSHOPT |\
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t · line 659
Quoted 1 line
target/i386/cpu.c +0
-0CPUID_7_0_EBX_CLWB | CPUID_7_0_EBX_MPX |
CPUID_7_0_EBX_FSGSBASE| \target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t · line 660
Quoted 2 lines
target/i386/cpu.c +1
-1- CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2) + CPUID_7_0_EBX_ERMS | CPUID_7_0_EBX_AVX2 |
CPUID_7_0_EBX_RDSEED)target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
Quoted 6 lines
target/i386/cpu.c +1
-2/* missing: CPUID_7_0_EBX_HLE -CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM, -CPUID_7_0_EBX_RDSEED */ +CPUID_7_0_EBX_INVPCID, CPUID_7_0_EBX_RTM */ #define TCG_7_0_ECX_FEATURES (CPUID_7_0_ECX_UMIP | CPUID_7_0_ECX_PKU |
Quoted 6 lines
\
target/i386/cpu.c void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1, · line 666
Quoted 2 lines
target/i386/cpu.c +0
-0/* CPUID_7_0_ECX_OSPKE is dynamic */ \ CPUID_7_0_ECX_LA57 | CPUID_7_0_ECX_PKS | CPUID_7_0_ECX_VAES)Still missing the check for
CPUID_7_0_EBX_RDSEEDat the RDSEED insn.
Sorry, I 6kissed that remain. It's more of a separate patch IMO, I will add it.
Paolo
Quoted 3 lines
r~