From patchwork Wed Sep 13 09:05:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?UTF-8?q?Sergio=20Andr=C3=A9s=20G=C3=B3mez=20Del=20Real?= X-Patchwork-Id: 9950773 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CC15D6038F for ; Wed, 13 Sep 2017 09:15:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD51828FCD for ; Wed, 13 Sep 2017 09:15:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B213E290BC; Wed, 13 Sep 2017 09:15:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2A76928FCD for ; Wed, 13 Sep 2017 09:15:15 +0000 (UTC) Received: from localhost ([::1]:41091 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds3lK-0002Ek-CN for patchwork-qemu-devel@patchwork.kernel.org; Wed, 13 Sep 2017 05:15:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds3cT-0001d3-Ju for qemu-devel@nongnu.org; Wed, 13 Sep 2017 05:06:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ds3cR-0007bC-Lm for qemu-devel@nongnu.org; Wed, 13 Sep 2017 05:06:05 -0400 Received: from mail-vk0-x241.google.com ([2607:f8b0:400c:c05::241]:36913) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ds3cR-0007ap-FC for qemu-devel@nongnu.org; Wed, 13 Sep 2017 05:06:03 -0400 Received: by mail-vk0-x241.google.com with SMTP id 184so3319697vkn.4 for ; Wed, 13 Sep 2017 02:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dy8iUm4Kdq1vX0qqsKIWuEbh1Ht8cSwurLPNcBI2nK4=; b=dFNvsvzeBqL7/SkpwLttE1SA9T5o+SNoeHQTtpcxQaV36Wd1hl+4Ch0HMPzlhJ4k/S Lu3rT2X3RDIrpsRLu3O2masHLmnd4ZPXkPEzkXCF2hCRogkR+VbqFy5uvRFIQaqUi6LG MWLQdvJTosHVO7EdxNlJQ+N35+1csYAZwKtqgU+PsNUbYTJ3L53L9rGwGc75iGfBTA9E Cuvo0juOyRDO6CcPOzfP6qRBs3bJJ19Yluq9iUSZkwy7kRDZxJtu0e00QNNq6wmILQs4 QIktR8z5ZKXUsgn3WzkM5ztGUZ2BeHlb2lcrRYrqaLwGBItaDlo3nNAR1TYPjgmd2rnw sfWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dy8iUm4Kdq1vX0qqsKIWuEbh1Ht8cSwurLPNcBI2nK4=; b=tUsgPkiu25waOQQ9F9pWxMLJoJ8Z923ORiO4Xjb7187i8ewsRwTQGBmPDGt7vDsH6/ U6Wi2ORyL4Lj4ugIWCeLQsPb+spnYn+mU5cJ56Dp8X0oGPN5Bb9K1VHRdpyYzMWbK6Wx OHdzCXSamBms9BZGnDV4z9+6aJCs/Su7/ChuONsLxoDGtJI+frjipWEOzfyCvAFKuPMk fnAi7pzPc3Z+zbpeg8er39sBPXj3uAG/LnWk81JEYvuAYi8gU7STsctncT2tdZvCVovj gtskmQnkmlWnxGyNzoncfRtAVDac8Lo1YXxzXzUup7hF8fRwkmqxkc+LfZ21I/6ihqAw Tg+A== X-Gm-Message-State: AHPjjUiypGXCyb3uRMqrPaVHAUrbJNBWzOvYtC8T5XvTcLlV/uaqJK/5 5/ueauJaBGGPTmEE X-Google-Smtp-Source: AOwi7QBw360FH9K1yoDjFQ0sDgXP/BDc22auuuLfsb4N+K23Cx4Z8FGZmvKvSZ7neLzYyr/Y7espVg== X-Received: by 10.31.148.66 with SMTP id w63mr10889570vkd.51.1505293562737; Wed, 13 Sep 2017 02:06:02 -0700 (PDT) Received: from localhost.localdomain ([190.66.154.128]) by smtp.gmail.com with ESMTPSA id r74sm2677601vkf.19.2017.09.13.02.06.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Sep 2017 02:06:02 -0700 (PDT) From: Sergio Andres Gomez Del Real X-Google-Original-From: Sergio Andres Gomez Del Real To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 04:05:21 -0500 Message-Id: <20170913090522.4022-14-Sergio.G.DelReal@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170913090522.4022-1-Sergio.G.DelReal@gmail.com> References: <20170913090522.4022-1-Sergio.G.DelReal@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400c:c05::241 Subject: [Qemu-devel] [PATCH v4 13/14] hvf: refactor event injection code for hvf X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Sergio Andres Gomez Del Real , pbonzini@redhat.com, stefanha@gmail.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch refactors the event-injection code for hvf by using the appropriate fields already provided by CPUX86State. At vmexit, it fills these fields so that hvf_inject_interrupts can just retrieve them without calling into hvf. Signed-off-by: Sergio Andres Gomez Del Real --- target/i386/cpu.c | 3 ++ target/i386/cpu.h | 1 + target/i386/hvf-all.c | 57 ++++++++++++++++++++++++++++++++---- target/i386/hvf-utils/vmcs.h | 3 ++ target/i386/hvf-utils/vmx.h | 8 ++++++ target/i386/hvf-utils/x86hvf.c | 65 ++++++++++++++++++++---------------------- target/i386/kvm.c | 2 -- 7 files changed, 98 insertions(+), 41 deletions(-) diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 2bf8c0ce0f..76bfcc23f3 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -3247,6 +3247,9 @@ static void x86_cpu_reset(CPUState *s) memset(env->mtrr_var, 0, sizeof(env->mtrr_var)); memset(env->mtrr_fixed, 0, sizeof(env->mtrr_fixed)); + env->interrupt_injected = -1; + env->exception_injected = -1; + env->nmi_injected = false; #if !defined(CONFIG_USER_ONLY) /* We hard-wire the BSP to the first CPU. */ apic_designate_bsp(cpu->apic_state, s->cpu_index == 0); diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 630a4c2c0a..81e3f511a6 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -1188,6 +1188,7 @@ typedef struct CPUX86State { int32_t interrupt_injected; uint8_t soft_interrupt; uint8_t has_error_code; + uint32_t ins_len; uint32_t sipi_vector; bool tsc_valid; int64_t tsc_khz; diff --git a/target/i386/hvf-all.c b/target/i386/hvf-all.c index e8466e6f47..105a63ad4f 100644 --- a/target/i386/hvf-all.c +++ b/target/i386/hvf-all.c @@ -589,6 +589,55 @@ void hvf_disable(int shouldDisable) hvf_disabled = shouldDisable; } +static void hvf_store_events(CPUState *cpu, uint32_t ins_len, uint64_t idtvec_info) +{ + X86CPU *x86_cpu = X86_CPU(cpu); + CPUX86State *env = &x86_cpu->env; + + env->exception_injected = -1; + env->interrupt_injected = -1; + env->nmi_injected = false; + if (idtvec_info & VMCS_IDT_VEC_VALID) { + switch (idtvec_info & VMCS_IDT_VEC_TYPE) { + case VMCS_IDT_VEC_HWINTR: + case VMCS_IDT_VEC_SWINTR: + env->interrupt_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; + break; + case VMCS_IDT_VEC_NMI: + env->nmi_injected = true; + break; + case VMCS_IDT_VEC_HWEXCEPTION: + case VMCS_IDT_VEC_SWEXCEPTION: + env->exception_injected = idtvec_info & VMCS_IDT_VEC_VECNUM; + break; + case VMCS_IDT_VEC_PRIV_SWEXCEPTION: + default: + abort(); + } + if ((idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWEXCEPTION || + (idtvec_info & VMCS_IDT_VEC_TYPE) == VMCS_IDT_VEC_SWINTR) { + env->ins_len = ins_len; + } + if (idtvec_info & VMCS_INTR_DEL_ERRCODE) { + env->has_error_code = true; + env->error_code = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_ERROR); + } + } + if ((rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & + VMCS_INTERRUPTIBILITY_NMI_BLOCKING)) { + env->hflags2 |= HF2_NMI_MASK; + } else { + env->hflags2 &= ~HF2_NMI_MASK; + } + if (rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & + (VMCS_INTERRUPTIBILITY_STI_BLOCKING | + VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) { + env->hflags |= HF_INHIBIT_IRQ_MASK; + } else { + env->hflags &= ~HF_INHIBIT_IRQ_MASK; + } +} + int hvf_vcpu_exec(CPUState *cpu) { X86CPU *x86_cpu = X86_CPU(cpu); @@ -608,11 +657,6 @@ int hvf_vcpu_exec(CPUState *cpu) cpu->vcpu_dirty = false; } - env->hvf_emul->interruptable = - !(rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & - (VMCS_INTERRUPTIBILITY_STI_BLOCKING | - VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)); - hvf_inject_interrupts(cpu); vmx_update_tpr(cpu); @@ -631,7 +675,10 @@ int hvf_vcpu_exec(CPUState *cpu) uint64_t exit_qual = rvmcs(cpu->hvf_fd, VMCS_EXIT_QUALIFICATION); uint32_t ins_len = (uint32_t)rvmcs(cpu->hvf_fd, VMCS_EXIT_INSTRUCTION_LENGTH); + uint64_t idtvec_info = rvmcs(cpu->hvf_fd, VMCS_IDT_VECTORING_INFO); + + hvf_store_events(cpu, ins_len, idtvec_info); rip = rreg(cpu->hvf_fd, HV_X86_RIP); RFLAGS(env) = rreg(cpu->hvf_fd, HV_X86_RFLAGS); env->eflags = RFLAGS(env); diff --git a/target/i386/hvf-utils/vmcs.h b/target/i386/hvf-utils/vmcs.h index c410dcfaaa..0fae73dce5 100644 --- a/target/i386/hvf-utils/vmcs.h +++ b/target/i386/hvf-utils/vmcs.h @@ -299,6 +299,7 @@ /* * VMCS IDT-Vectoring information fields */ +#define VMCS_IDT_VEC_VECNUM 0xFF #define VMCS_IDT_VEC_VALID (1U << 31) #define VMCS_IDT_VEC_TYPE 0x700 #define VMCS_IDT_VEC_ERRCODE_VALID (1U << 11) @@ -306,6 +307,8 @@ #define VMCS_IDT_VEC_NMI (2 << 8) #define VMCS_IDT_VEC_HWEXCEPTION (3 << 8) #define VMCS_IDT_VEC_SWINTR (4 << 8) +#define VMCS_IDT_VEC_PRIV_SWEXCEPTION (5 << 8) +#define VMCS_IDT_VEC_SWEXCEPTION (6 << 8) /* * VMCS Guest interruptibility field diff --git a/target/i386/hvf-utils/vmx.h b/target/i386/hvf-utils/vmx.h index 44a5c6d554..102075d0d4 100644 --- a/target/i386/hvf-utils/vmx.h +++ b/target/i386/hvf-utils/vmx.h @@ -181,6 +181,10 @@ static inline void macvm_set_rip(CPUState *cpu, uint64_t rip) static inline void vmx_clear_nmi_blocking(CPUState *cpu) { + X86CPU *x86_cpu = X86_CPU(cpu); + CPUX86State *env = &x86_cpu->env; + + env->hflags2 &= ~HF2_NMI_MASK; uint32_t gi = (uint32_t) rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY); gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING; wvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY, gi); @@ -188,6 +192,10 @@ static inline void vmx_clear_nmi_blocking(CPUState *cpu) static inline void vmx_set_nmi_blocking(CPUState *cpu) { + X86CPU *x86_cpu = X86_CPU(cpu); + CPUX86State *env = &x86_cpu->env; + + env->hflags2 |= HF2_NMI_MASK; uint32_t gi = (uint32_t)rvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY); gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING; wvmcs(cpu->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY, gi); diff --git a/target/i386/hvf-utils/x86hvf.c b/target/i386/hvf-utils/x86hvf.c index dd0710d056..83eb3be065 100644 --- a/target/i386/hvf-utils/x86hvf.c +++ b/target/i386/hvf-utils/x86hvf.c @@ -356,50 +356,47 @@ void vmx_clear_int_window_exiting(CPUState *cpu) void hvf_inject_interrupts(CPUState *cpu_state) { - int allow_nmi = !(rvmcs(cpu_state->hvf_fd, VMCS_GUEST_INTERRUPTIBILITY) & - VMCS_INTERRUPTIBILITY_NMI_BLOCKING); X86CPU *x86cpu = X86_CPU(cpu_state); CPUX86State *env = &x86cpu->env; - uint64_t idt_info = rvmcs(cpu_state->hvf_fd, VMCS_IDT_VECTORING_INFO); + uint8_t vector; + uint64_t intr_type; + bool have_event = true; + if (env->interrupt_injected != -1) { + vector = env->interrupt_injected; + intr_type = VMCS_INTR_T_SWINTR; + } else if (env->exception_injected != -1) { + vector = env->exception_injected; + if (vector == EXCP03_INT3 || vector == EXCP04_INTO) { + intr_type = VMCS_INTR_T_SWEXCEPTION; + } else { + intr_type = VMCS_INTR_T_HWEXCEPTION; + } + } else if (env->nmi_injected) { + vector = NMI_VEC; + intr_type = VMCS_INTR_T_NMI; + } else { + have_event = false; + } + uint64_t info = 0; - - if (idt_info & VMCS_IDT_VEC_VALID) { - uint8_t vector = idt_info & 0xff; - uint64_t intr_type = idt_info & VMCS_INTR_T_MASK; - info = idt_info; - + if (have_event) { + info = vector | intr_type | VMCS_INTR_VALID; uint64_t reason = rvmcs(cpu_state->hvf_fd, VMCS_EXIT_REASON); - if (intr_type == VMCS_INTR_T_NMI && reason != EXIT_REASON_TASK_SWITCH) { - allow_nmi = 1; + if (env->nmi_injected && reason != EXIT_REASON_TASK_SWITCH) { vmx_clear_nmi_blocking(cpu_state); } - - if ((allow_nmi || intr_type != VMCS_INTR_T_NMI)) { + + if (!(env->hflags2 & HF2_NMI_MASK) || intr_type != VMCS_INTR_T_NMI) { info &= ~(1 << 12); /* clear undefined bit */ if (intr_type == VMCS_INTR_T_SWINTR || - intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION || intr_type == VMCS_INTR_T_SWEXCEPTION) { - uint64_t ins_len = rvmcs(cpu_state->hvf_fd, - VMCS_EXIT_INSTRUCTION_LENGTH); - wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_INST_LENGTH, ins_len); - } - if (vector == EXCEPTION_BP || vector == EXCEPTION_OF) { - /* - * VT-x requires #BP and #OF to be injected as software - * exceptions. - */ - info &= ~VMCS_INTR_T_MASK; - info |= VMCS_INTR_T_SWEXCEPTION; - uint64_t ins_len = rvmcs(cpu_state->hvf_fd, - VMCS_EXIT_INSTRUCTION_LENGTH); - wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_INST_LENGTH, ins_len); + wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_INST_LENGTH, env->ins_len); } - uint64_t err = 0; - if (idt_info & VMCS_INTR_DEL_ERRCODE) { - err = rvmcs(cpu_state->hvf_fd, VMCS_IDT_VECTORING_ERROR); - wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_EXCEPTION_ERROR, err); + if (env->has_error_code) { + wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_EXCEPTION_ERROR, + env->error_code); } /*printf("reinject %lx err %d\n", info, err);*/ wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_INTR_INFO, info); @@ -407,7 +404,7 @@ void hvf_inject_interrupts(CPUState *cpu_state) } if (cpu_state->interrupt_request & CPU_INTERRUPT_NMI) { - if (allow_nmi && !(info & VMCS_INTR_VALID)) { + if (!(env->hflags2 & HF2_NMI_MASK) && !(info & VMCS_INTR_VALID)) { cpu_state->interrupt_request &= ~CPU_INTERRUPT_NMI; info = VMCS_INTR_VALID | VMCS_INTR_T_NMI | NMI_VEC; wvmcs(cpu_state->hvf_fd, VMCS_ENTRY_INTR_INFO, info); @@ -416,7 +413,7 @@ void hvf_inject_interrupts(CPUState *cpu_state) } } - if (env->hvf_emul->interruptable && + if (!(env->hflags & HF_INHIBIT_IRQ_MASK) && (cpu_state->interrupt_request & CPU_INTERRUPT_HARD) && (EFLAGS(env) & IF_MASK) && !(info & VMCS_INTR_VALID)) { int line = cpu_get_pic_interrupt(&x86cpu->env); diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 6db7783edc..a695b8cd4e 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -1030,8 +1030,6 @@ void kvm_arch_reset_vcpu(X86CPU *cpu) { CPUX86State *env = &cpu->env; - env->exception_injected = -1; - env->interrupt_injected = -1; env->xcr0 = 1; if (kvm_irqchip_in_kernel()) { env->mp_state = cpu_is_bsp(cpu) ? KVM_MP_STATE_RUNNABLE :