From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.46.57 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991892; b=YhSzMi6YIwTCQO1lFzYcsd9jYQgKZ8UoYMuUpwu9tBNznBg1DR53MZstLymlfBhthGmG sVkiDcJ82O6ekfCQGal9puyYiLsimevUCpfZTNjOlSnBAhhG5f04/QS3YKgNDp97zZze1 zUIXWOjTH3RiL/W2/TIt3WNpVqbvO5bUv5ORYVSOqxFpCZfnuMk/YBvLSCQ04GIjPxKpT B2MBbJ+XSuconMWI9BDszEv1nrTFpjKGmsqo9nX7fhd0V7qMQWV5x0V3m8sCNe5uncavz wrYwqUDo6L4+KLG5KHHzhV1OwdpBCuUOO7D1Ed7T4TeBHc/Nx1NGMEvouNBK/tllBFg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991892; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=Y6sXIYY67/6qiJFsOhNNzJ4HWTL26uO9ln+DGCV36d0=; b=Mdnf7GVPMA+I9ZysOALZSXXCZhVq8eg+7/0so+mbns6Pw8jD+5KpPw/YHhjEyW8vJbCE cpqYzMgDD6eVjs+ZhhxQJ6I/1meau9RK9G7KdtiHC7Ut6ZQMWyYVAMYHhsAcRl2HTKgke hSUOklrVHUDJ79DXuQKTKSLsls/SlFNxSXgeuEoqQoisSSHBVMI03t7JrsJp5BMvnixlO LsOwiBchbIxHwT7p0K19DFW0sZzDZHvOkP9rhDdGbp5wYcQhgs6PKPR16zLfVF3j+dpG+ emDWcTJb2z3TCmoV4JfcepuN55d0Cvy5XmKgwG9aClqylnA0qKEc2GbOPMOWK9XgNqg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=cE1NJ2BL; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.46.57 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010057.outbound.protection.outlook.com [52.101.46.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 267CA606A336 for ; Sun, 29 Nov 2026 22:38:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 267CA606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=cE1NJ2BL ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hMPmZ5OBgdXbm9XKu3IEv2M2ZDgvHPBXRQSri4S1ihfgm9WoRB6uWWKnPiXKlv41Pra0hSRPlj8xcVksdxQ08ugBHKBLKmpRvqgL6v8T5cXGKx40acZy62FFzETN4gwt7CFv0nHuLBjbtBxfXpd1K6R045YPyP0XCmoyzoFR8W3vcUZ5CTUbc+/53H5I2nAIW7Dm/HTGKhycYaA2e4NpsjbgWFjj9vzcUNbMChdiUJoNyEo6YHEA303S9X1O2PUcHlJYmYUWGj9X8e1iB9zZZixR3aP9TbsuNIR17mdldqIfqGzuwPgNxdokWilL6x7j6+Zz/JhsqvRG3FywJHFkTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Y6sXIYY67/6qiJFsOhNNzJ4HWTL26uO9ln+DGCV36d0=; b=jspAZICjR/iGgb6h17vg79gdlSexPOoIg0mdQC7S+t7TByO+2g63BLoYtoI2Wf54TwD6g38qQrg75r1A5KK2Mdil9Rgsb5TJV7CFXTEv4X2El3sCS3YyvY+J3hpxoogK8IyOnVZg7kKFmCCE+8/GVMyXA9ASANeQQX+6fLE+jHd5IDZ/XosevuLpXzDaBfyZ1rOt67TmR7Mlfq//V1OmEyIImiCKS3cbp5OumWY2DBDjYqt9HRyIXvZjBJ2ufDc9ptRymN61XX27gHsaiYjgb/ByBXpAor7nrFVXeeu5tCzhDncRSLrhA5wdqCgGPiGKrETm9QiMsEFHTklX+bYFag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux.intel.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Y6sXIYY67/6qiJFsOhNNzJ4HWTL26uO9ln+DGCV36d0=; b=cE1NJ2BLZiNjyr4xKkz+rx363A1AYT8xG84fon37AedQxFD/5LTESRWFVeWiTX6Mp+liqi5S5ejyQI1hC2Fr04CA1u0C3pVvpqgQRCNNZZPuz4ov9ntZzZfIAamL5yFZmu5i6n8olPoJveRgftgeL/smHqhcxinfEE7q0Sk0yf0= Received: from DM6PR11CA0045.namprd11.prod.outlook.com (2603:10b6:5:14c::19) by IA0PR12MB7774.namprd12.prod.outlook.com (2603:10b6:208:430::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.11; Sun, 29 Nov 2026 22:38:07 +0000 Received: from CY7PEPF000000F3.namprd05.prod.outlook.com (2603:10b6:5:14c:cafe::57) by DM6PR11CA0045.outlook.office365.com (2603:10b6:5:14c::19) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.12 via Frontend Transport; Sun, 29 Nov 2026 22:38:07 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by CY7PEPF000000F3.mail.protection.outlook.com (10.167.247.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Sun, 29 Nov 2026 22:38:07 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Sun, 29 Nov 2026 16:38:02 -0600 From: K Prateek Nayak To: Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , the arch/x86 maintainers , CC: Andy Lutomirski , Peter Zijlstra , Rick Edgecombe , Andrew Cooper , Kees Cook , Pawan Gupta , K Prateek Nayak Subject: [RFC PATCH 27/27] x86/entry: Handle shadow stack on nested NMI Date: Sun, 29 Nov 2026 22:29:52 +0000 Message-ID: <20261129222953.9124-28-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com> References: <20261129222953.9124-1-kprateek.nayak@amd.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY7PEPF000000F3:EE_|IA0PR12MB7774:EE_ X-MS-Office365-Filtering-Correlation-Id: 1718ce1a-7caf-4206-ca76-08df4e697044 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|7416014|376014|23010399003|1800799024|82310400026|10067099003|56012099006|11063799006|261021121427099003|261026121527099003|261026140227099003|261022113327099003|261022105027099003|261022075727099003|261009223027099003|261028144727099003|261102123227099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: Od3+URiW8JhoYsmroUHp41CSO6bUzFku5h5NR2TF6G82qY0BHi94EeClBTA9RFgDjcP8lfd2ACW6z0FDehUBPnMEzQvdkRYfkkkPB08sPvNip337QejjhDa6iIyuSKfAlvN8UlcHmGiZSF2TwlO0HUhRxaBcia6+C+obBl08rK8oUzdgRzCNO8Rdeg7zN8tRkKhFocecTGzH5JoVdl6iDSKjSQiN+lTUkgd9oE/YmO8x+OH4FzlCp1qhY1To7gBQcluTn57w4v9pzj7m8Vmt5ySHcK4aq/SUK8Bnlkfr1vyuQmjAMGbjGV+Gyeo0zd7aIEZ1BibY4Tp6FsvejgpZBC6/q9vG/LYqGJoEcaxJ7/2miRyp9ETKWFb9ammZrlCf+HAa89XOwzfeSK2RVS3YZJCeYE0QDoxHX+PCRT2xCCQ6EjlpucV7uTvS8fmE9wD3/5Y/9rAhBYXfOJJy7lRWE4LrljuGdrZCpZHZ8jXJxuZWlsHaAwdKWTEFNEIuDuyusUo+awGRRqD25z5N4xehUhtQvdDos+4lokeYPbwURb+Qb+78eCLB8rLL1RZBRkgwwTjaofBnJl4aLaDtEyMIzY1AIy6gmJyOjsWb0V8bXVBrAKiwMednzEgth2E/vBsftYkmjnEDJTc+PxC7dFNVq1dWqZRY9mEAUKVyydI2VNs+D+4PB75UQV9qqRz8XhTmf8jDWsVezV2M1HY4gKmx3Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(7416014)(376014)(23010399003)(1800799024)(82310400026)(10067099003)(56012099006)(11063799006)(261021121427099003)(261026121527099003)(261026140227099003)(261022113327099003)(261022105027099003)(261022075727099003)(261009223027099003)(261028144727099003)(261102123227099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RzTgE4FkNqvAUTTz4jSTnSFgg30roP5TwWBoaBplqM0hpa5LHL52gI8YQpvMl6io0fsWmJ9/EtHynverlDc35rXT8gGxFTopjCqXiDk2E0dQgtj0bvNkT4/Xftddm6yOC7eEwtkn3tc/1+2OLyjUyYBFAOrG08AfrZyTwQEjyP880GRRzr7lJVP9/YAZhJn+pd6kETbG223+KXct5f1Dr1l3RacAVh9vGABYicdHeYGOwH+NOsBQ/gf0McHUhGa+MUAeubaWhO0poykk03styTkGoAW2nf1jMXijpI2KaZf0FsV3kJS+dWwElRGK+E0mdigKoJ8gc5q1C2rdtViNVPWL75vs4ZSyw8Lr0DaxDoBoVu36fu2o8tRKvyADFlBdHCK/YVCjkHIwoY4UzBFV6qmtnpXIypFPc/M8IBazINIcikzCnzzAUM4Nf0BooH75 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:38:07.1094 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1718ce1a-7caf-4206-ca76-08df4e697044 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY7PEPF000000F3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7774 IDT sets NMI on IST gate 1 and teh corresponding shadow shadows from INT_SSP_TAB is asserted and marked busy. On userspace NMIs that use the task's stack, clear the busy bit from the NMI shadow stack to allow nesting and switch to PL0_SSP stack. Since it is an NMI from userspace, the PL0SSP stack is empty and the stack can be used safely from the top. NMI from kernel needs special handling. Similar to the NMI from userspace handling, clear the busy bit on the IST gate token, and switch back to the same shadow stack at Gate 1 via restorssp. Once back, the shadow stack establishes the same pattern as the regular stack by duplicating { CS, LIP, SS } on top as the inner frame. The NMI entry in "repeat NMI" will replicate the entries a second time to stablish the outer frame. The structure of shadow stack is as follows: +-------------------------------+ | supervisor token | +-------------------------------+ | Original CS | | Original LIP | Hardware overwrites this slot | Original SSP | +-------------------------------+ | Inner CS | | Inner LIP | Copied from first NMI | Inner SSP | +-------------------------------+ | Outer CS | | Outer LIP | For the IRET from first NMI | Outer SSP | (BUILD_NMI_REPEAT_SHSTK_FRAME) +-------------------------------+ At the beginning, all three copies are same when it is established by the first NMI. A nested NMI, will point outer SSP to Inner SSP's slot, sets CS to, __KERNEL_CS and modify the LIP to jump to repeat_nmi just like it populates the outer IRET frame on the stack. Since the replication is consistent, the address of the outer frame is known beforehand. IRET of first NMI is supposed to consume the outer frame. If there was nesting, repeat_nmi will rebuild the outer frame it consumed during IRET in BUILD_NMI_REPEAT_SHSTK_FRAME and continue. Any nested NMI at this point will simply take the nested_nmi_out path without modifying anything on the shadow stack. If there is no nested NMI, the outer frame, which is a replica of the hardware populated frame, will transfer the control back into the kernel. This is super convoluted like the stack handling and severely unbalances the RSB. Signed-off-by: K Prateek Nayak --- arch/x86/entry/entry_64.S | 232 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index 53ea96470f15..5a25c629a4d2 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1145,6 +1145,214 @@ SYM_CODE_START_LOCAL(error_return) jmp swapgs_restore_regs_and_return_to_usermode SYM_CODE_END(error_return) +#ifdef CONFIG_X86_KERN_SHADOW_STACK + +/* + * This allocates three dummy slots on the shadow stack frame + * and copies the previous top 3 slots on to the newly + * created dummy slots. + */ +.macro BUILD_NMI_REPEAT_SHSTK_FRAME needs_scratch=1 + /* Save the scratch that will be clobbered. */ + .if \needs_scratch + pushq %rax /* Extra scratch. */ + pushq %rdx + .endif + + xor %rdx, %rdx + rdsspq %rdx + testq %rdx, %rdx + jz .Lskip_shstk\@ + + /* Replicate top 3 frames below. */ + .rept 3 + ANNOTATE_INTRA_FUNCTION_CALL + call 7771f + int3 +7771: + movq 16(%rdx), %rax + wrssq %rax, -8(%rdx) + leaq -8(%rdx), %rdx + .endr + addq $24, %rsp +.Lskip_shstk\@: + + /* Restore scratch. */ + .if \needs_scratch + popq %rdx + popq %rax + .endif +.endm + +/* + * Nested NMI Shadow Stack handling + * + * On IDT systems, the hardware asserts the IST Gate-1 token and pushes + * a { CS, LIP, SS } package onto the shadow stack. Until the token is + * occupied, a nested NMI will #GP. + * + * We mark the shadow stack non-busy and use rstorssp to switch back to + * the same shadow stack. Then, like NMI handler does on regular stack, + * we construct a similar inner frame, outer frame structure, on the + * shadow stack: + * + * +-------------------------------+ + * | supervisor token | + * +-------------------------------+ + * | Original CS | + * | Original LIP | Hardware overwrites this slot + * | Original SSP | + * +-------------------------------+ + * | Inner CS | + * | Inner LIP | Copied from first NMI + * | Inner SSP | + * +-------------------------------+ + * | Outer CS | + * | Outer LIP | For the IRET from first NMI + * | Outer SSP | (BUILD_NMI_REPEAT_SHSTK_FRAME) + * +-------------------------------+ + * + * At the beginning, all three copies are same when first NMI arrives. + * A nested NMI, will point outer SSP to Inner SSP's slot, set CS to, + * __KERNEL_CS and modify the LIP to jump to repeat_nmi just like it + * populates the stack frame. + * + * repeat_nmi will rebuild the outer frame it consumed in + * BUILD_NMI_REPEAT_SHSTK_FRAME and continue. Any nested NMI at this + * point will simply take the nested_nmi_out path to jump out. + * + * If there is no other NMI in-between, the outer frame is same as + * hardware populated one and the transfer jumps back at IRET. + * + * Note: RSB is severely unbalanced by the time we exit here. + */ +.macro INIT_NMI_SHSTK_STATE + xor %rdx, %rdx + rdsspq %rdx + testq %rdx, %rdx + jz .Lskip_shstk_init\@ + + /* Write a restore token. */ + pushq %rax /* Extra scratch. */ + movq %rdx, %rax + orq $1, %rax + wrssq %rax, -8(%rdx) + + /* + * Supervisor token is 3 slots up, at the IST SSP. + * Clear the busy bit to allow nesting. + * NMIs are still disabled. + */ + clrssbsy 24(%rdx) + + /* Switch back to the same shadow stack */ + rstorssp -8(%rdx) + movq $1, %rdx + incsspq %rdx + + /* + * Build the original copy. first_nmi falls through into + * repeat_nmi, which builds the outer copy from it. + */ + BUILD_NMI_REPEAT_SHSTK_FRAME needs_scratch=0 + + popq %rax /* Restore scratch. */ + +.Lskip_shstk_init\@: +.endm + +/* + * For CONFIG_DEBUG_ENTRY build a shadow stack frame that + * IRET-to-self can pop out. + */ +.macro BUILD_NMI_DEBUG_SHSTK_FRAME + pushq %rax /* Extra scratch. */ + pushq %rdx + + xor %eax, %eax + rdsspq %rax + testq %rax, %rax + jz .Lskip_shstk_debug\@ + + .rept 3 + ANNOTATE_INTRA_FUNCTION_CALL + call 7772f + int3 +7772: + .endr + addq $24, %rsp + + rdsspq %rdx + wrssq %rax, (%rdx) /* SSP */ + movq 16(%rsp), %rax + wrssq %rax, 8(%rdx) /* LIP */ + movq 24(%rsp), %rax + wrssq %rax, 16(%rdx) /* CS */ + +.Lskip_shstk_debug\@: + popq %rdx + popq %rax +.endm + +/* + * Fixup the outer frame to jump to "repeat_nmi" and point the + * CS, SSP to correct location to jump back to the inner copy + * 3 slots down the same shadow stack. + */ +.macro FIX_NESTED_NMI_SHSTK_FRAME + xor %rdx, %rdx + rdsspq %rdx + testq %rdx, %rdx + jz .Lskip_shstk_fix\@ + + pushq %rax + + /* + * %rdx is S0-24. Redirect the outer copy (S0-56..S0-72) to + * repeat_nmi, resuming on top of the original copy (S0-48). + */ + movq $__KERNEL_CS, %rax + wrssq %rax, -32(%rdx) /* CS */ + + movabsq $repeat_nmi, %rax + wrssq %rax, -40(%rdx) /* LIP */ + + leaq -24(%rdx), %rax + wrssq %rax, -48(%rdx) /* SSP */ + + popq %rax +.Lskip_shstk_fix\@: +.endm + +/* + * NMI from userspace uses the thread's stack. + * + * Set NMI shadow stack as non-busy to accommodate nested NMIs and + * switch to PL0_SSP stack. Simce this comes from userspace, the + * SSP0 stack is currently unused. + */ +.macro SWITCH_TO_TASK_SHSTK + rdsspq %rdx + clrssbsy (%rdx) + setssbsy +.endm + +#else + +.macro BUILD_NMI_REPEAT_SHSTK_FRAME +.endm +.macro INIT_NMI_SHSTK_STATE +.endm +.macro BUILD_NMI_DEBUG_SHSTK_FRAME +.endm +.macro FIX_NESTED_NMI_SHSTK_FRAME +.endm +.macro SWITCH_TO_TASK_SHSTK +.endm + +#endif + + /* * Runs on exception stack. Xen PV does not go through this path at all, * so we can use real assembly here. @@ -1231,6 +1439,13 @@ SYM_CODE_START(asm_exc_nmi) PUSH_AND_CLEAR_REGS rdx=(%rdx) ENCODE_FRAME_POINTER + /* + * Like the data stack, leave the NMI IST shadow stack so a nested + * NMI can claim its token, and run on the task's shadow stack. + * The IRET to user frees the task token again. + */ + ALTERNATIVE "", __stringify(SWITCH_TO_TASK_SHSTK), X86_FEATURE_KERN_SHSTK + IBRS_ENTER UNTRAIN_RET @@ -1354,6 +1569,14 @@ nested_nmi: * iteration of NMI handling. */ subq $8, %rsp + + /* + * Fix needs to be done here since we have + * 5 spare stack frame we can use that gets + * overwritten by the later push. + */ + FIX_NESTED_NMI_SHSTK_FRAME + leaq -10*8(%rsp), %rdx pushq $__KERNEL_DS pushq %rdx @@ -1371,6 +1594,9 @@ nested_nmi_out: iretq first_nmi: + + INIT_NMI_SHSTK_STATE + /* Restore rdx. */ movq (%rsp), %rdx @@ -1399,6 +1625,9 @@ first_nmi: pushfq /* RFLAGS */ pushq $__KERNEL_CS /* CS */ pushq $1f /* RIP */ + + BUILD_NMI_DEBUG_SHSTK_FRAME + iretq /* continues at repeat_nmi below */ UNWIND_HINT_IRET_REGS 1: @@ -1433,6 +1662,9 @@ repeat_nmi: pushq -6*8(%rsp) .endr subq $(5*8), %rsp + + BUILD_NMI_REPEAT_SHSTK_FRAME + end_repeat_nmi: ANNOTATE_NOENDBR // this code -- 2.43.0