From patchwork Thu Mar 16 00:42:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1757665 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=YQ8WGIgG; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PcT5K5QHlz1yWp for ; Thu, 16 Mar 2023 11:43:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DECC385840E for ; Thu, 16 Mar 2023 00:43:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DECC385840E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1678927395; bh=qXHUE5vtZ8ROsuYN8IEf1tOfEsX2MDxWkPx3M+I4CuI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=YQ8WGIgGS/+P/0wNU3HMUxbpJnNGkoAJcHVAzXqSnS88ROqZ2DjQiYbwLm+4thwR4 yVigw/F9avoYzKVdIO7Mp6K181DOtYyKVgOERWTZFpRkVyRitO1prH+FPXZ32aWZYX HHT3IjHK1gMGdRJ4aFF6uRO4eu74y4LdXnrxm4Fw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 Received: by mail-pj1-x1031.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so19653pjb.0 for ; Wed, 15 Mar 2023 17:42:56 -0700 (PDT) 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) 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 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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. Reviewed-by: Florian Weimer --- 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);