Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp137908rdb; Thu, 25 Jan 2024 10:23:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGXBCL/3ZYYBFf8Il7qa8BwaGNQ48pY7tpCQGIlvnaU6rb9bK6xljCLNTcNvpP9joVawJfb X-Received: by 2002:a05:622a:304:b0:42a:7137:ebf8 with SMTP id q4-20020a05622a030400b0042a7137ebf8mr83230qtw.26.1706207027218; Thu, 25 Jan 2024 10:23:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706207027; cv=pass; d=google.com; s=arc-20160816; b=HMUYH60m9StFiYxEKK5vdMgGKenBrYSlAAjcwGX6k/PYrb/KFV4NFmRVnq33UQNjCw w7oHCVDTbEBhwBTJLDrngKpHZ/QkWsNIy0lKW1gmH9+tK7ArBfX8yerR9jNnB5h1NOIP QESJb6CQ9/wGdPHYGsvw6tjYfrgK0agUegELraZQk2UT/5T6cmDF56wXYEadB+llqxhT kvTRnW7vbPnakllgc3nuGyBpC6rtUqhAt9L74sKh8QxLK/2e2JZMjyY0f2UrFFrqMJHk HOiqlUTTXhdsFgzbQJBUYZHUx2Ifx2ps7osYwffDOOlrEFChiBVeSeRyWxZYrLuZAYZs FrJg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=SKoHAxQn7OgUgWOM4vBJuKe7dL8QrD6l4ff3gGQEznw=; fh=U/Hd0cPmVpmsXI9UC370Q88/l4pozXEkKxB1Pi0MTn0=; b=fSofH2URHgVjuNNLN8sILbXYb4LfkcYCSSz3e3G4MxEktk68eZ6l99jFfiqMWsXh1o b9ZMZfcP/lmykX2EDkd1PqitRKyp3Br4cIB61EjCyVGHP/svx22lm/nIRJj1QYC31lFa 1vU77jFIeJTNaJuzLGyEb145a92aodJxMbQWWhZqV75WvNKgCBlB3UYNOn1aKU8/DAEt zQO188RFYRxcyiht8txN8oQWw4nF2idcq8ubEYLvvNv/d5MYjVRzNul5zCBXbU5nUOUK KYnMd4cnukyz26UP6GSLtDSIAW2wUwGMsBqTIvnea7K+qZOujYw6vxgJzwHLn18Yb63E b/Gg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=POiMYfHT; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Rdk3w0u7; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-39090-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39090-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g8-20020ac87d08000000b0042a2af5e591si13235856qtb.173.2024.01.25.10.23.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 10:23:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39090-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=POiMYfHT; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Rdk3w0u7; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-39090-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39090-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E2BE01C25EFE for ; Thu, 25 Jan 2024 18:23:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2360613A248; Thu, 25 Jan 2024 18:21:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="POiMYfHT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Rdk3w0u7" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8F1113474A; Thu, 25 Jan 2024 18:21:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706206892; cv=none; b=PW6IPhBLSpeuYUDBAJ/vP74Z8qkVGqeyjb+AF9ZUhnTyOj0Ab+AK3b7tBXzGwG8Nnmd5iistsofmUQ4uq1nWglFhbj2qj55EV9UPuo7r+9Mpij2zYA9GE1LyKwDngP3gHiqCAHDHN87toH6NsOD3QvRYXDN4IR6QBlnqsXPV7wY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706206892; c=relaxed/simple; bh=ZkcaNIDlz1Wj7CtC3enINZCK9nuZ/7epHFqtDI+HjA8=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=JA3NmMQAFYE9IejYU+5CUrYwmTIewPMB8QT8P8cvo604ZpbEzNb6wnZdZ4W199k0bUsvIRSrix6Oi5m6gkHuriPZAA53Tn6zYnDOK6e1ADFKM8geISmmxBMti/GaQuKSn6AHNrTOk4wD8JbojHMWjw06JClHycjOa6J6pZWy8fA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=POiMYfHT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Rdk3w0u7; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Thu, 25 Jan 2024 18:21:18 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1706206880; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SKoHAxQn7OgUgWOM4vBJuKe7dL8QrD6l4ff3gGQEznw=; b=POiMYfHTvGkhrKQ/HrsOjSctZ/hJx0OQdaT/VT9nCVzZJTNzLTRoIOkkU1+r9IdgKsqTPr UqDEOnZ1b3IwPRPFjVwO+b0GutIwGEp/mBqyxvPH3DJGwJshAWfX3WWj9iqJO4Jwt9o6MF MaUMWoBECaym0QNAZxw0BRw2zMt3qax9QRFS/PteZGprAz26twEd0LYAfeYDwVjYAL42Tr hV9TFkvRxLltNoh/8Bn1e/uATskzL+LEJqXNmXZryuRUbo1VLBM5TgZfdYnbgHTm33v3vU Pxgre6v77qspAac1YnUvBbFBUgCX3nhd4W68bMZ5J25EVlIv2jSJC1Fi7C7b8A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1706206880; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SKoHAxQn7OgUgWOM4vBJuKe7dL8QrD6l4ff3gGQEznw=; b=Rdk3w0u7Mt5IlFRGfak0W/p6Dl6rimBt96so61ZL8AmNLpkjcErvrEVi8Y0MbPbDSymDLf lvD2ZB4puhrfIeBQ== From: "tip-bot2 for Xin Li" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fred] x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user Cc: "H. Peter Anvin (Intel)" , Xin Li , Thomas Gleixner , Shan Kang , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231205105030.8698-30-xin3.li@intel.com> References: <20231205105030.8698-30-xin3.li@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170620687895.398.11160649698794719453.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit The following commit has been merged into the x86/fred branch of tip: Commit-ID: ea8fd5563541733a309fa00d17fe33912cef2f63 Gitweb: https://git.kernel.org/tip/ea8fd5563541733a309fa00d17fe33912cef2f63 Author: Xin Li AuthorDate: Tue, 05 Dec 2023 02:50:18 -08:00 Committer: Thomas Gleixner CommitterDate: Thu, 25 Jan 2024 19:10:33 +01:00 x86/fred: Fixup fault on ERETU by jumping to fred_entrypoint_user If the stack frame contains an invalid user context (e.g. due to invalid SS, a non-canonical RIP, etc.) the ERETU instruction will trap (#SS or #GP). >From a Linux point of view, this really should be considered a user space failure, so use the standard fault fixup mechanism to intercept the fault, fix up the exception frame, and redirect execution to fred_entrypoint_user. The end result is that it appears just as if the hardware had taken the exception immediately after completing the transition to user space. Suggested-by: H. Peter Anvin (Intel) Signed-off-by: Xin Li Signed-off-by: Thomas Gleixner Tested-by: Shan Kang Link: https://lore.kernel.org/r/20231205105030.8698-30-xin3.li@intel.com --- arch/x86/entry/entry_64_fred.S | 5 +- arch/x86/include/asm/extable_fixup_types.h | 4 +- arch/x86/mm/extable.c | 78 +++++++++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S index 5781c34..d1c2fc4 100644 --- a/arch/x86/entry/entry_64_fred.S +++ b/arch/x86/entry/entry_64_fred.S @@ -3,6 +3,7 @@ * The actual FRED entry points. */ +#include #include #include "calling.h" @@ -34,7 +35,9 @@ SYM_CODE_START_NOALIGN(asm_fred_entrypoint_user) call fred_entry_from_user SYM_INNER_LABEL(asm_fred_exit_user, SYM_L_GLOBAL) FRED_EXIT - ERETU +1: ERETU + + _ASM_EXTABLE_TYPE(1b, asm_fred_entrypoint_user, EX_TYPE_ERETU) SYM_CODE_END(asm_fred_entrypoint_user) .fill asm_fred_entrypoint_kernel - ., 1, 0xcc diff --git a/arch/x86/include/asm/extable_fixup_types.h b/arch/x86/include/asm/extable_fixup_types.h index fe63120..7acf038 100644 --- a/arch/x86/include/asm/extable_fixup_types.h +++ b/arch/x86/include/asm/extable_fixup_types.h @@ -64,6 +64,8 @@ #define EX_TYPE_UCOPY_LEN4 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(4)) #define EX_TYPE_UCOPY_LEN8 (EX_TYPE_UCOPY_LEN | EX_DATA_IMM(8)) -#define EX_TYPE_ZEROPAD 20 /* longword load with zeropad on fault */ +#define EX_TYPE_ZEROPAD 20 /* longword load with zeropad on fault */ + +#define EX_TYPE_ERETU 21 #endif diff --git a/arch/x86/mm/extable.c b/arch/x86/mm/extable.c index 271dcb2..b522933 100644 --- a/arch/x86/mm/extable.c +++ b/arch/x86/mm/extable.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -223,6 +224,79 @@ static bool ex_handler_ucopy_len(const struct exception_table_entry *fixup, return ex_handler_uaccess(fixup, regs, trapnr, fault_address); } +#ifdef CONFIG_X86_FRED +static bool ex_handler_eretu(const struct exception_table_entry *fixup, + struct pt_regs *regs, unsigned long error_code) +{ + struct pt_regs *uregs = (struct pt_regs *)(regs->sp - offsetof(struct pt_regs, orig_ax)); + unsigned short ss = uregs->ss; + unsigned short cs = uregs->cs; + + /* + * Move the NMI bit from the invalid stack frame, which caused ERETU + * to fault, to the fault handler's stack frame, thus to unblock NMI + * with the fault handler's ERETS instruction ASAP if NMI is blocked. + */ + regs->fred_ss.nmi = uregs->fred_ss.nmi; + + /* + * Sync event information to uregs, i.e., the ERETU return frame, but + * is it safe to write to the ERETU return frame which is just above + * current event stack frame? + * + * The RSP used by FRED to push a stack frame is not the value in %rsp, + * it is calculated from %rsp with the following 2 steps: + * 1) RSP = %rsp - (IA32_FRED_CONFIG & 0x1c0) // Reserve N*64 bytes + * 2) RSP = RSP & ~0x3f // Align to a 64-byte cache line + * when an event delivery doesn't trigger a stack level change. + * + * Here is an example with N*64 (N=1) bytes reserved: + * + * 64-byte cache line ==> ______________ + * |___Reserved___| + * |__Event_data__| + * |_____SS_______| + * |_____RSP______| + * |_____FLAGS____| + * |_____CS_______| + * |_____IP_______| + * 64-byte cache line ==> |__Error_code__| <== ERETU return frame + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * |______________| + * 64-byte cache line ==> |______________| <== RSP after step 1) and 2) + * |___Reserved___| + * |__Event_data__| + * |_____SS_______| + * |_____RSP______| + * |_____FLAGS____| + * |_____CS_______| + * |_____IP_______| + * 64-byte cache line ==> |__Error_code__| <== ERETS return frame + * + * Thus a new FRED stack frame will always be pushed below a previous + * FRED stack frame ((N*64) bytes may be reserved between), and it is + * safe to write to a previous FRED stack frame as they never overlap. + */ + fred_info(uregs)->edata = fred_event_data(regs); + uregs->ssx = regs->ssx; + uregs->fred_ss.ss = ss; + /* The NMI bit was moved away above */ + uregs->fred_ss.nmi = 0; + uregs->csx = regs->csx; + uregs->fred_cs.sl = 0; + uregs->fred_cs.wfe = 0; + uregs->cs = cs; + uregs->orig_ax = error_code; + + return ex_handler_default(fixup, regs); +} +#endif + int ex_get_fixup_type(unsigned long ip) { const struct exception_table_entry *e = search_exception_tables(ip); @@ -300,6 +374,10 @@ int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code, return ex_handler_ucopy_len(e, regs, trapnr, fault_addr, reg, imm); case EX_TYPE_ZEROPAD: return ex_handler_zeropad(e, regs, fault_addr); +#ifdef CONFIG_X86_FRED + case EX_TYPE_ERETU: + return ex_handler_eretu(e, regs, error_code); +#endif } BUG(); }