From patchwork Thu Feb 16 21:54:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9578441 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 D31DB60209 for ; Thu, 16 Feb 2017 21:57:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2C061FF1E for ; Thu, 16 Feb 2017 21:57:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B514A2843B; Thu, 16 Feb 2017 21:57:19 +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=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 600AD1FF1E for ; Thu, 16 Feb 2017 21:57:19 +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 1ceU0r-0006QE-Fj; Thu, 16 Feb 2017 21:54:53 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ceU0q-0006P6-VM for xen-devel@lists.xenproject.org; Thu, 16 Feb 2017 21:54:53 +0000 Received: from [85.158.139.211] by server-1.bemta-5.messagelabs.com id 78/6A-23102-CAF16A85; Thu, 16 Feb 2017 21:54:52 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrGIsWRWlGSWpSXmKPExsVyMfTANt3V8ss iDM4+Z7P4vmUykwOjx+EPV1gCGKNYM/OS8isSWDMevHQu6OOvuDp3E1MD4wqeLkYuDiGBGYwS L+f8ZQNxWAResUisvTSdBcSREOhnldhxbwJrFyMnkBMjMf3wFCi7QuLp939MILaQgJLE1g1Lm SFGNTBJzN99ACzBJqAlsadhPhNIQkRgDrfE2p5+sB3MAr8ZJSbt6WYDqRIWMJd4f+cGO4jNIq Aq8fLDZjCbV8BS4tq8b+wQ64wlbvefBlrNwcEpYCWxdBkTiCkEVDJ3ovoERoEFjAyrGDWKU4v KUot0jUz0kooy0zNKchMzc3QNDUz1clOLixPTU3MSk4r1kvNzNzECQ6uegYFxB+PNyX6HGCU5 mJREeY8yL4sQ4kvKT6nMSCzOiC8qzUktPsQow8GhJMFbKweUEyxKTU+tSMvMAQY5TFqCg0dJh NcNJM1bXJCYW5yZDpE6xWjM0dN1+iUTx55dl18yCbHk5eelSonzuoKUCoCUZpTmwQ2CRd8lRl kpYV5GBgYGIZ6C1KLczBJU+VeM4hyMSsK8+iBTeDLzSuD2vQI6hQnolM6IpSCnlCQipKQaGPV Sn6yPNn9ygd1H46NA8Y0iBT+VlJM/3z3XscxkvX9m8pubvYEn/ruotVbssfvXUznT/E2xZPiO pZET7/bd0/7sEiDxq8bbfs361mkfN4ZfOv/9t32f2zuFytnMk4XCjnQKbdE2/VQxj8X50dT+e WWdCZpHTNz3HrnLp3k14drVVbpBxX5XmpVYijMSDbWYi4oTAQfYMSq5AgAA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-15.tower-206.messagelabs.com!1487282089!71300723!1 X-Originating-IP: [209.85.192.182] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 26791 invoked from network); 16 Feb 2017 21:54:50 -0000 Received: from mail-pf0-f182.google.com (HELO mail-pf0-f182.google.com) (209.85.192.182) by server-15.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 16 Feb 2017 21:54:50 -0000 Received: by mail-pf0-f182.google.com with SMTP id 189so8253175pfu.3 for ; Thu, 16 Feb 2017 13:54:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ogC2INpCBYPqHtDWFE4YVfEJxDcIqb2BRf9Uz8fxhI0=; b=NEK2m5kHDoEuC8MfRCnP2brgLv4tvIcHg+ry0kOAAiVVGc+6Paaokn3UQcVbAYZquO T9z2TeOAQOt81ZzcK/sa7RP6PETccGh/BkSIwaRot0T112PAbf+j6/mYZ5gGE/KiX6Hl lTXvOvJWsyzUEWNSjx6pqcgivJXbUYRvJ/nNJ8JISEGTnXE8NGn+IwaWgsZnryUdi8Ja 9lQlQWSCAiBIFNxbrVUQKZOQfEakeYnBGVE3b6atGdmJ+m6vQXWlDdEMjzR8gM5zTeF6 siiQrKUtFkU8f/9caDhpDNKRDWR/cx0V46btZ1eciHaYaSn9BFkZ2YByGQKoH56Kndp9 iwSg== 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=ogC2INpCBYPqHtDWFE4YVfEJxDcIqb2BRf9Uz8fxhI0=; b=L9zlrdSocZJUDbmJDOdaMZQnILL7h8UwvfpbBm1IOuo2lv+CK/KewYulu1BtI7Mvyx n/rUpo4nukEY7JedSKHH1mq3pMFd6SvjyS1F+Q1xMp83ymLCo0fSdU8Vmmfn1Fey89K7 1Rtz2ObY+fgPTHwtyQ3EopZtDM483XLHBIITUAWwefhygH3xGNW6Yd+QBo2sSKqWYeXK 5DuU7BKJISlhGTCwAXwkY7lN4d4h9nDC9zar4k/695Xqw7VgC/shegbSBLfdLhjNQp32 lNV8HPT1ctNtvy75+Cp7SXcil3IC+B8wzsSzanRu4JkjbaXt+q46TXw7vzMkG4n+84sB h9gQ== X-Gm-Message-State: AMke39nFqt3/40mFN24xfmNDSHheclpPPg5YS3wiY7i6Jn/EMlJZp+kt1LEZSBw9ioqZlupd X-Received: by 10.84.140.36 with SMTP id 33mr5631576pls.136.1487282089224; Thu, 16 Feb 2017 13:54:49 -0800 (PST) Received: from skynet.sea.corp.google.com ([100.100.206.185]) by smtp.gmail.com with ESMTPSA id s65sm15391637pgb.64.2017.02.16.13.54.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 Feb 2017 13:54:48 -0800 (PST) From: Thomas Garnier To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Jonathan Corbet , Thomas Garnier , Kees Cook , Lorenzo Stoakes , Juergen Gross , Andy Lutomirski , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Jiri Kosina , Matt Fleming , Ard Biesheuvel , Boris Ostrovsky , Rusty Russell , Borislav Petkov , Christian Borntraeger , Brian Gerst , He Chen , "Luis R . Rodriguez" , Stanislaw Gruszka , Arnd Bergmann , Peter Zijlstra , Dave Hansen , Vitaly Kuznetsov , Paul Gortmaker , Josh Poimboeuf , Tim Chen , Andi Kleen , Jiri Olsa , Michael Ellerman , Joerg Roedel , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Andy Lutomirski Date: Thu, 16 Feb 2017 13:54:38 -0800 Message-Id: <20170216215438.126111-4-thgarnie@google.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170216215438.126111-1-thgarnie@google.com> References: <20170216215438.126111-1-thgarnie@google.com> Cc: linux-efi@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, lguest@lists.ozlabs.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v4 4/4] KVM: VMX: Simplify segment_base 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The KVM segment_base function is confusing. This patch replaces integers with appropriate flags, simplify constructs and add comments. Signed-off-by: Thomas Garnier --- Based on next-20170213 --- arch/x86/kvm/vmx.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 99167f20bc34..91e619269128 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2060,27 +2060,37 @@ static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) static unsigned long segment_base(u16 selector) { struct desc_struct *d; - unsigned long table_base; + struct desc_struct *table_base; unsigned long v; + u32 high32; - if (!(selector & ~3)) + if (!(selector & ~SEGMENT_RPL_MASK)) return 0; - table_base = get_current_gdt_rw_vaddr(); - - if (selector & 4) { /* from ldt */ + /* LDT selector */ + if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { u16 ldt_selector = kvm_read_ldt(); - if (!(ldt_selector & ~3)) + if (!(ldt_selector & ~SEGMENT_RPL_MASK)) return 0; - table_base = segment_base(ldt_selector); + table_base = (struct desc_struct *)segment_base(ldt_selector); + } else { + table_base = get_current_gdt_rw(); } - d = (struct desc_struct *)(table_base + (selector & ~7)); + + d = table_base + (selector >> 3); v = get_desc_base(d); #ifdef CONFIG_X86_64 - if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) - v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; + /* + * Extend the virtual address if we have a system descriptor entry for + * LDT or TSS (available or busy). + */ + if (d->s == 0 && (d->type == DESC_LDT || d->type == DESC_TSS || + d->type == 11/*Busy TSS */)) { + high32 = ((struct ldttss_desc64 *)d)->base3; + v |= (u64)high32 << 32; + } #endif return v; }