Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp988035rdd; Wed, 10 Jan 2024 05:38:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYWMD7FdxDGo1YTRhlSdd212Fn3KrkDHC19AncMxFa74oBQ1Ueya3yivBah4+zIokS5Fjh X-Received: by 2002:a17:90b:3586:b0:28d:c2ec:8950 with SMTP id mm6-20020a17090b358600b0028dc2ec8950mr129996pjb.2.1704893899177; Wed, 10 Jan 2024 05:38:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704893899; cv=none; d=google.com; s=arc-20160816; b=LRBUIyEiTAks/Mho9bVsHKscRLB5zut8AKzX7Oyw0cOXAWIW4jkzODzbqSfWhZX1Wb bQ9T1C+Dcsao0bSpZ0X8cT9mtKBqxkaB/FhwF6SS2/xE7U7uyztH7jjSrbsKr1INr+et s7mjs67Ue4VbuOiwr+z0tfWsLZG4S4rBglN5J3K+wY1EqxgnVM/OUbToX0dfFuF3gSlU 5AKtkB/Jmqq+BTkPa4SilQGhi2bDsovJoEeo2VCza0v+nvCG+WWdRTMBp+fCh0oSgMk3 54P5eFjnC+4DGZTFZB1ODqb6kFIrBuHMZ8hjtLR4p7YPjpy99QQ1XAWg8msptjk8RFzF I17A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=hoqaLgp6GdTzi+i7ry9FGBDhsXBXvIujiSYzAvKzlVs=; fh=Im2yJoKhT2xf+HqZpxeISz8gV+1D0EZ4duWzI5c4BTo=; b=xg5xogWHusc6jYywwhw8AAF2CUzlYK/IQOlJpn1sUt39SCExLg3p0wptdSnPjrIImp NFfTbsbCIV4cu8S7vIlsJYNgM/vcZc/SY2XpUEc9ouoCQlHCulvQ/fW60SxYkzb+dyQi v06scrvufB+QDIZHNivToIxvrehdZ2XasGSG/8tOgVRcThAAiKY5gCZKQIMwuanaRFVG yhdKG8Ng0t88I5vFqX5ihdjkQO57R5lFqfRlR7zHTtugPWGE/IRQgDp+y8OoiXjSq9vM JFLv/eMRDm6XGP4QlCXyBprMLk/M3CycmNIX0u0Cp3L5h82nb/qDEVHxxH+PyV1bpL+W 4EXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TPhnoxYf; spf=pass (google.com: domain of linux-kernel+bounces-22255-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22255-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q1-20020a170902788100b001d393941ae4si3674181pll.217.2024.01.10.05.38.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 05:38:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-22255-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TPhnoxYf; spf=pass (google.com: domain of linux-kernel+bounces-22255-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22255-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A0C9828501C for ; Wed, 10 Jan 2024 13:38:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D25654A995; Wed, 10 Jan 2024 13:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TPhnoxYf" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 753534A994 for ; Wed, 10 Jan 2024 13:37:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704893827; x=1736429827; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=3KjM4afpgsVkg16ec8qhOMcIMU+MFDkAz+XeEDCLYjk=; b=TPhnoxYfGeNSdELeoOn5XtcSkU7D6IW/Bg2Nj3hnlwke+dhZ4MzVXc/E FQKTo3G0bjFiAFl/JXkyUk2ERiOSPFO8TPvVGFa7E65BaAyg7JvKL6vCF NXn+gSkWb6vN6iv3eMBMWLdM3ThQKFEmyXY6eXbO5+4s8ReQPGbssFTOA DY0B/UjXfwq6oF3tkysPjegrDl35URIwHUundxkn+ZuJ1qTExezOE2rYt nv3FeZBF7/Nipz3B0F1Rpw9AnnapjlqVr8+xnLrn2xbj4ddX9n7Oo6Q5p wq5fHQXbyMiEsn+emZFj1TEY8rS04n1COJ4n4s8v/b7RO8D20CkglNG65 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10948"; a="5291538" X-IronPort-AV: E=Sophos;i="6.04,184,1695711600"; d="scan'208";a="5291538" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 05:37:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,184,1695711600"; d="scan'208";a="24271084" Received: from jganji-mobl1.gar.corp.intel.com (HELO box.shutemov.name) ([10.249.37.201]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Jan 2024 05:37:00 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 4DBCC109589; Wed, 10 Jan 2024 16:36:57 +0300 (+03) Date: Wed, 10 Jan 2024 16:36:57 +0300 From: "Kirill A. Shutemov" To: Kevin Loughlin Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Rick Edgecombe , Kees Cook , "Masami Hiramatsu (Google)" , Ze Gao , Josh Poimboeuf , Pengfei Xu , Brijesh Singh , Michael Roth , Ashish Kalra , Tom Lendacky , Joerg Roedel , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-coco@lists.linux.dev, Adam Dunlap , Peter Gonda , Jacob Xu , Sidharth Telang Subject: Re: [RFC PATCH] x86/sev: x86/sev: enforce PC-relative addressing in clang Message-ID: <20240110133657.vbpzplchgaim3bya@box> References: <20240110012640.1335694-1-kevinloughlin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240110012640.1335694-1-kevinloughlin@google.com> On Wed, Jan 10, 2024 at 01:26:39AM +0000, Kevin Loughlin wrote: > SEV/SME code can execute prior to page table fixups for kernel > relocation. However, as with global variables accessed in > __startup_64(), clang does not currently generate PC-relative accesses > for SEV/SME global variables, causing certain flavors of SEV hosts and > guests to crash. > > While an attempt was made to force PC-relative addressing for certain > global SEV/SME variables via inline assembly (see snp_cpuid_get_table() > for example), PC-relative addressing must be pervasively-enforced for > SEV/SME global variables that can be accessed prior to page table > fixups. > > To avoid the error-prone approach of manually referencing each SEV/SME > global variable via a general form of snp_cpuid_get_table(), it is > preferable to use compiler flags for position-independent code (ex: > `-fPIE`) that result in PC-relative accesses. While architecture- > specific code for Linux can be pervasively compiled as position- > independent on select architectures (ex: RISC-V), this is not currently > the case for x86-64 and would require extensive changes (see "[PATCH > RFC 00/43] x86/pie: Make kernel image's virtual address flexible" for > example). > > Fortunately, the relevant files for SEV/SME code do indeed support > position-independent clang compilation, so we can use this technique to > ensure all global variables in these files are accessed via PC-relative > addressing. > > Unlike clang, gcc does not currently allow `-fPIE` in conjunction with > `mcmodel=kernel`. Thus, to preserve existing gcc behavior, this patch > does not remove the (otherwise unnecessary) inline assembly that > already enforces PC-relative addressing for select SEV/SME globals > (mentioned above). If gcc supports these joint options in the future, > we can remove such inline assembly and also apply this patch to gcc > builds. > > Tested by successful boot of SEV-SNP guest built with clang, alongside > Adam Dunlap's necessary "[PATCH v2] x86/asm: Force native_apic_mem_read > to use mov". > Similar issues was fixed before with fixup_pointer() tricks. Have you tried looking this direction. Relevant thread starting with: https://lore.kernel.org/all/20210920192341.maue7db4lcbdn46x@box.shutemov.name -- Kiryl Shutsemau / Kirill A. Shutemov