target-i386: fix CPL computation
cover target-i386: fix CPL computation
CS.RPL is not equal to the CPL in the few instructions between setting CR0.PE and reloading CS. We get this right in the common case, because writes to CR0 do not modify the CPL, but it would not be enough if an SMI comes exactly during that brief period.
So this patch fixes the CPL to come from SS.DPL instead, which requires a couple changes to make access rights correct for VM86 mode, and to override the CPL to CS.RPL during task switches (more details in patch 2).
Tested with FreeDOS and kvm-unit-tests.
Paolo Bonzini (3):
target-i386: fix segment flags for SMM and VM86 mode
target-i386: prepare CPL checks for next patch
target-i386: get CPL from SS.DPL
target-i386/cpu.h | 8 +++----- target-i386/kvm.c | 2 +- target-i386/machine.c | 8 ++++++++ target-i386/seg_helper.c | 27 ++++++++++++--------------- target-i386/smm_helper.c | 24 ++++++++++++++++++------ 5 files changed, 42 insertions(+), 27 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/3 target-i386: fix segment flags for SMM and VM86 mode
With the next patch, these need to be correct or VM86 tasks have the wrong CPL. The flags are basically what the Intel VMX documentation say is mandatory for entry into a VM86 guest.
For consistency, SMM ought to have the same flags except with CPL=0.
--- target-i386/seg_helper.c | 6 ++++-- target-i386/smm_helper.c | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-)
target-i386/seg_helper.c +4 -2
@@ -88,8 +88,10 @@ static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1,static inline void load_seg_vm (CPUX86State *env,int seg,int selector) { selector &=0xffff ; -cpu_x86_load_seg_cache (env, seg, selector, - (selector <<4 ),0xffff ,0 ); + +cpu_x86_load_seg_cache (env, seg, selector, (selector <<4 ),0xffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK | (3 <<DESC_DPL_SHIFT )); } static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
target-i386/smm_helper.c +18 -6
@@ -170,12 +170,24 @@ void do_smm_enter(X86CPU *cpu) env->dr[7 ] =0x00000400 ;cpu_x86_load_seg_cache (env,R_CS , (env->smbase >>4 ) &0xffff , env->smbase, -0xffffffff ,0 ); -cpu_x86_load_seg_cache (env,R_DS ,0 ,0 ,0xffffffff ,0 ); -cpu_x86_load_seg_cache (env,R_ES ,0 ,0 ,0xffffffff ,0 ); -cpu_x86_load_seg_cache (env,R_SS ,0 ,0 ,0xffffffff ,0 ); -cpu_x86_load_seg_cache (env,R_FS ,0 ,0 ,0xffffffff ,0 ); -cpu_x86_load_seg_cache (env,R_GS ,0 ,0 ,0xffffffff ,0 ); +0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); +cpu_x86_load_seg_cache (env,R_DS ,0 ,0 ,0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); +cpu_x86_load_seg_cache (env,R_ES ,0 ,0 ,0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); +cpu_x86_load_seg_cache (env,R_SS ,0 ,0 ,0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); +cpu_x86_load_seg_cache (env,R_FS ,0 ,0 ,0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); +cpu_x86_load_seg_cache (env,R_GS ,0 ,0 ,0xffffffff , +DESC_P_MASK |DESC_S_MASK |DESC_W_MASK | +DESC_A_MASK ); }void helper_rsm (CPUX86State *env)
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 1/3] target-i386: fix segment flags for SMM and VM86 mode
Quoted 6 lines · Paolo Bonzini
With the next patch, these need to be correct or VM86 tasks have the wrong CPL. The flags are basically what the Intel VMX documentation say is mandatory for entry into a VM86 guest.
For consistency, SMM ought to have the same flags except with CPL=0.
Does cpu_x86_load_seg() and x86_cpu_gdb_load_seg() then also have to change?
-Kevin
Re: [Qemu-devel] [PATCH 1/3] target-i386: fix segment flags for SMM and VM86 mode
Il 15/05/2014 20:45, Kevin O'Connor ha scritto:
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines
With the next patch, these need to be correct or VM86 tasks have the wrong CPL. The flags are basically what the Intel VMX documentation say is mandatory for entry into a VM86 guest.
For consistency, SMM ought to have the same flags except with CPL=0.
Does
cpu_x86_load_seg()andx86_cpu_gdb_load_seg()then also have to change?
The former is just for user-mode emulation, but it probably doesn't hurt to fix it (I just found out about tests/tcg/runcom.c, which can be used to test this code path). For the latter you're right, thanks!
Paolo
PATCH 2/3 target-i386: prepare CPL checks for next patch
In reply to Paolo BonziniDuring task switch, all of CS.DPL, CS.RPL, SS.DPL must match (in addition to all the other requirements) and will be the new CPL. So far this worked by carefully setting the CS selector and flags before doing the task switch; but this will not work once we get the CPL from SS.DPL.
Temporarily assume that the CPL comes from CS.RPL during task switch to a protected-mode task, until the descriptor of SS is loaded.
Also cleanup the comments in tss_laod_seg. The dpl == rpl test is present in the SDM, and subsumes the dpl <= rpl test just below.
--- target-i386/seg_helper.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
target-i386/seg_helper.c +8 -13
@@ -135,8 +135,7 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr, } } -/* XXX: merge with load_seg() */ -static void tss_load_seg (CPUX86State *env,int seg_reg,int selector) +static void tss_load_seg (CPUX86State *env,int seg_reg,int selector,int cpl) {uint32_t e1, e2;int rpl, dpl, cpl;
@@ -150,18 +149,13 @@ static void tss_load_seg(CPUX86State *env, int seg_reg, int selector) } rpl = selector &3 ; dpl = (e2 >>DESC_DPL_SHIFT ) &3 ; - cpl = env->hflags &HF_CPL_MASK ;if (seg_reg ==R_CS ) {if (!(e2 &DESC_CS_MASK )) {raise_exception_err (env,EXCP0A_TSS , selector &0xfffc ); } -/* XXX: is it correct? */ if (dpl != rpl) {raise_exception_err (env,EXCP0A_TSS , selector &0xfffc ); } -if ((e2 &DESC_C_MASK ) && dpl > rpl) { -raise_exception_err (env,EXCP0A_TSS , selector &0xfffc ); - } }else if (seg_reg ==R_SS ) {/* SS must be writable data */ if ((e2 &DESC_CS_MASK ) || !(e2 &DESC_W_MASK )) {
@@ -448,12 +442,13 @@ static void switch_tss(CPUX86State *env, int tss_selector,/* load the segments */ if (!(new_eflags &VM_MASK )) { -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 ]); +int cpl = new_segs[R_CS ] &3 ; +tss_load_seg (env,R_CS , new_segs[R_CS ], cpl); +tss_load_seg (env,R_SS , new_segs[R_SS ], cpl); +tss_load_seg (env,R_ES , new_segs[R_ES ], cpl); +tss_load_seg (env,R_DS , new_segs[R_DS ], cpl); +tss_load_seg (env,R_FS , new_segs[R_FS ], cpl); +tss_load_seg (env,R_GS , new_segs[R_GS ], cpl); }/* check that env->eip is in the CS segment limits */
Signature · 1 line
1.8.3.1
PATCH 3/3 target-i386: get CPL from SS.DPL
In reply to Paolo BonziniCS.RPL is not equal to the CPL in the few instructions between setting CR0.PE and reloading CS. We get this right in the common case, because writes to CR0 do not modify the CPL, but it would not be enough if an SMI comes exactly during that brief period. Were this to happen, the RSM instruction would erroneously set CPL to the low two bits of the real-mode selector; and if they are not 00, the next instruction fetch cannot access the code segment and causes a triple fault.
However, SS.DPL *is* always equal to the CPL (except during task switches as noted in the previous patch). In real processors (AMD only) there is a weird case of SYSRET setting SS.DPL=SS.RPL from the STAR register while forcing CPL=3, but we do not emulate that.
--- target-i386/cpu.h | 8 +++----- target-i386/kvm.c | 2 +- target-i386/machine.c | 8 ++++++++ 3 files changed, 12 insertions(+), 6 deletions(-)
target-i386/cpu.h +3 -5
@@ -986,7 +986,6 @@ static inline void cpu_x86_load_seg_cache(CPUX86State *env,/* update the hidden flags */ {if (seg_reg ==R_CS ) { -int cpl = selector &3 ;#ifdef TARGET_X86_64 if ((env->hflags &HF_LMA_MASK ) && (flags &DESC_L_MASK )) {/* long mode */
@@ -996,15 +995,14 @@ static inline void cpu_x86_load_seg_cache(CPUX86State *env,#endif {/* legacy / compatibility case */ -if (!(env->cr[0 ] &CR0_PE_MASK )) - cpl =0 ; -else if (env->eflags &VM_MASK ) - cpl =3 ; new_hflags = (env->segs[R_CS ].flags &DESC_B_MASK ) >> (DESC_B_SHIFT -HF_CS32_SHIFT ); env->hflags = (env->hflags & ~(HF_CS32_MASK |HF_CS64_MASK )) | new_hflags; } + } +if (seg_reg ==R_SS ) { +int cpl = (flags >>DESC_DPL_SHIFT ) &3 ;#if HF_CPL_MASK !=3 #error HF_CPL_MASK is hardcoded#endif
target-i386/kvm.c +1 -1
@@ -1430,7 +1430,7 @@ static int kvm_get_sregs(X86CPU *cpu) HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \ HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK) - hflags = (env->segs[R_CS ].flags >>DESC_DPL_SHIFT ) &HF_CPL_MASK ; + hflags = (env->segs[R_SS ].flags >>DESC_DPL_SHIFT ) &HF_CPL_MASK ; hflags |= (env->cr[0 ] &CR0_PE_MASK ) << (HF_PE_SHIFT -CR0_PE_SHIFT ); hflags |= (env->cr[0 ] << (HF_MP_SHIFT -CR0_MP_SHIFT )) & (HF_MP_MASK |HF_EM_MASK |HF_TS_MASK );
target-i386/machine.c +8 -0
@@ -312,6 +312,14 @@ static int cpu_post_load(void *opaque, int version_id) env->segs[R_SS ].flags &= ~(env->segs[R_SS ].flags &DESC_DPL_MASK ); } +/* Older versions of QEMU incorrectly used CS.DPL as the CPL when +* running under KVM. This is wrong for conforming code segments. +* Luckily, in our implementation the CPL field of hflags is redundant +* and we can get the right value from the SS descriptor privilege level. +*/ + env->hflags &= ~HF_CPL_MASK ; + env->hflags |= (env->segs[R_SS ].flags >>DESC_DPL_SHIFT ) &HF_CPL_MASK ; +/* XXX: restore FPU round state */ env->fpstt = (env->fpus_vmstate >>11 ) &7 ; env->fpus = env->fpus_vmstate & ~0x3800 ;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 3/3] target-i386: get CPL from SS.DPL
Quoted 9 lines · Paolo Bonzini
CS.RPL is not equal to the CPL in the few instructions between setting CR0.PE and reloading CS. We get this right in the common case, because writes to CR0 do not modify the CPL, but it would not be enough if an SMI comes exactly during that brief period. Were this to happen, the RSM instruction would erroneously set CPL to the low two bits of the real-mode selector; and if they are not 00, the next instruction fetch cannot access the code segment and causes a triple fault.
Quoted 4 lines
However, SS.DPL *is* always equal to the CPL (except during task switches as noted in the previous patch). In real processors (AMD only) there is a weird case of SYSRET setting SS.DPL=SS.RPL from the STAR register while forcing CPL=3, but we do not emulate that.
It looks to me like there could be a couple of places in the code where cpu_x86_load_seg_cache(R_CS) is called, but cpu_x86_load_seg_cache(R_SS) may not be. In particular, helper_ret_protected() and cpu_x86_load_seg_cache_sipi(). Are these still okay?
-Kevin
Re: [Qemu-devel] [PATCH 3/3] target-i386: get CPL from SS.DPL
Il 15/05/2014 20:38, Kevin O'Connor ha scritto:
Quoted 15 lines · Paolo Bonzini
Quoted 9 lines
CS.RPL is not equal to the CPL in the few instructions between setting CR0.PE and reloading CS. We get this right in the common case, because writes to CR0 do not modify the CPL, but it would not be enough if an SMI comes exactly during that brief period. Were this to happen, the RSM instruction would erroneously set CPL to the low two bits of the real-mode selector; and if they are not 00, the next instruction fetch cannot access the code segment and causes a triple fault.
Quoted 4 lines
However, SS.DPL *is* always equal to the CPL (except during task switches as noted in the previous patch). In real processors (AMD only) there is a weird case of SYSRET setting SS.DPL=SS.RPL from the STAR register while forcing CPL=3, but we do not emulate that.
Quoted 5 lines
It looks to me like there could be a couple of places in the code where
cpu_x86_load_seg_cache(R_CS)is called, butcpu_x86_load_seg_cache(R_SS)may not be. In particular,helper_ret_protected()andcpu_x86_load_seg_cache_sipi(). Are these still okay?
Yes, helper_ret_protected() skips the SS load only if rpl == cpl (so if CS.RPL == SS.DPL, and the invariant is respected). cpu_x86_load_seg_cache_sipi() runs in real mode only.
Paolo