From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AG47ELtSBFIcLzBsJg5wjoBg1C6BGpOf5SlS1PyK8sfp48kk2JKWpkA+bdoyYZ2ZeLjUcjTO9AMr ARC-Seal: i=1; a=rsa-sha256; t=1520245572; cv=none; d=google.com; s=arc-20160816; b=Bl+T6uDLAYIE5td1N78WriuPiuuO9NJF/ia3lwW9XbYeJqW+2V/M9biw2dBuderzLd Kx01y33ff2xCX8nsMgR5mOvbSNYeRTTAxwlzqy4nlWqPKd8VQa+p8xyck6ZOzga1I62G I73wfRudhDtnLadKFeuUo/DhpdRB6VjjcFbsfnZbUcORu55u5FBXD0GhJtGkC0gEWEMr fkO6yUCIxtOE93tfoq+msCQJ3sCb95UJN8WqLTFWGZGiIQ0F4VqxzRjJMmFf/aqYffPR 8cLyeUkmvhva0wp3bpl3YRkNRO1k8adMHZBZNjMRjqgTZvxxn5Gmeia0REwU79L7R1zr 4oJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=FsHr9cyDtWkPGmYU2PDo95OfsWWzAjreN/HkHHuHYlM=; b=A4UvkczTlqS/W3ZEFfqE2OoC0fgS4ExP2RNaabWe79G5GXmFjCoopfTa0Km/9H86AG 5WiVdLVMJrDTq35T/OTAWLqy+6Th9A/XLicZb3yedry8YenAOM7s6HUxPcckXZyGMfS/ tPl7IYHPTo8mLPP8IpJ8WIsv+j8/s2zUlrpqGRN42phLRPiH4qLb4trqbbkXgWcO8+xS RlHtrlY4icqXOFqWvqppH1ysCae/cLs+gR9JL9bSdMVcdaDjwbe2NNIx0Nd7/PxUAx8R LZurX2PhuTEyOKTaL4+W/VS53GRQt5J+mGi7KNktmXTbSm9vIiVWW1vmE9GauuxkKR0D FiMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=IbBmpPRF; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=IbBmpPRF; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org 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/34] x86/entry/32: Restore segments before int registers Date: Mon, 5 Mar 2018 11:25:36 +0100 Message-Id: <1520245563-8444-8-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520245563-8444-1-git-send-email-joro@8bytes.org> References: <1520245563-8444-1-git-send-email-joro@8bytes.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1594093021030252223?= X-GMAIL-MSGID: =?utf-8?q?1594093021030252223?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 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