From patchwork Tue Feb 7 00:16:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christoph_M=C3=BCllner?= X-Patchwork-Id: 64386 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C4D3F38493DF for ; Tue, 7 Feb 2023 00:18:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id DCC0C3858C50 for ; Tue, 7 Feb 2023 00:16:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DCC0C3858C50 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-wm1-x32b.google.com with SMTP id j29-20020a05600c1c1d00b003dc52fed235so10240132wms.1 for ; Mon, 06 Feb 2023 16:16:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n9dxq7kpd010HIi1OLQl6ApK22PyYnLk2lBE1dozjxw=; b=PM1C+fHQd/x1ptXfiv3U5ZBaN39Uz0ZtSenwYIexpAt47ycXmpdilKW0P1P191XAlo dcLc5AzN1SMcS1DhfvOo776qo6BYAlYMwDTx2CC7FH4x3n15j+RpEnMi3d3FPHXLpXPU DjoyfSZu81m2/B9BrZsXG0NtQ1/S9iH6IPtAIU2LMBbmj6qR9uJ6sHNSTyOEF28OwHOD oRZ6xhM0gBA3S5CX79oqIOUDLfsXSEBwNHf+EFtnupWLtnRnWsGL6XnRI19xOHXpxfEs nYzXIMMu616FRHhm6ztJcRhzdmCXeYrFQRszflh3y4rQmiV9c5Uou32HA3OWeGq16shr jNzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n9dxq7kpd010HIi1OLQl6ApK22PyYnLk2lBE1dozjxw=; b=Guou766CKlRQ6GrKGkmjKUEoTx02Ho9OwYE9FuMSRr9cy5PCm8krS2tD3BBJDja2Gg XmewwoyIFhi6dpX43R3Eym3iT0awQbBLoUu7v4K/F91Fe7+kK5+rtjpzrPBplUUwxYw9 kYwrxohsMrfgICX4a4BbgaVomZoSKePxYYaCeQSU514vF1h4Y7OoZXROt57OpGsAHldi ML5mwYCdlsiqVaMdKQQ06HeemyoSCbI9/FkkkGXi/gtU8Supd+2y2AAIUWLg9wjW95Q4 ycMXo8S8SB9Z1SMoFU979Ps/lgRHXTgH2Saw/dQH6Dx60QvKAloZXgYf82owb+BD8C/R 5YCQ== X-Gm-Message-State: AO0yUKUF1LEkTwDKN2qLAxi1JGHbgTPtOQXDEG5MkSImZ6RvRMAJAbuo hS2CWVInbkHuoddZlMGDomhKW1twMeL/Gu9X X-Google-Smtp-Source: AK7set9rmiqFH7Z8vCr4qUWnfeFpMoDS21e5kz2E5TDWA8ENumNeIjxSbtEToCr2AIYaHQJcqGMxuA== X-Received: by 2002:a05:600c:2b46:b0:3e0:185:44af with SMTP id e6-20020a05600c2b4600b003e0018544afmr805115wmf.20.1675728996018; Mon, 06 Feb 2023 16:16:36 -0800 (PST) Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id f1-20020a1cc901000000b003df14531724sm16862050wmb.21.2023.02.06.16.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Feb 2023 16:16:35 -0800 (PST) From: Christoph Muellner To: libc-alpha@sourceware.org, Palmer Dabbelt , Darius Rad , Andrew Waterman , DJ Delorie , Vineet Gupta , Kito Cheng , Jeff Law , Philipp Tomsich , Heiko Stuebner Cc: =?utf-8?q?Christoph_M=C3=BCllner?= Subject: [RFC PATCH 04/19] riscv: Add hart feature run-time detection framework Date: Tue, 7 Feb 2023 01:16:03 +0100 Message-Id: <20230207001618.458947-5-christoph.muellner@vrull.eu> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230207001618.458947-1-christoph.muellner@vrull.eu> References: <20230207001618.458947-1-christoph.muellner@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_MANYTO, 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: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Christoph Müllner This patch introduces a framework to detect and store hart features (e.g. ISA extensions and their parameters) for RISC-V. This patch does not introduce a concrete mechanism for run-time detection, but implements everything so that such a mechanism can be introduced. Most of the changes in this patch are inspired by similar code for other architectures, so nothing surprising should be hidden here. Signed-off-by: Christoph Müllner --- sysdeps/riscv/dl-machine.h | 13 ++++ sysdeps/riscv/ldsodefs.h | 1 + sysdeps/unix/sysv/linux/riscv/dl-procinfo.c | 62 +++++++++++++++++++ sysdeps/unix/sysv/linux/riscv/dl-procinfo.h | 46 ++++++++++++++ sysdeps/unix/sysv/linux/riscv/hart-features.c | 43 +++++++++++++ sysdeps/unix/sysv/linux/riscv/hart-features.h | 26 ++++++++ sysdeps/unix/sysv/linux/riscv/libc-start.c | 29 +++++++++ 7 files changed, 220 insertions(+) create mode 100644 sysdeps/unix/sysv/linux/riscv/dl-procinfo.c create mode 100644 sysdeps/unix/sysv/linux/riscv/dl-procinfo.h create mode 100644 sysdeps/unix/sysv/linux/riscv/hart-features.c create mode 100644 sysdeps/unix/sysv/linux/riscv/hart-features.h create mode 100644 sysdeps/unix/sysv/linux/riscv/libc-start.c diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index c0c9bd93ad..43f4f96c0e 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef _RTLD_PROLOGUE # define _RTLD_PROLOGUE(entry) \ @@ -148,6 +149,18 @@ elf_machine_fixup_plt (struct link_map *map, lookup_t t, return *reloc_addr = value; } +#define DL_PLATFORM_INIT dl_platform_init () + +static inline void __attribute__ ((unused)) +dl_platform_init (void) +{ +#ifdef SHARED + /* init_hart_features has been called early from __libc_start_main in + static executable. */ + init_hart_features (&GLRO(dl_riscv_hart_features)); +#endif /* SHARED */ +} + #endif /* !dl_machine_h */ #ifdef RESOLVE_MAP diff --git a/sysdeps/riscv/ldsodefs.h b/sysdeps/riscv/ldsodefs.h index 90e95e60c5..4b184de255 100644 --- a/sysdeps/riscv/ldsodefs.h +++ b/sysdeps/riscv/ldsodefs.h @@ -20,6 +20,7 @@ #define _RISCV_LDSODEFS_H 1 #include +#include struct La_riscv_regs; struct La_riscv_retval; diff --git a/sysdeps/unix/sysv/linux/riscv/dl-procinfo.c b/sysdeps/unix/sysv/linux/riscv/dl-procinfo.c new file mode 100644 index 0000000000..ce137d10c4 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/dl-procinfo.c @@ -0,0 +1,62 @@ +/* Data for RISC-V version of processor capability information. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + . */ + +/* This information must be kept in sync with the _DL_PLATFORM_COUNT + definitions in procinfo.h. + + If anything should be added here check whether the size of each string + is still ok with the given array size. + + All the #ifdefs in the definitions are quite irritating but + necessary if we want to avoid duplicating the information. There + are three different modes: + + - PROCINFO_DECL is defined. This means we are only interested in + declarations. + + - PROCINFO_DECL is not defined: + + + if SHARED is defined the file is included in an array + initializer. The .element = { ... } syntax is needed. + + + if SHARED is not defined a normal array initialization is + needed. + */ + +#ifndef PROCINFO_CLASS +# define PROCINFO_CLASS +#endif + +#if !IS_IN (ldconfig) +# if !defined PROCINFO_DECL && defined SHARED + ._dl_riscv_hart_features +# else +PROCINFO_CLASS struct hart_features _dl_riscv_hart_features +# endif +# ifndef PROCINFO_DECL += { } +# endif +# if !defined SHARED || defined PROCINFO_DECL +; +# else +, +# endif +#endif + +#undef PROCINFO_DECL +#undef PROCINFO_CLASS diff --git a/sysdeps/unix/sysv/linux/riscv/dl-procinfo.h b/sysdeps/unix/sysv/linux/riscv/dl-procinfo.h new file mode 100644 index 0000000000..27aaebe02d --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/dl-procinfo.h @@ -0,0 +1,46 @@ +/* RISC-V version of processor capability information handling macros. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + . */ + +#ifndef _DL_PROCINFO_H +#define _DL_PROCINFO_H 1 + +#include +#include +#include +#include + +/* We cannot provide a general printing function. */ +#define _dl_procinfo(word, val) -1 + +/* There are no hardware capabilities defined. */ +#define _dl_hwcap_string(idx) "" + +/* By default there is no important hardware capability. */ +#define HWCAP_IMPORTANT (0) + +/* We don't have any hardware capabilities. */ +#define _DL_HWCAP_COUNT 0 + +#define _dl_string_hwcap(str) (-1) + +/* There're no platforms to filter out. */ +#define _DL_HWCAP_PLATFORM 0 + +#define _dl_string_platform(str) (-1) + +#endif /* dl-procinfo.h */ diff --git a/sysdeps/unix/sysv/linux/riscv/hart-features.c b/sysdeps/unix/sysv/linux/riscv/hart-features.c new file mode 100644 index 0000000000..41111eff57 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/hart-features.c @@ -0,0 +1,43 @@ +/* Initialize hart feature data. RISC-V version. + This file is part of the GNU C Library. + Copyright (C) 2022 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 + . */ + +#include + +/* The code in this file is executed very early, so we cannot call + indirect functions because ifunc support is not initialized. + Therefore this file adds a few simple helper functions to avoid + dependencies to functions outside of this file. */ + +static inline void +inhibit_loop_to_libcall +simple_memset (void *s, int c, size_t n) +{ + char *p = (char*)s; + while (n != 0) + { + *p = c; + n--; + } +} + +/* Discover hart features and store them. */ +static inline void +init_hart_features (struct hart_features *hart_features) +{ + simple_memset (hart_features, 0, sizeof (*hart_features)); +} diff --git a/sysdeps/unix/sysv/linux/riscv/hart-features.h b/sysdeps/unix/sysv/linux/riscv/hart-features.h new file mode 100644 index 0000000000..a417cbc326 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/hart-features.h @@ -0,0 +1,26 @@ +/* Initialize CPU feature data. RISC-V version. + This file is part of the GNU C Library. + Copyright (C) 2022 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 + . */ + +#ifndef _CPU_FEATURES_RISCV_H +#define _CPU_FEATURES_RISCV_H + +struct hart_features +{ +}; + +#endif /* _CPU_FEATURES_RISCV_H */ diff --git a/sysdeps/unix/sysv/linux/riscv/libc-start.c b/sysdeps/unix/sysv/linux/riscv/libc-start.c new file mode 100644 index 0000000000..57c7c09223 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/libc-start.c @@ -0,0 +1,29 @@ +/* Override csu/libc-start.c on RISC-V + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 + . */ + +#ifndef SHARED + +# include +# include + +extern struct hart_features _dl_riscv_hart_features; + +# define ARCH_INIT_CPU_FEATURES() init_hart_features (&_dl_riscv_hart_features) + +#endif +#include