Received: by 10.223.176.5 with SMTP id f5csp442351wra; Fri, 9 Feb 2018 01:28:39 -0800 (PST) X-Google-Smtp-Source: AH8x225IPnqABDHypKQLde5D1erGtMYCqsnVTw3lVBwTNis3/gmnuPEa27vOcO7BnAz+TTKxLIWL X-Received: by 2002:a17:902:125:: with SMTP id 34-v6mr2003536plb.54.1518168519658; Fri, 09 Feb 2018 01:28:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518168519; cv=none; d=google.com; s=arc-20160816; b=dNPJeW1ukK9pyYmPw854jM1TlDApG5xkeei3kAUmQCWjbSiqkxBASLzTLEszOxrgrd Tb0PcFMeJgUsllgxPlmWffXzAzBzfHxNgtxZYjeGRW2a/i0dJlH0+LXB/h3lJm8CS/du WSOV70nJcusbADmKArLNiVkqDnyt5KQ6nBqLMtKNVmg5oO/JETiopyRTvLiR6A3nzO0Y EshCdBtXkLQ+yvwEtHsX3EjXTfzAxbJTgmdArN4ax6xrKlm7PjTCa4BkPPDJ+6wmEd5S EcSaB/sVLw3McCaDpRTqREdBnTq5xBk77WAAiPjJxOBeCU5YFpDT5zhktGETI7t4abXb SB4w== 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=FsHr9cyDtWkPGmYU2PDo95OfsWWzAjreN/HkHHuHYlM=; b=n56Zp4LwYFPxTqxVU2L94icHoFj7YGC2jx0yHFhA1d4fpTeQSl2jL1h4KH/qRZD82M CRLR+3DDIC0x4V2C1ap+eu2Tf9KbPkvEHFYJfzGy4irGREyqr49F1jRegF9CRJrh6e1d gPKEpkWzf1UKUv0HA23Eu0EkwoJN7/58dlPfXCUv+Ll/OW+wTZnJIRjLE7jsLUPWi/nW AM8OOQSobzYFOGhZX2DJU5dzlk3p/eDgJYsxOQRurqXPCTuLNzo8xmxHgBxiGicJLtPD TtfXx+OsiSmoYCrwEVlLuzGsNxBAu65BmvRtcCncswxmZfRLjzUOy6/HR5rQ4TJSg79L yfJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=elXEVgVH; 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 o2si1160103pgs.314.2018.02.09.01.28.22; Fri, 09 Feb 2018 01:28: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=elXEVgVH; 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 S1752432AbeBIJ0N (ORCPT + 99 others); Fri, 9 Feb 2018 04:26:13 -0500 Received: from 8bytes.org ([81.169.241.247]:43250 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752159AbeBIJZ7 (ORCPT ); Fri, 9 Feb 2018 04:25:59 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 4D9814BB; 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=1518168352; bh=K/VCNAYFb+z4VILmScdW8838Jrj7mRY5+JOhSGxmt30=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=elXEVgVHoq1bEacOysZCKp8G1A0TWVyo2az2tLTkGYzkxCv0Q5Jz9rMw4m+/R0j4k 6hiGAwJijrWcCjxxXd4by9tCd8Yy9Dy/orYA8+a2HBazW5Np5iI/9DQPmIscj8Rzu0 qHimK8yYXMIKgWte6xScmROxPWQFxaw0RiP5/R7bz3/VBNGOdJ7EAvse3vMoPGUF3+ J19RadZurw2z3DGCd54vnUTkRDKNp/xZGWZN92zcspM9ECKdNiF+pAG7cn/DlChIrn Q0MvlIVToTv+v4wrSEwfL6+jf61JjzVrodLlfC9IBzlq2BgWDDsHL7IQIMEtbniXIl 0jD2eI/FvtIFA== 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 07/31] x86/entry/32: Restore segments before int registers Date: Fri, 9 Feb 2018 10:25:16 +0100 Message-Id: <1518168340-9392-8-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 Restoring the segments can cause exceptions that need to be handled. With PTI enabled, we still need to be on kernel cr3 when the exception happens. For the cr3-switch we need at least one integer scratch register, so we can't switch with the user integer registers already loaded. Avoid a push/pop cycle to free a register for the cr3 switch by restoring the segments first. That way the integer registers are not live yet and we can use them for the cr3 switch. This also helps in the NMI path, where we need to leave with the same cr3 as we entered. There we still have the callee-saved registers live when switching cr3s. Signed-off-by: Joerg Roedel --- arch/x86/entry/entry_32.S | 50 ++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S index 9bd7718..b39c5e2 100644 --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -92,11 +92,6 @@ .macro PUSH_GS pushl $0 .endm -.macro POP_GS pop=0 - addl $(4 + \pop), %esp -.endm -.macro POP_GS_EX -.endm /* all the rest are no-op */ .macro PTGS_TO_GS @@ -116,20 +111,6 @@ pushl %gs .endm -.macro POP_GS pop=0 -98: popl %gs - .if \pop <> 0 - add $\pop, %esp - .endif -.endm -.macro POP_GS_EX -.pushsection .fixup, "ax" -99: movl $0, (%esp) - jmp 98b -.popsection - _ASM_EXTABLE(98b, 99b) -.endm - .macro PTGS_TO_GS 98: mov PT_GS(%esp), %gs .endm @@ -201,24 +182,35 @@ popl %eax .endm -.macro RESTORE_REGS pop=0 - RESTORE_INT_REGS -1: popl %ds -2: popl %es -3: popl %fs - POP_GS \pop +.macro RESTORE_SEGMENTS +1: mov PT_DS(%esp), %ds +2: mov PT_ES(%esp), %es +3: mov PT_FS(%esp), %fs + PTGS_TO_GS .pushsection .fixup, "ax" -4: movl $0, (%esp) +4: movl $0, PT_DS(%esp) jmp 1b -5: movl $0, (%esp) +5: movl $0, PT_ES(%esp) jmp 2b -6: movl $0, (%esp) +6: movl $0, PT_FS(%esp) jmp 3b .popsection _ASM_EXTABLE(1b, 4b) _ASM_EXTABLE(2b, 5b) _ASM_EXTABLE(3b, 6b) - POP_GS_EX + PTGS_TO_GS_EX +.endm + +.macro RESTORE_SKIP_SEGMENTS pop=0 + /* Jump over the segments stored on stack */ + addl $((4 * 4) + \pop), %esp +.endm + +.macro RESTORE_REGS pop=0 + RESTORE_SEGMENTS + RESTORE_INT_REGS + /* Skip over already restored segment registers */ + RESTORE_SKIP_SEGMENTS \pop .endm .macro CHECK_AND_APPLY_ESPFIX -- 2.7.4