From patchwork Thu Sep 14 15:16:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 9953359 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 8A931602C9 for ; Thu, 14 Sep 2017 15:18:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C65C290BE for ; Thu, 14 Sep 2017 15:18:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F3A6290C2; Thu, 14 Sep 2017 15:18:55 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D2C5D290BE for ; Thu, 14 Sep 2017 15:18:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsVsg-0003Jn-CS; Thu, 14 Sep 2017 15:16:42 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dsVsf-0003JQ-1g for xen-devel@lists.xenproject.org; Thu, 14 Sep 2017 15:16:41 +0000 Received: from [85.158.143.35] by server-8.bemta-6.messagelabs.com id 99/35-17770-85D9AB95; Thu, 14 Sep 2017 15:16:40 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrPIsWRWlGSWpSXmKPExsXS6fjDSzd87q5 Ig72rDCy+b5nM5MDocfjDFZYAxijWzLyk/IoE1owF65ewFzzQrDh66TpbA+MzqS5GTg4hgTyJ Dcu2MYLYvAJ2EvsX3GYDsSUEDCVOL7zJAmKzCKhKfHu7ghXEZhNQl2h7th3I5uAQETCQOHc0C cRkFoiXuLrOHqRCWMBNYsX/44wQ04skbnfOBLM5BewlOudPA+vkFRCU+LtDGCTMLKAl8fDXLR YIW1ti2cLXzBATpSWW/+OYwMg3C6FhFpKGWUgaZiE0LGBkWcWoUZxaVJZapGtooZdUlJmeUZK bmJmja2hgppebWlycmJ6ak5hUrJecn7uJERh4DECwg/HmxoBDjJIcTEqivHt1d0YK8SXlp1Rm JBZnxBeV5qQWH2KU4eBQkuDlmLMrUkiwKDU9tSItMwcYAzBpCQ4eJRHe97OB0rzFBYm5xZnpE KlTjLocHTfv/mESYsnLz0uVEuf1BZkhAFKUUZoHNwIWj5cYZaWEeRmBjhLiKUgtys0sQZV/xS jOwagkzGsGMoUnM68EbtMroCOYgI44c3oHyBEliQgpqQZG3/z76bcyfwZnP3jUaagTd/fMWtN tfbUq+89YF8hXXteQuvay3a+6243H+JDG7Imn7Jz2Plf9+d1mdqbE38NiO3WV3r8S3npDPkhs GsdTbv2Jz2Odjxx1c1sd1dxoujbCxP229g4nX3P2I0J/fkgvzT2w9sqJxu0H9YR21kj/YDSoM L8QuL1eiaU4I9FQi7moOBEAmZ20YMICAAA= X-Env-Sender: JBeulich@suse.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1505402198!76689617!1 X-Originating-IP: [137.65.248.74] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 28418 invoked from network); 14 Sep 2017 15:16:39 -0000 Received: from prv-mh.provo.novell.com (HELO prv-mh.provo.novell.com) (137.65.248.74) by server-4.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 14 Sep 2017 15:16:39 -0000 Received: from INET-PRV-MTA by prv-mh.provo.novell.com with Novell_GroupWise; Thu, 14 Sep 2017 09:16:37 -0600 Message-Id: <59BAB974020000780017B39C@prv-mh.provo.novell.com> X-Mailer: Novell GroupWise Internet Agent 14.2.2 Date: Thu, 14 Sep 2017 09:16:36 -0600 From: "Jan Beulich" To: "xen-devel" References: <59BAB38A020000780017B34E@prv-mh.provo.novell.com> <59BAB38A020000780017B34E@prv-mh.provo.novell.com> In-Reply-To: <59BAB38A020000780017B34E@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Disposition: inline Cc: George Dunlap , Andrew Cooper Subject: [Xen-devel] [PATCH v2 08/17] x86emul: fold/eliminate some local variables X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Make i switch-wide (at once making it unsigned, as it should have been) and introduce n (for immediate use in enter and aam/aad handling). Eliminate on-stack arrays in pusha/popa handling. Use ea.val instead of a custom variable in bound handling. No (intended) functional change. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -3238,6 +3238,7 @@ x86_emulate( struct segment_register cs, sreg; struct cpuid_leaf cpuid_leaf; uint64_t msr_val; + unsigned int i, n; unsigned long dummy; case 0x00 ... 0x05: add: /* add */ @@ -3370,47 +3371,45 @@ x86_emulate( goto done; break; - case 0x60: /* pusha */ { - int i; - unsigned int regs[] = { - _regs.eax, _regs.ecx, _regs.edx, _regs.ebx, - _regs.esp, _regs.ebp, _regs.esi, _regs.edi }; - + case 0x60: /* pusha */ fail_if(!ops->write); + ea.val = _regs.esp; for ( i = 0; i < 8; i++ ) + { + void *reg = decode_register(i, &_regs, 0); + if ( (rc = ops->write(x86_seg_ss, sp_pre_dec(op_bytes), - ®s[i], op_bytes, ctxt)) != 0 ) - goto done; + reg != &_regs.esp ? reg : &ea.val, + op_bytes, ctxt)) != 0 ) + goto done; + } break; - } - - case 0x61: /* popa */ { - int i; - unsigned int dummy_esp, *regs[] = { - &_regs.edi, &_regs.esi, &_regs.ebp, &dummy_esp, - &_regs.ebx, &_regs.edx, &_regs.ecx, &_regs.eax }; + case 0x61: /* popa */ for ( i = 0; i < 8; i++ ) { + void *reg = decode_register(7 - i, &_regs, 0); + if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes), &dst.val, op_bytes, ctxt, ops)) != 0 ) goto done; + if ( reg == &_regs.r(sp) ) + continue; if ( op_bytes == 2 ) - *(uint16_t *)regs[i] = (uint16_t)dst.val; + *(uint16_t *)reg = dst.val; else - *regs[i] = dst.val; /* 64b: zero-ext done by read_ulong() */ + *(unsigned long *)reg = dst.val; } break; - } case 0x62: /* bound */ { - unsigned long src_val2; int lb, ub, idx; + generate_exception_if(src.type != OP_MEM, EXC_UD); if ( (rc = read_ulong(src.mem.seg, truncate_ea(src.mem.off + op_bytes), - &src_val2, op_bytes, ctxt, ops)) ) + &ea.val, op_bytes, ctxt, ops)) ) goto done; - ub = (op_bytes == 2) ? (int16_t)src_val2 : (int32_t)src_val2; + ub = (op_bytes == 2) ? (int16_t)ea.val : (int32_t)ea.val; lb = (op_bytes == 2) ? (int16_t)src.val : (int32_t)src.val; idx = (op_bytes == 2) ? (int16_t)dst.val : (int32_t)dst.val; generate_exception_if((idx < lb) || (idx > ub), EXC_BR); @@ -3967,10 +3966,7 @@ x86_emulate( dst.val = src.val; break; - case 0xc8: /* enter imm16,imm8 */ { - uint8_t depth = imm2 & 31; - int i; - + case 0xc8: /* enter imm16,imm8 */ dst.type = OP_REG; dst.bytes = (mode_64bit() && (op_bytes == 4)) ? 8 : op_bytes; dst.reg = (unsigned long *)&_regs.r(bp); @@ -3980,9 +3976,10 @@ x86_emulate( goto done; dst.val = _regs.r(sp); - if ( depth > 0 ) + n = imm2 & 31; + if ( n ) { - for ( i = 1; i < depth; i++ ) + for ( i = 1; i < n; i++ ) { unsigned long ebp, temp_data; ebp = truncate_word(_regs.r(bp) - i*dst.bytes, ctxt->sp_size/8); @@ -3999,7 +3996,6 @@ x86_emulate( sp_pre_dec(src.val); break; - } case 0xc9: /* leave */ /* First writeback, to %%esp. */ @@ -4094,28 +4090,21 @@ x86_emulate( goto grp2; case 0xd4: /* aam */ - case 0xd5: /* aad */ { - unsigned int base = (uint8_t)src.val; - + case 0xd5: /* aad */ + n = (uint8_t)src.val; if ( b & 0x01 ) - { - uint16_t ax = _regs.ax; - - _regs.ax = (uint8_t)(ax + ((ax >> 8) * base)); - } + _regs.ax = (uint8_t)(_regs.al + (_regs.ah * n)); else { - uint8_t al = _regs.al; - - generate_exception_if(!base, EXC_DE); - _regs.ax = ((al / base) << 8) | (al % base); + generate_exception_if(!n, EXC_DE); + _regs.al = _regs.al % n; + _regs.ah = _regs.al / n; } _regs.eflags &= ~(X86_EFLAGS_SF | X86_EFLAGS_ZF | X86_EFLAGS_PF); _regs.eflags |= !_regs.al ? X86_EFLAGS_ZF : 0; _regs.eflags |= ((int8_t)_regs.al < 0) ? X86_EFLAGS_SF : 0; _regs.eflags |= even_parity(_regs.al) ? X86_EFLAGS_PF : 0; break; - } case 0xd6: /* salc */ _regs.al = (_regs.eflags & X86_EFLAGS_CF) ? 0xff : 0x00;