From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id 01C2E3858D38 for ; Thu, 16 Mar 2023 00:42:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01C2E3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x1031.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so19653pjb.0 for ; Wed, 15 Mar 2023 17:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678927376; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qXHUE5vtZ8ROsuYN8IEf1tOfEsX2MDxWkPx3M+I4CuI=; b=kyTkESjpdpZ5hmiFIDnvpQxBM6u2c0BUGu1PEKcDi951UJ+TS5DVbxGrUYxjSvhinR 9w8ak/cXLxECJnSnjvoloBhGimNpfzSEpwYvA/kJVPQ2EVoAOW83lYqKTvtEunHa1r0w bngTb3X+1l+Iy/a1ToKLC2MUQoxMePAlbe9jfgXqaYtk012PRqGZM4eDUEY4wNeEwbB0 GCQ7sE+SSuw3Iwrj+yliBHygMuWcwXSKdn+IXQPdg+f6nDUu1F7bgE0PW4485lppP99e WylEHCzl7ROwYIaT1sQhNh/MNX30Xao9e/eq/VyKmLNHNfTBBv4Yu73pX8QmSxCK9j5f nu1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678927376; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qXHUE5vtZ8ROsuYN8IEf1tOfEsX2MDxWkPx3M+I4CuI=; b=yQXXimjQ4x9MPHoyfAS+YdFNA8IpvZBaspUbRC0vgtaAV1J1sp2AflPhmqItrv6obk ckACshodOMyAz7E5X1B5anTq3DIJQ/3w21S50ehY8ElywmJXJpxlRYMwPu2Jr01F8Tuy bm9bGQTbrSLAo7LZo7YYmQoWO79EAv8Uud43+b+R2ax3ifp3SDD37ORFQEYY1T/zdYL5 MzOepvSsZOgvj9s7+nto7tfngJAvryAkvowFFXaj9rT2nTzEuWjn6rntNdgVIFSo7Ld9 pF0HvAd5isvYT8rCquSxQQQ0xwcGY1MsPmn8TZylQ+q/75ikp2eIP95gTqKYLbYoYkko MgXQ== X-Gm-Message-State: AO0yUKWxYM4lfiee2qs6K0S7pFlpXvw+U3f5OyQ8XLGB+qqE1o0IgBFN RJyP/pPFNdRtCpV68RS3WPCbD6+SVR0= X-Google-Smtp-Source: AK7set/CrgP6vEjQOkORYCqT1XiD8Svr4c2bOgaxILaZVUF5CSg7d++IVdwwzO1rC5q00J5dWBQndg== X-Received: by 2002:a05:6a20:918d:b0:cd:3bca:cfa2 with SMTP id v13-20020a056a20918d00b000cd3bcacfa2mr1599903pzd.23.1678927375869; Wed, 15 Mar 2023 17:42:55 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.161.113]) by smtp.gmail.com with ESMTPSA id q30-20020a635c1e000000b004fb10399da2sm3878445pgb.56.2023.03.15.17.42.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:42:55 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 4B05B740032; Wed, 15 Mar 2023 17:42:54 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH v2] x86: Set FSGSBASE to active if enabled by kernel Date: Wed, 15 Mar 2023 17:42:54 -0700 Message-Id: <20230316004254.2507895-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3025.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Linux kernel uses AT_HWCAP2 to indicate if FSGSBASE instructions are enabled. If the HWCAP2_FSGSBASE bit in AT_HWCAP2 is set, FSGSBASE instructions can be used in user space. Define dl_check_hwcap2 to set the FSGSBASE feature to active on Linux when the HWCAP2_FSGSBASE bit is set. Add a test to verify that FSGSBASE is active on current kernels. NB: This test will fail if the kernel doesn't set the HWCAP2_FSGSBASE bit in AT_HWCAP2 while fsgsbase shows up in /proc/cpuinfo. --- sysdeps/unix/sysv/linux/x86/dl-hwcap2.h | 27 +++++++++++++++++++++++++ sysdeps/x86/cpu-features.c | 3 +++ sysdeps/x86/dl-hwcap2.h | 21 +++++++++++++++++++ sysdeps/x86/tst-cpu-features-cpuinfo.c | 7 +++++++ 4 files changed, 58 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/x86/dl-hwcap2.h create mode 100644 sysdeps/x86/dl-hwcap2.h diff --git a/sysdeps/unix/sysv/linux/x86/dl-hwcap2.h b/sysdeps/unix/sysv/linux/x86/dl-hwcap2.h new file mode 100644 index 0000000000..28f38ea510 --- /dev/null +++ b/sysdeps/unix/sysv/linux/x86/dl-hwcap2.h @@ -0,0 +1,27 @@ +/* Handle AT_HWCAP2. Linux/x86 version. + Copyright (C) 2023 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* The following must match the kernel's . */ +#define HWCAP2_RING3MWAIT (1 << 0) +#define HWCAP2_FSGSBASE (1 << 1) + +static inline void +dl_check_hwcap2 (struct cpu_features *cpu_features) +{ + if ((GLRO(dl_hwcap2) & HWCAP2_FSGSBASE) != 0) + CPU_FEATURE_SET_ACTIVE (cpu_features, FSGSBASE); +} diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c index 822688e21f..05f966ea64 100644 --- a/sysdeps/x86/cpu-features.c +++ b/sysdeps/x86/cpu-features.c @@ -22,6 +22,7 @@ #include #include #include +#include #if HAVE_TUNABLES extern void TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *) @@ -303,6 +304,8 @@ update_active (struct cpu_features *cpu_features) CPU_FEATURE_SET_ACTIVE (cpu_features, WIDE_KL); } + dl_check_hwcap2 (cpu_features); + cpu_features->isa_1 = get_isa_level (cpu_features); } diff --git a/sysdeps/x86/dl-hwcap2.h b/sysdeps/x86/dl-hwcap2.h new file mode 100644 index 0000000000..73cd6cc37d --- /dev/null +++ b/sysdeps/x86/dl-hwcap2.h @@ -0,0 +1,21 @@ +/* Handle AT_HWCAP2. Generic x86 version. + Copyright (C) 2023 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +static inline void +dl_check_hwcap2 (struct cpu_features *cpu_features) +{ +} diff --git a/sysdeps/x86/tst-cpu-features-cpuinfo.c b/sysdeps/x86/tst-cpu-features-cpuinfo.c index c25240774e..544171028d 100644 --- a/sysdeps/x86/tst-cpu-features-cpuinfo.c +++ b/sysdeps/x86/tst-cpu-features-cpuinfo.c @@ -96,6 +96,11 @@ check_proc (const char *proc_name, const char *search_name, int flag, CPU_FEATURE_USABLE (name), \ "HAS_CPU_FEATURE (" #name ")") +#define CHECK_PROC_ACTIVE(str, name) \ + check_proc (#str, " "#str" ", CPU_FEATURE_USABLE (name), \ + CPU_FEATURE_USABLE (name), \ + "CPU_FEATURE_USABLE (" #name ")") + static int do_test (int argc, char **argv) { @@ -295,6 +300,8 @@ do_test (int argc, char **argv) fails += CHECK_PROC (xsaves, XSAVES); fails += CHECK_PROC (xtpr, XTPRUPDCTRL); + fails += CHECK_PROC_ACTIVE (fsgsbase, FSGSBASE); + printf ("%d differences between /proc/cpuinfo and glibc code.\n", fails); return (fails != 0); -- 2.39.2