From patchwork Fri Jan 31 16:46:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11360285 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7FE9613A4 for ; Fri, 31 Jan 2020 16:46:50 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 664EB20663 for ; Fri, 31 Jan 2020 16:46:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 664EB20663 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ixZQj-0000Ng-Pn; Fri, 31 Jan 2020 16:46:05 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ixZQi-0000NV-SR for xen-devel@lists.xenproject.org; Fri, 31 Jan 2020 16:46:04 +0000 X-Inumbo-ID: 2b50b370-4449-11ea-8396-bc764e2007e4 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2b50b370-4449-11ea-8396-bc764e2007e4; Fri, 31 Jan 2020 16:46:04 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 44324AC8F; Fri, 31 Jan 2020 16:46:03 +0000 (UTC) From: Jan Beulich To: "xen-devel@lists.xenproject.org" References: Message-ID: Date: Fri, 31 Jan 2020 17:46:06 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Subject: [Xen-devel] [PATCH v4 7/7] x86/HVM: reduce scope of pfec in hvm_emulate_init_per_insn() X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: George Dunlap , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , Paul Durrant Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" It needs calculating only in one out of three cases. Re-structure the code a little such that the variable truly gets calculated only when we don't get any insn bytes from elsewhere, and hence need to (try to) fetch them. Also OR in PFEC_insn_fetch right in the initializer. While in this mood, restrict addr's scope as well. Signed-off-by: Jan Beulich Acked-by: Andrew Cooper Reviewed-by: Paul Durrant --- v4: New. --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -2762,8 +2762,6 @@ void hvm_emulate_init_per_insn( unsigned int insn_bytes) { struct vcpu *curr = current; - unsigned int pfec = PFEC_page_present; - unsigned long addr; hvmemul_ctxt->ctxt.lma = hvm_long_mode_active(curr); @@ -2778,14 +2776,23 @@ void hvm_emulate_init_per_insn( hvmemul_ctxt->seg_reg[x86_seg_ss].db ? 32 : 16; } - if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 ) - pfec |= PFEC_user_mode; - hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->rip; - if ( !insn_bytes ) + + if ( insn_bytes ) { + hvmemul_ctxt->insn_buf_bytes = insn_bytes; + memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes); + } + else if ( !(hvmemul_ctxt->insn_buf_bytes = + hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf)) ) + { + unsigned int pfec = PFEC_page_present | PFEC_insn_fetch; + unsigned long addr; + + if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 ) + pfec |= PFEC_user_mode; + hvmemul_ctxt->insn_buf_bytes = - hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?: (hvm_virtual_to_linear_addr(x86_seg_cs, &hvmemul_ctxt->seg_reg[x86_seg_cs], hvmemul_ctxt->insn_buf_eip, @@ -2795,15 +2802,9 @@ void hvm_emulate_init_per_insn( &addr) && hvm_copy_from_guest_linear(hvmemul_ctxt->insn_buf, addr, sizeof(hvmemul_ctxt->insn_buf), - pfec | PFEC_insn_fetch, - NULL) == HVMTRANS_okay) ? + pfec, NULL) == HVMTRANS_okay) ? sizeof(hvmemul_ctxt->insn_buf) : 0; } - else - { - hvmemul_ctxt->insn_buf_bytes = insn_bytes; - memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes); - } } void hvm_emulate_writeback(