Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1922471rdb; Wed, 31 Jan 2024 13:21:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGSJU6BIzouaQcIqeRTCGvbTXgAYL8SEfCv6lt/onaIp+XAuWZ+7RDW43WkAEg+JX6LywJM X-Received: by 2002:a17:903:584:b0:1d7:3192:de38 with SMTP id jv4-20020a170903058400b001d73192de38mr2671846plb.69.1706736080427; Wed, 31 Jan 2024 13:21:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706736080; cv=pass; d=google.com; s=arc-20160816; b=HosQLwhCbZkZ+6NMTvfOKr49t8vHkL5Yong1zbIyu9YNwOM0sxoEMGpMmOsNV5s5f6 ED6TtoDW97ErJPCq6ex3ZaSOUUg+Aw6oOjCjVCPvZZk8nbcp8OOQVfB11JymPHmZh41u HY9LxBjortXYk/NCm2XrtXUHO+dXi+JCd+GJeEm+4C8HD+2wq9IpkYjcvsp6KjJ6qfeK MUXkGEz0JnxXDRUe4T8vBvstSZvLUJUwZ4L6GAof2etvuHlxpu8p/tQaN/6Z3f+mgkxN yxaB6R4oInA4U3H6GabC6OfvLk7pX3XUrP/QDE8doTKY0bfGMgWGPUHJ/PPhWUxvZM+P 5xBQ== 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=K90tbVXhZFmfJhns82LQ9QMx3cTg6w8J9UyWPRkjn9s=; fh=W6FW51/ZH6gJeONKQsfqWYR9zr9RT8dkAs2huV1T9RI=; b=qbF9JFC71FrMU6JV59tpRj8iMTjXvNCVo0OALT1DceqK8qB7+AXIEXoDHQONjzLm55 cwtR0cplRjsDbtZU1WEyP2yAxGjEA1SExfSPa6CF7c4C7QyZSUStMeZhay9ZRaXJf0sl ruRp2khV9PCMnkta47koCHvt+uIuKNMD+f7ekkI7SQk7zyk8/WQCw0B1M/l7pYaGuX4M buYRN5+yfnl6ePAf4Q2T21poqWViyqxhJJ1r8s58IYVjFCxuKhYE8vq+F8OhuJ3jn4pR k+cOLHzQRScABqbEwV8OpxeYgtQn3ZG92GsvHm81Ed+whAAYaBtRweLYnrFErUSHYd7F Vh8g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=xIZIf12b; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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-47121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47121-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=1; AJvYcCV5KphzzpIZj6Rc3Xp1xWFJTumeFtMVpK2+vf0HKf9EaR8BiSen3pfFmdcujkqYunl2T4KtuZvBZ1d2eFwkE2iCY32TKFkat46xXwYMcw== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id j4-20020a170902da8400b001d74c169000si3124874plx.222.2024.01.31.13.21.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 13:21:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=xIZIf12b; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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-47121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47121-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id BC002B2227C for ; Wed, 31 Jan 2024 21:15:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D256639FF2; Wed, 31 Jan 2024 21:14:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xIZIf12b"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="N0BwHDMF" 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 CB72638DDE; Wed, 31 Jan 2024 21:14:43 +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=1706735685; cv=none; b=Kf8Y+oa2CAoUp7de/sWjkglDj6x6OwmfhJrd+dm9mBCI7nmUuNRTKuqWbqIeG8RRazlwW3LqOyTFnV9SMBj3qWjSJKNkKw3tCKcmgLvJkVXTtxZmNEZ/J5RfNYAGjn6wX1KV0fzvnsknX7s3kJRFxpqZIUzKufA/k82SpEiBphc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706735685; c=relaxed/simple; bh=DUdu1UUn6wALW1cj2l8XknGX0U2NFDxrahheQj1928I=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=QY6M7oQZyH4+LeX5P4WgBIvgyBNFZpvQS1eKnz/rN3tvVMwvsZp2bHh7sIqcjXtL3682YvLMNIMItzMDK1kS8UlgQTybYvDPOQw3yiSDY7B7Xri/RsfrFzkQse5u1+abFIjZVAT06rYaJ25cf5oTGi9oNp7JCFZ2ByAG6XxiKC8= 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=xIZIf12b; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=N0BwHDMF; 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: Wed, 31 Jan 2024 21:14:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1706735682; 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=K90tbVXhZFmfJhns82LQ9QMx3cTg6w8J9UyWPRkjn9s=; b=xIZIf12bbP363ruX57tRSlkAo6DK4z8NB/sO/fqduaFeJ2w7tZ0xdLsR+9sqOusJcMmt7d 4WfcpUWcO0qveTdPQ+X7tvM/hfhn/Tqnk+W8HcS2QnkMDj1Jlc9tsiA4D8IBA9kUFW5hnc x0bgPZXt/w/bgKY7sA3tnPLPODHUsX4RPBGNBG4GemiC4ZieUwmaNK14IUEqOjij0hfU+h hAT9h/dncRXfIs9j5oh5EZbgVaIslVH+bONrXDUN5r8LbGRPT89TN7vlva39L8gIBRx89i zFUNalt6xvx0mJcBE8Nd3KAcOrAa/G1AS3LR9AxpD7sv/+UoXYYisQqmiIz0wQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1706735682; 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=K90tbVXhZFmfJhns82LQ9QMx3cTg6w8J9UyWPRkjn9s=; b=N0BwHDMF0J5NZOZRYZkYlcPFpbOtG7Vwso5QPJeSv7fP8f11110XMXnEich+7qp1ZON2Fr wVz9QFzb5EmKxoBg== 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/entry: Add fred_entry_from_kvm() for VMX to handle IRQ/NMI Cc: Sean Christopherson , Thomas Gleixner , Xin Li , "Borislav Petkov (AMD)" , Shan Kang , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231205105030.8698-32-xin3.li@intel.com> References: <20231205105030.8698-32-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: <170673568142.398.18052347481450991187.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: 2e670358ec1829238c99fbff178e285d3eb43ef1 Gitweb: https://git.kernel.org/tip/2e670358ec1829238c99fbff178e285d3eb43ef1 Author: Xin Li AuthorDate: Tue, 05 Dec 2023 02:50:20 -08:00 Committer: Borislav Petkov (AMD) CommitterDate: Wed, 31 Jan 2024 22:03:16 +01:00 x86/entry: Add fred_entry_from_kvm() for VMX to handle IRQ/NMI In IRQ/NMI induced VM exits, KVM VMX needs to execute the respective handlers, which requires the software to create a FRED stack frame, and use it to invoke the handlers. Add fred_irq_entry_from_kvm() for this job. Export fred_entry_from_kvm() because VMX can be compiled as a module. Suggested-by: Sean Christopherson Suggested-by: Thomas Gleixner Signed-off-by: Xin Li Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov (AMD) Tested-by: Shan Kang Link: https://lore.kernel.org/r/20231205105030.8698-32-xin3.li@intel.com --- arch/x86/entry/entry_64_fred.S | 77 +++++++++++++++++++++++++++++++++- arch/x86/entry/entry_fred.c | 14 ++++++- arch/x86/include/asm/fred.h | 18 ++++++++- 3 files changed, 109 insertions(+) diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S index 7fe2722..a02bc6f 100644 --- a/arch/x86/entry/entry_64_fred.S +++ b/arch/x86/entry/entry_64_fred.S @@ -3,8 +3,11 @@ * The actual FRED entry points. */ +#include + #include #include +#include #include "calling.h" @@ -52,3 +55,77 @@ SYM_CODE_START_NOALIGN(asm_fred_entrypoint_kernel) FRED_EXIT ERETS SYM_CODE_END(asm_fred_entrypoint_kernel) + +#if IS_ENABLED(CONFIG_KVM_INTEL) +SYM_FUNC_START(asm_fred_entry_from_kvm) + push %rbp + mov %rsp, %rbp + + UNWIND_HINT_SAVE + + /* + * Both IRQ and NMI from VMX can be handled on current task stack + * because there is no need to protect from reentrancy and the call + * stack leading to this helper is effectively constant and shallow + * (relatively speaking). Do the same when FRED is active, i.e., no + * need to check current stack level for a stack switch. + * + * Emulate the FRED-defined redzone and stack alignment. + */ + sub $(FRED_CONFIG_REDZONE_AMOUNT << 6), %rsp + and $FRED_STACK_FRAME_RSP_MASK, %rsp + + /* + * Start to push a FRED stack frame, which is always 64 bytes: + * + * +--------+-----------------+ + * | Bytes | Usage | + * +--------+-----------------+ + * | 63:56 | Reserved | + * | 55:48 | Event Data | + * | 47:40 | SS + Event Info | + * | 39:32 | RSP | + * | 31:24 | RFLAGS | + * | 23:16 | CS + Aux Info | + * | 15:8 | RIP | + * | 7:0 | Error Code | + * +--------+-----------------+ + */ + push $0 /* Reserved, must be 0 */ + push $0 /* Event data, 0 for IRQ/NMI */ + push %rdi /* fred_ss handed in by the caller */ + push %rbp + pushf + mov $__KERNEL_CS, %rax + push %rax + + /* + * Unlike the IDT event delivery, FRED _always_ pushes an error code + * after pushing the return RIP, thus the CALL instruction CANNOT be + * used here to push the return RIP, otherwise there is no chance to + * push an error code before invoking the IRQ/NMI handler. + * + * Use LEA to get the return RIP and push it, then push an error code. + */ + lea 1f(%rip), %rax + push %rax /* Return RIP */ + push $0 /* Error code, 0 for IRQ/NMI */ + + PUSH_AND_CLEAR_REGS clear_bp=0 unwind_hint=0 + movq %rsp, %rdi /* %rdi -> pt_regs */ + call __fred_entry_from_kvm /* Call the C entry point */ + POP_REGS + ERETS +1: + /* + * Objtool doesn't understand what ERETS does, this hint tells it that + * yes, we'll reach here and with what stack state. A save/restore pair + * isn't strictly needed, but it's the simplest form. + */ + UNWIND_HINT_RESTORE + pop %rbp + RET + +SYM_FUNC_END(asm_fred_entry_from_kvm) +EXPORT_SYMBOL_GPL(asm_fred_entry_from_kvm); +#endif diff --git a/arch/x86/entry/entry_fred.c b/arch/x86/entry/entry_fred.c index 3be0269..6ecc08b 100644 --- a/arch/x86/entry/entry_fred.c +++ b/arch/x86/entry/entry_fred.c @@ -257,3 +257,17 @@ __visible noinstr void fred_entry_from_kernel(struct pt_regs *regs) return fred_bad_type(regs, error_code); } + +#if IS_ENABLED(CONFIG_KVM_INTEL) +__visible noinstr void __fred_entry_from_kvm(struct pt_regs *regs) +{ + switch (regs->fred_ss.type) { + case EVENT_TYPE_EXTINT: + return fred_extint(regs); + case EVENT_TYPE_NMI: + return fred_exc_nmi(regs); + default: + WARN_ON_ONCE(1); + } +} +#endif diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h index 16a64ff..2fa9f34 100644 --- a/arch/x86/include/asm/fred.h +++ b/arch/x86/include/asm/fred.h @@ -9,6 +9,7 @@ #include #include +#include /* * FRED event return instruction opcodes for ERET{S,U}; supported in @@ -62,12 +63,29 @@ static __always_inline unsigned long fred_event_data(struct pt_regs *regs) void asm_fred_entrypoint_user(void); void asm_fred_entrypoint_kernel(void); +void asm_fred_entry_from_kvm(struct fred_ss); __visible void fred_entry_from_user(struct pt_regs *regs); __visible void fred_entry_from_kernel(struct pt_regs *regs); +__visible void __fred_entry_from_kvm(struct pt_regs *regs); + +/* Can be called from noinstr code, thus __always_inline */ +static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) +{ + struct fred_ss ss = { + .ss =__KERNEL_DS, + .type = type, + .vector = vector, + .nmi = type == EVENT_TYPE_NMI, + .lm = 1, + }; + + asm_fred_entry_from_kvm(ss); +} #else /* CONFIG_X86_FRED */ static __always_inline unsigned long fred_event_data(struct pt_regs *regs) { return 0; } +static __always_inline void fred_entry_from_kvm(unsigned int type, unsigned int vector) { } #endif /* CONFIG_X86_FRED */ #endif /* !__ASSEMBLY__ */