Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp124730lqp; Thu, 4 Apr 2024 08:37:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWw4Lod2oy8k0J3KAfl2+4IoANuo/2UG7qXn6gNCsnATYsTExjG63rx4kq1kuYpzhDpzADzfStjPe2qib1b6Aw7gPtDivSdlyGaRoQLEw== X-Google-Smtp-Source: AGHT+IHWHEEI2cpgGQxisY9+0ggwwFheZ8IyLaoCI81PQ7DNLSkmE5BaGfwk7qwdbzcdemn1z+sn X-Received: by 2002:a05:6870:332b:b0:22e:7de8:c745 with SMTP id x43-20020a056870332b00b0022e7de8c745mr2381346oae.56.1712245057671; Thu, 04 Apr 2024 08:37:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712245057; cv=pass; d=google.com; s=arc-20160816; b=mPYqvp+3OZpMIavAn5krZ44JkTyie6HyDuUBqEXDv5epnKK8EwV6RvXVx5zGyPTLqu lGUsfRIYac46iS7P3Z72eXX5/FhRkliNRX6V0fbmDTbI7ODD2PguC/7NZcoosqhsHx3D 8kRQHiLSDJXLEnungE32p6kd29CksHfwRgic4yqS1f3ZhL2x997fuuSS4FvmA20aD8gF j3HNzfvtEWzk/Ao8JjFBSCyRDbU3eUsOeVmNyp1jjDVeIJD4RI5V/8bE74rPAA2aKOUP jmvQI22/CLuy0IE/8zs1rygFLJnlnqorvtkCVwXp8zIf+jSAnOAc3niSEoAKJMLkxLsW lESw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=yUXSMqrAFeMZhzUDd522kajaYKlUc1pmVeNZWeTCyCk=; fh=BzFAPEWjU7uoDgOHhb1YFIQiKf464XBvf5czI9hAQXc=; b=n6u1Y9Cenf0waWiBmmwzi5GfNl2YRwyOuIIEkcOV3Rl/KAB/wHhYcOuOs1ekxMkdPI OQBinnP5MFZ04rUDcN0B+z6MkJfmg3qvcBunCeODIJ35wBbJxYB7v+wCypBFJVjxlpgA zyJLvCTTFsycIGFD4j+YS2V+lx5b7FaEguz2EHxxMaVjitON3Oit+tOb0T3bJwPYH9r4 tuBnrowjriN5ci4OVM00qy1qKwdwwJ6FTBxBXROouXdGCWGv48jihTDEMUnXSesHOEzH s6c73P+8yr/AZqTvygSrBRsPwsELDUtIuwPsY+XVDEysULtsLpUwkZmvslHfeEIAPrBa 04KA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Glzqzh1P; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-131671-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131671-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id n6-20020a63e046000000b005dc9564a0afsi15243722pgj.317.2024.04.04.08.37.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 08:37:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131671-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=@google.com header.s=20230601 header.b=Glzqzh1P; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-131671-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131671-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8D85CB29CE3 for ; Thu, 4 Apr 2024 15:06:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8AAD1D54F; Thu, 4 Apr 2024 15:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Glzqzh1P" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F4B3D272 for ; Thu, 4 Apr 2024 15:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712243157; cv=none; b=eyfV6JbpIcUUEBcl+9C6aA6AOvu9tAqFV/wV6wYLdFcwzAkA7z2w01RBKbmtUwpraiafgcraFxvTMmcNBra6sUZTyX+eoo3vZ46HQuc0LQHRCGfyQnmOLfMA5mZ4KU07/8S7kxurjJDjkpyTgo9GsPazF6ZeEkxdRuQ7Nq/kjMM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712243157; c=relaxed/simple; bh=9pCwI5rLluyqtKsh2NShOZejLdkgTqnq8f0wSQ9Nb9g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=m8eSaQruiye+ZyTCG7KrAHQpyht/rPtlUF136hR/mJ+sil21j76nkZ2xW8cDvqD6a2hVSurK62GJKOVZ1G2cFE6NLrZ412Mv0zematChUxN3L4cxulof9alJHLaOmgx/l9wrm7ed+ldiC/GqfY4mP9mI6NLskCjrIJ7C4qn8LzU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Glzqzh1P; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcbfe1a42a4so1968325276.2 for ; Thu, 04 Apr 2024 08:05:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712243154; x=1712847954; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yUXSMqrAFeMZhzUDd522kajaYKlUc1pmVeNZWeTCyCk=; b=Glzqzh1PSwpinaoiw35fvGMKVNc/ejrJUbJ0TW5ou/ar5sm7NUSZBBOJZLmSwyG/wX q2TAn4EANDM8A6NLlkFnfFBitNNXoc8LHZ6f3u7czIXFRAbHQXHbh4gcyMO4jV7gdeyr A3ie/CF8NimS1rjcbPpwZRn4OwRFp4h9TuUeGXWovDIpzmaT0b8BPLc7s8W5ghGK4seH V9w4uxAMe73wsBKZgRtAQQCSi8+0ha/khlPoJNCfonMIqDtNeEVMXBl35kPmw4jA7VPs 1eYFAE/J8xzwZDZmLSZNlwfX2tC1a7+PsD/t+cGGR/GW5UMcKz9BoluJQCyDipVbZfB9 p61w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712243154; x=1712847954; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yUXSMqrAFeMZhzUDd522kajaYKlUc1pmVeNZWeTCyCk=; b=c8VXRWYYWT9YSMsyp2m5YIeUJsLYlg43GyxtV7tKEs0ZCEtWx74YiHiUfHxI8aKUC8 9Y/+kAECc9XmRyAS9nPNNvhns9gnQxKpQBhofsj0tzptkdFW6FyonZlAzIsXYlx6v03K /9bHMLeo36btzGaLGMLBq0/WQROLp4Wbgm/Q1IQNp8pVuqF4r5I2GTURTYAfqTtuIkCS IcMsIQPYnXN4ZNT4Ft6jmJYRI/iZqysUHicXVYTqpcNU0UN0uNRVA7I6lws+CV8lJ/ug UegLXbgJUFGqwrsoc1aXPfqiF+TMzEenX6EUlUxm/aFcyLCHP9YmS+bUkfx5AVwdHVLg gC4A== X-Gm-Message-State: AOJu0Yzyzl0qhaSKjzm7AOHAjiB9J3xj1wdC6HMCoMspoLPdQfKEgJj1 Utv1VcNo8DsLR3lD9z5w0V5zMiVa+d/vyKN6TvARC7/KWgMEjURQ5Meu7qrsPA+esdMMDitKQbz 2eQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2b83:b0:dc6:dfc6:4207 with SMTP id fj3-20020a0569022b8300b00dc6dfc64207mr702053ybb.10.1712243154484; Thu, 04 Apr 2024 08:05:54 -0700 (PDT) Date: Thu, 4 Apr 2024 08:05:52 -0700 In-Reply-To: <20240403153514.CC2C9D13@davehans-spike.ostc.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240403153508.7328E749@davehans-spike.ostc.intel.com> <20240403153514.CC2C9D13@davehans-spike.ostc.intel.com> Message-ID: Subject: Re: [PATCH 4/4] x86/xen: Enumerate NX from CPUID directly From: Sean Christopherson To: Dave Hansen Cc: linux-kernel@vger.kernel.org, jgross@suse.com, tglx@linutronix.de, x86@kernel.org, bp@alien8.de Content-Type: text/plain; charset="us-ascii" On Wed, Apr 03, 2024, Dave Hansen wrote: > +/* > + * It is too early for boot_cpu_has() and friends to work. > + * Use CPUID to directly enumerate NX support. > + */ > +static inline void xen_configure_nx(void) > +{ > + bool nx_supported; > + u32 eax = 0; > + > + get_cpuid_region_leaf(0x80000001, CPUID_EDX, &eax); > + > + nx_supported = eax & (X86_FEATURE_NX & 31); Heh, I wasn't looking to review this, but this is wrong, and the fly-by comment I was going to make is relevant (and got really long once I started typing...). X86_FEATURE_NX isn't a bit, it's bit position: (1*32+20) & 0x31 == 20. I.e. the code would actually need to be nx_supported = edx & BIT(X86_FEATURE_NX & 31); The TL;DR version of comment I came to give, is that KVM has a lot of infrastructure for manipulating raw CPUID values using X86_FEATURE_* flags, and that I think we should seriously consider moving much of KVM's infrastructure to core x86. KVM has several macros for doing this sort of thing, and we solve the "way shorter than any helper" conundrum is by pasting tokens, e.g. #define feature_bit(name) __feature_bit(X86_FEATURE_##name) which yields fairly readable code, e.g. this would be nx_supported = edx & feature_bit(NX); and if you want to go really terse with a macro that is local to a .c file #define F feature_bit though I doubt that's necessary outside of KVM (KVM has ~200 uses in a single function that computes the support guest CPUID). Grabbing feature_bit() directly from KVM would be cumbersome, as __feature_bit() pulls in a _lot_ of dependencies that aren't strictly necessary. But if you do do want to add a generic macro, I definitely think it's worth moving KVM's stuff to common code, because all of the dependencies are compile-time assertions to guard against incorrect usage. E.g. using the "& 31" trick on scattered flags for raw CPUID will give the wrong result, because the bit position for scattered flags doesn't match the bit position for hardware-defined CPUID. But if we went a step further, KVM's code can also programatically generate the inputs to CPUID. x86_feature_cpuid() returns a cpuid_reg structure, which gives the function, index, and register: struct cpuid_reg { u32 function; u32 index; int reg; }; E.g. if we move the KVM stuff to common code, we could have a helper like: static __always_inline bool x86_cpuid_has(unsigned int feature) { struct cpuid_reg cpuid = x86_feature_cpuid(feature); u32 val; if (!get_cpuid_region_leaf(cpuid.function, cpuid.reg, &val)) return false; return val & __feature_bit(feature); } and then the NX Xen code is more simply x86_configure_nx(x86_cpuid_has(X86_FEATURE_NX)); If we wanted to go really crazy, I bet we could figure out a way to use an alternative-like implementation to automagically redirect boot_cpuid_has() to a raw CPUID-based check if it's invoked before boot_cpu_data() is populated, without increasing the code footprint.