Received: by 10.223.176.5 with SMTP id f5csp451674wra; Fri, 9 Feb 2018 01:39:40 -0800 (PST) X-Google-Smtp-Source: AH8x224iXFqrnNixzh08j/6/e0Udr5ChCzOD+TsNDItF5CJUj51FO3oTBgYFDaeuM7itap48bRFo X-Received: by 2002:a17:902:7247:: with SMTP id c7-v6mr2015279pll.340.1518169180025; Fri, 09 Feb 2018 01:39:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518169179; cv=none; d=google.com; s=arc-20160816; b=c3JQMm9FkW6EsOH7z6qajgXmI0I1X+G02rX7w7cWxCAvs8buoVWFnI2bNW4hg789ax 3qCMjQFEW4ZGpBqnDLyzBcOb3Uf+3QeJKenenXqVpRQWZtBnPL+9T3VqSWneNuc3HZle Ck1+MhZXLxRFtP4i1/dgUvF6Xbg2QQ2+9Z7LeI2D09U8wyli8pbmujOOXK8Ul1mqsuGu lx5n1pvkaFowAAea447k+tBrd1ptzA2xHOovZvpGs4/HlvMTLxanXrpSdafFJUM36rLv HqMA37iSnQEhaJV41sDfJXueMkMyCIR+BXXJbFxrCw0qgtTYePzzx3CT+YvXWLcN7/zs TP/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=lFVEWZD8iyFBLPwCnkcx5rWQZtK2TQ+3za/aoOilQ44=; b=GGGknwkz4uZEGLSBcfhbQsGT5M22WK9qRbVJZKwb/vufq5fXfOEd9xHr09Mxgfy+ZZ WfBx7YsBmviPOAgfHTk6oXFd2kd8AB8Xw91bMORkG+/Zh+lJ3BYd7iGQ9w0EKy9OENI5 2UfQ5JRyAsHMO7s+oKiEtk7LKFML6WPOfAqmCCj9zkzLryGL2+ny9ZzNPqggLIF2d1Pp VsuyzjWKPSy4be+q8URnoDlSsK+ZPxSptSyeSgr85K8SmxsBxlJXexy53SEEPyET6VmK cVsMbS86hXgdSleqaM0eM3bSvWuwXrf6Mr4SHZXY7JZnChCzbDzl7RDlDILAE9pKy0WD gzmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=Zk9J6ua8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r7si1159671pgu.447.2018.02.09.01.39.23; Fri, 09 Feb 2018 01:39:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=Zk9J6ua8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752253AbeBIJgU (ORCPT + 99 others); Fri, 9 Feb 2018 04:36:20 -0500 Received: from 8bytes.org ([81.169.241.247]:43094 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752111AbeBIJZ6 (ORCPT ); Fri, 9 Feb 2018 04:25:58 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 603973BF; Fri, 9 Feb 2018 10:25:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1518168351; bh=YKZJrEEwNapuiM96Xp5vYeRWdJj2UAchgcOVsK0O92I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zk9J6ua8Ki0EPVvyL5ksNAxBsvldcKta1Fe5wSe/EaJk7gQyaebTDDRklo6axFRbb 9DvO3XXm2ftELhUXt40YCRGnKdnKK4a/zYQkRcL3aNL1MYrq0Kv45u1uwi0qwLU8DS 66gDHVzaSgCLTmBgGJPvDmjvWDCxFk/dXl6GYNAdsDyHprU//C9e2A50pCUCtjRgAB Nlpudo7TyEOT+sZV5fIRtpQBg6+2X2ap2YVnZKzf6CjjRMPoEvwHaiv2OY4M5l/QKW 5STS9kKgqZkrAtdqBmemU4iU/j8PDYNg/o5U2kMCtiII2o2Ze5TsGbWpBlI7AnyC7K SXpm0rJcKeu8g== From: Joerg Roedel To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Andy Lutomirski , Dave Hansen , Josh Poimboeuf , Juergen Gross , Peter Zijlstra , Borislav Petkov , Jiri Kosina , Boris Ostrovsky , Brian Gerst , David Laight , Denys Vlasenko , Eduardo Valentin , Greg KH , Will Deacon , aliguori@amazon.com, daniel.gruss@iaik.tugraz.at, hughd@google.com, keescook@google.com, Andrea Arcangeli , Waiman Long , Pavel Machek , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 04/31] x86/entry/32: Put ESPFIX code into a macro Date: Fri, 9 Feb 2018 10:25:13 +0100 Message-Id: <1518168340-9392-5-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1518168340-9392-1-git-send-email-joro@8bytes.org> References: <1518168340-9392-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel This makes it easier to split up the shared iret code path. Signed-off-by: Joerg Roedel --- arch/x86/entry/entry_32.S | 97 ++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index e659776..0289bde 100644 --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -221,6 +221,54 @@ POP_GS_EX .endm +.macro CHECK_AND_APPLY_ESPFIX +#ifdef CONFIG_X86_ESPFIX32 +#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8) + + ALTERNATIVE "jmp .Lend_\@", "", X86_BUG_ESPFIX + + movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS + /* + * Warning: PT_OLDSS(%esp) contains the wrong/random values if we + * are returning to the kernel. + * See comments in process.c:copy_thread() for details. + */ + movb PT_OLDSS(%esp), %ah + movb PT_CS(%esp), %al + andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax + cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax + jne .Lend_\@ # returning to user-space with LDT SS + + /* + * Setup and switch to ESPFIX stack + * + * We're returning to userspace with a 16 bit stack. The CPU will not + * restore the high word of ESP for us on executing iret... This is an + * "official" bug of all the x86-compatible CPUs, which we can work + * around to make dosemu and wine happy. We do this by preloading the + * high word of ESP with the high word of the userspace ESP while + * compensating for the offset by changing to the ESPFIX segment with + * a base address that matches for the difference. + */ + mov %esp, %edx /* load kernel esp */ + mov PT_OLDESP(%esp), %eax /* load userspace esp */ + mov %dx, %ax /* eax: new kernel esp */ + sub %eax, %edx /* offset (low word is 0) */ + shr $16, %edx + mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */ + mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */ + pushl $__ESPFIX_SS + pushl %eax /* new kernel esp */ + /* + * Disable interrupts, but do not irqtrace this section: we + * will soon execute iret and the tracer was already set to + * the irqstate after the IRET: + */ + DISABLE_INTERRUPTS(CLBR_ANY) + lss (%esp), %esp /* switch to espfix segment */ +.Lend_\@: +#endif /* CONFIG_X86_ESPFIX32 */ +.endm /* * %eax: prev task * %edx: next task @@ -548,21 +596,7 @@ ENTRY(entry_INT80_32) restore_all: TRACE_IRQS_IRET .Lrestore_all_notrace: -#ifdef CONFIG_X86_ESPFIX32 - ALTERNATIVE "jmp .Lrestore_nocheck", "", X86_BUG_ESPFIX - - movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS - /* - * Warning: PT_OLDSS(%esp) contains the wrong/random values if we - * are returning to the kernel. - * See comments in process.c:copy_thread() for details. - */ - movb PT_OLDSS(%esp), %ah - movb PT_CS(%esp), %al - andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax - cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax - je .Lldt_ss # returning to user-space with LDT SS -#endif + CHECK_AND_APPLY_ESPFIX .Lrestore_nocheck: RESTORE_REGS 4 # skip orig_eax/error_code .Lirq_return: @@ -575,39 +609,6 @@ ENTRY(iret_exc ) jmp common_exception .previous _ASM_EXTABLE(.Lirq_return, iret_exc) - -#ifdef CONFIG_X86_ESPFIX32 -.Lldt_ss: -/* - * Setup and switch to ESPFIX stack - * - * We're returning to userspace with a 16 bit stack. The CPU will not - * restore the high word of ESP for us on executing iret... This is an - * "official" bug of all the x86-compatible CPUs, which we can work - * around to make dosemu and wine happy. We do this by preloading the - * high word of ESP with the high word of the userspace ESP while - * compensating for the offset by changing to the ESPFIX segment with - * a base address that matches for the difference. - */ -#define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8) - mov %esp, %edx /* load kernel esp */ - mov PT_OLDESP(%esp), %eax /* load userspace esp */ - mov %dx, %ax /* eax: new kernel esp */ - sub %eax, %edx /* offset (low word is 0) */ - shr $16, %edx - mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */ - mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */ - pushl $__ESPFIX_SS - pushl %eax /* new kernel esp */ - /* - * Disable interrupts, but do not irqtrace this section: we - * will soon execute iret and the tracer was already set to - * the irqstate after the IRET: - */ - DISABLE_INTERRUPTS(CLBR_ANY) - lss (%esp), %esp /* switch to espfix segment */ - jmp .Lrestore_nocheck -#endif ENDPROC(entry_INT80_32) .macro FIXUP_ESPFIX_STACK -- 2.7.4