Skip to contentSkip to conversation

target-i386: fix iret emulation correctness

PATCH Paolo Bonzini 2 participants 3 messages Jun 7, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

target-i386/translate.c static target_ulong disas_insn(CPUX86State *env, · line 6324

Quoted 11 lines · poletaev
target-i386/translate.c +1 -0
index f010022..c409baf 100644
@@ -6319,6 +6319,7 @@ static target_ulong disas_insn(CPUX86State *env,
 DisasContext *s,
             set_cc_op(s, CC_OP_EFLAGS);
         } else if (s->vm86) {
             if (s->iopl != 3) {
+                gen_helper_reset_nmi_blocking(cpu_env);
                 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);

I am afraid that the solution is more complicated. The exception will be handled before the NMI, while the opposite should be done according to the manual.

So, first, you need to move HF2_NMI_MASK from hflags2 to hflags, so that different NMI masking states cause the guest code to be retranslated. Second, an IRET with HF_NMI_MASK set can be translated to _only_ the reset of NMI mask followed by end of basic block. An IRET without HF_NMI_MASK instead can be translated the same way as now.

Paolo

Quoted 5 lines
target-i386/translate.c +0 -0
             } else {
                 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
#2 lore raw
Quoted 3 lines

Second, an IRET with HF_NMI_MASK set can be translated to _only_ the reset of NMI mask followed by end of basic block. An IRET without HF_NMI_MASK instead can be translated the same way as now.

I want to make it like this, but it seems to me it can lead to zero tb size and introduce bad side effect. What I do wrong?

case 0xcf: /* iret */

    gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
    if (!s->pe) {
        /* real mode */
        gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
        set_cc_op(s, CC_OP_EFLAGS);
    } else if (s->vm86) {
        if (s->iopl != 3) {
            if (s->flags & HF_NMI_MASK) {
                gen_reset_hflag(s, HF_NMI_MASK);
                s->pc = pc_start;
                gen_jmp_im(pc_start - s->cs_base);
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
            set_cc_op(s, CC_OP_EFLAGS);
        }
    } else {
        gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
                                  tcg_const_i32(s->pc - s->cs_base));
        set_cc_op(s, CC_OP_EFLAGS);
    }
    gen_eob(s);
    break;

Best regards,
Dmitry Poletaev.

#3 lore raw
Quoted 25 lines · poletaev
Quoted 3 lines

Second, an IRET with HF_NMI_MASK set can be translated to _only_ the reset of NMI mask followed by end of basic block. An IRET without HF_NMI_MASK instead can be translated the same way as now.

I want to make it like this, but it seems to me it can lead to zero tb size and introduce bad side effect. What I do wrong?

case 0xcf: /* iret */

    gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
    if (!s->pe) {
        /* real mode */
        gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
        set_cc_op(s, CC_OP_EFLAGS);
    } else if (s->vm86) {
        if (s->iopl != 3) {
            if (s->flags & HF_NMI_MASK) {
                gen_reset_hflag(s, HF_NMI_MASK);
                s->pc = pc_start;
                gen_jmp_im(pc_start - s->cs_base);
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
            set_cc_op(s, CC_OP_EFLAGS);
Quoted 8 lines
        }
    } else {
        gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
                                  tcg_const_i32(s->pc - s->cs_base));
        set_cc_op(s, CC_OP_EFLAGS);
    }
    gen_eob(s);
    break;

I think it's okay to make it count as a one instruction tb, just like a tb with a "jmp ." instruction.

Thanks,

Paolo