Received: by 10.192.165.148 with SMTP id m20csp3583247imm; Mon, 23 Apr 2018 08:55:09 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+6we2Ka4aa00J3LadbyMZyALmp815UOa1pWoZHHnUZVY+s3HjAeYNUp6sqMJP+zR5cT/9K X-Received: by 10.98.18.212 with SMTP id 81mr11525198pfs.243.1524498909564; Mon, 23 Apr 2018 08:55:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524498909; cv=none; d=google.com; s=arc-20160816; b=H26X9y8E9PzqwPaLMFjoLysl6er0zlBv1okGqG/pJVL7HV8FcBoKGkBh3aFTUtx4dc tM/Fp4ifCQGCzqsNyw7r7pFaqOBEgOScGb80zKCx6ipV+bsZEu5M1bzBIZLHPj+J8vEG xwlH+HG41I12XEUv7cZR/juTWYPJI/FQJUFK0J3q5g+mEOaqo+UuUlpMq9oY5Kfue3Ra hm+ZipTz9CLbGYB5ZCjBwgaq3BUGFiuoJOZBlnNtNrRBMgkSOwEKHKs+6BLBLeZ32Jx6 52RIhOBz1kCD8i+rZdeaNenPyX4VP2dVOByZhHyzwaqKFVtoejRsPq69zdJfcAzPiHxF kNvg== 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=1qwHGzdWkP5jX8w2SrciXl5XH9MAxHyVcJhmfGmh2J8=; b=PEqbgQ4ZQOZXGEWRnuS9w8FmtFVSthVdlSj6Ad9S2IZ9d4N4DzsNV360fBeB+nG4UA SHiQ04R52dTHBvuiCHxv9C2zHl02fQXm+Zzpu+3JvDznSkhVExMvH41+JMFryJ7CXi1E BE0cqe1wHq2DsrOioUvr7FnsuHgnHLyGsHswhFjXZ66eLXWt6SBB0vaI7YJE4BkuBfDP Ve/1jrhCg3U8sFeLrkQ1SqnJWO/KV9p1AZvVbhTiR0mryY78DuuEKdipNPs0My6a7KGK 5465lF1FwE7WvBavfhhZuA4HIDwpkuiIxWdZA+5Mh9NCLACzOy8UTjydUqqx8xnZSQ/0 SoUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=ANqwXgGg; 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 i192si10187107pgc.380.2018.04.23.08.54.54; Mon, 23 Apr 2018 08:55:09 -0700 (PDT) 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=ANqwXgGg; 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 S1755831AbeDWPr6 (ORCPT + 99 others); Mon, 23 Apr 2018 11:47:58 -0400 Received: from 8bytes.org ([81.169.241.247]:60132 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754992AbeDWPrt (ORCPT ); Mon, 23 Apr 2018 11:47:49 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id EF4A96EE; Mon, 23 Apr 2018 17:47:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1524498463; bh=AQ11aB6K0+SrzmUbtDwHbeOfL+77K+UW3OnWN7lV/Ps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ANqwXgGgS3oS/X7FmGMa95tK6O/cZCzcFG0zSfvsv+g2cUJNFUE9yN1c7Tln9lDUq 46wMtX/LDgYAip9TJRpXfarpO6lWi6hPEoZtqA5tbzd+wQmK9UpuoMSJUmCCJUTZfW 106tL75eknK+OL/EhvzzwulsBiIuTPUaH+TAgTPXg2ohYiZL6HffC+NuNWU+TgQcnn yT8ZJMBnlsHIIre5qkeN0ZSKM8QzM3eYMTVNkU2MipqVo/6P7Rh87y9io+Jhajrgh2 tabqBpZC93dkLasAKUzq5NSDiKZDEcw2M2yY6IiiGVQUY3vJ8lLmqLQTzC5yVyU/8b MTxwrE8m9RGfw== 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 , "David H . Gutteridge" , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 04/37] x86/entry/32: Put ESPFIX code into a macro Date: Mon, 23 Apr 2018 17:47:07 +0200 Message-Id: <1524498460-25530-5-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524498460-25530-1-git-send-email-joro@8bytes.org> References: <1524498460-25530-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 ec288be..118420b 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 @@ -547,21 +595,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: @@ -579,39 +613,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