Commit 5101c7e3de for openssl.org
commit 5101c7e3deec56a02569eaaf24bdce6dfb2fda79
Author: Julian Zhu <jz531210@gmail.com>
Date: Tue Sep 29 15:31:51 2026 +0800
LoongArch64: Add HWCAP reporting and environment overrides
Report the effective HWCAP mask and allow to replace it or clear
selected bits with a "~" prefix by setting OPENSSL_loongarchcap
environment variable. This makes scalar, LSX and LASX code paths
easier to select and compare when debugging algorithm dispatch.
Signed-off-by: Julian Zhu <jz531210@gmail.com>
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Mounir Idrassi <mounir.idrassi@idrix.fr>
Merge-date: Thu Nov 19 08:52:03 2026
Merged-from: https://github.com/openssl/openssl/pull/33089
diff --git a/crypto/info.c b/crypto/info.c
index b9fca5de84..d87f706f39 100644
--- a/crypto/info.c
+++ b/crypto/info.c
@@ -24,6 +24,9 @@
#if defined(__arm__) || defined(__arm) || defined(__aarch64__)
#include "arch/arm_arch.h"
#define CPU_INFO_STR_LEN 128
+#elif defined(__loongarch__)
+#include "arch/loongarch_arch.h"
+#define CPU_INFO_STR_LEN 128
#elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
#include "arch/ppc_arch.h"
#define CPU_INFO_STR_LEN 128
@@ -89,6 +92,13 @@ DEFINE_RUN_ONCE_STATIC(init_info_strings)
cpu_info_append(CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
if ((env = getenv("OPENSSL_armcap")) != NULL)
cpu_info_append(" env:%s", env);
+#elif defined(__loongarch__)
+ const char *env;
+
+ cpu_info_append(CPUINFO_PREFIX "OPENSSL_loongarchcap=0x%x",
+ OPENSSL_loongarch_hwcap_P);
+ if ((env = getenv("OPENSSL_loongarchcap")) != NULL)
+ cpu_info_append(" env:%s", env);
#elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
const char *env;
diff --git a/crypto/loongarchcap.c b/crypto/loongarchcap.c
index 152f94fa35..d5829ae8cd 100644
--- a/crypto/loongarchcap.c
+++ b/crypto/loongarchcap.c
@@ -6,13 +6,46 @@
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
#include <sys/auxv.h>
#include "internal/cryptlib.h"
#include "arch/loongarch_arch.h"
unsigned int OPENSSL_loongarch_hwcap_P = 0;
+#if defined(__GNUC__)
+__attribute__((constructor))
+#endif
void OPENSSL_cpuid_setup(void)
{
+ const char *env;
+ static int trigger = 0;
+
+ if (trigger)
+ return;
+ trigger = 1;
+
OPENSSL_loongarch_hwcap_P = getauxval(AT_HWCAP);
+ if ((env = getenv("OPENSSL_loongarchcap")) != NULL) {
+ int clear = *env == '~';
+ const char *value = env + clear;
+ char *end;
+ unsigned long mask;
+
+ /* Ignore malformed or out-of-range overrides. */
+ if (*value < '0' || *value > '9')
+ return;
+ errno = 0;
+ mask = strtoul(value, &end, 0);
+ if (errno != 0 || *end != '\0' || mask > UINT_MAX)
+ return;
+
+ if (clear)
+ OPENSSL_loongarch_hwcap_P &= ~(unsigned int)mask;
+ else
+ OPENSSL_loongarch_hwcap_P = (unsigned int)mask;
+ }
}
diff --git a/doc/build.info b/doc/build.info
index 4926e140a2..c7c400df15 100644
--- a/doc/build.info
+++ b/doc/build.info
@@ -1603,6 +1603,10 @@ DEPEND[html/man3/OPENSSL_load_u16_le.html]=man3/OPENSSL_load_u16_le.pod
GENERATE[html/man3/OPENSSL_load_u16_le.html]=man3/OPENSSL_load_u16_le.pod
DEPEND[man/man3/OPENSSL_load_u16_le.3]=man3/OPENSSL_load_u16_le.pod
GENERATE[man/man3/OPENSSL_load_u16_le.3]=man3/OPENSSL_load_u16_le.pod
+DEPEND[html/man3/OPENSSL_loongarchcap.html]=man3/OPENSSL_loongarchcap.pod
+GENERATE[html/man3/OPENSSL_loongarchcap.html]=man3/OPENSSL_loongarchcap.pod
+DEPEND[man/man3/OPENSSL_loongarchcap.3]=man3/OPENSSL_loongarchcap.pod
+GENERATE[man/man3/OPENSSL_loongarchcap.3]=man3/OPENSSL_loongarchcap.pod
DEPEND[html/man3/OPENSSL_malloc.html]=man3/OPENSSL_malloc.pod
GENERATE[html/man3/OPENSSL_malloc.html]=man3/OPENSSL_malloc.pod
DEPEND[man/man3/OPENSSL_malloc.3]=man3/OPENSSL_malloc.pod
@@ -3492,6 +3496,7 @@ html/man3/OPENSSL_init_ssl.html \
html/man3/OPENSSL_instrument_bus.html \
html/man3/OPENSSL_load_builtin_modules.html \
html/man3/OPENSSL_load_u16_le.html \
+html/man3/OPENSSL_loongarchcap.html \
html/man3/OPENSSL_malloc.html \
html/man3/OPENSSL_ppccap.html \
html/man3/OPENSSL_riscvcap.html \
@@ -4177,6 +4182,7 @@ man/man3/OPENSSL_init_ssl.3 \
man/man3/OPENSSL_instrument_bus.3 \
man/man3/OPENSSL_load_builtin_modules.3 \
man/man3/OPENSSL_load_u16_le.3 \
+man/man3/OPENSSL_loongarchcap.3 \
man/man3/OPENSSL_malloc.3 \
man/man3/OPENSSL_ppccap.3 \
man/man3/OPENSSL_riscvcap.3 \
diff --git a/doc/man3/OPENSSL_loongarchcap.pod b/doc/man3/OPENSSL_loongarchcap.pod
new file mode 100644
index 0000000000..ff88404fc1
--- /dev/null
+++ b/doc/man3/OPENSSL_loongarchcap.pod
@@ -0,0 +1,105 @@
+=pod
+
+=head1 NAME
+
+OPENSSL_loongarchcap - the LoongArch processor capabilities vector
+
+=head1 SYNOPSIS
+
+ env OPENSSL_loongarchcap=... <application>
+
+=head1 DESCRIPTION
+
+libcrypto supports LoongArch instruction set extensions. These
+extensions are denoted by individual bits in the capabilities vector.
+On Linux, when libcrypto is initialized, the results returned by
+C<getauxval(AT_HWCAP)> are stored in the vector. These capabilities
+account for both hardware and operating system support.
+
+To override the set of instructions available to an application, you can
+set the B<OPENSSL_loongarchcap> environment variable before you start the
+application.
+
+The environment variable specifies an unsigned 32-bit capability mask.
+Numbers may be decimal, hexadecimal with a C<0x> prefix, or octal with a
+leading C<0>.
+A numeric value replaces the detected capabilities. A value prefixed with
+C<~> clears the specified bits from the detected capabilities.
+
+Empty values, signs, whitespace, trailing characters and values outside
+the unsigned 32-bit range are invalid. Invalid overrides are ignored and
+the automatically detected capabilities are used. Enabling instructions
+unsupported by the processor or operating system can cause an illegal
+instruction exception.
+
+The environment variable is read once at library initialization. Changing
+it after initialization does not change algorithm selection. This facility
+is unavailable in builds configured with B<no-asm>.
+
+Note that the capability bits are controlled independently. For example,
+clearing LSX does not also clear LASX. To disable both extensions, clear
+C<0x30>.
+
+Currently the following extensions are used:
+
+=over 4
+
+=item LSX
+
+128-bit vector extension
+
+HWCAP bit 4 (C<0x10>)
+
+=item LASX
+
+256-bit vector extension
+
+HWCAP bit 5 (C<0x20>)
+
+=back
+
+=head1 RETURN VALUES
+
+Not available.
+
+=head1 EXAMPLES
+
+Check currently detected capabilities:
+
+ $ openssl info -cpusettings
+ OPENSSL_loongarchcap=0x5e7f
+
+Note: The displayed value is the effective capabilities vector. If the
+B<OPENSSL_loongarchcap> environment variable is set, its value is also
+displayed as C<env:...>.
+
+Disable all instruction set extensions:
+
+ export OPENSSL_loongarchcap=0
+
+Only enable the LSX extension, on a system supporting it:
+
+ export OPENSSL_loongarchcap=0x10
+
+Enable both LSX and LASX, on a system supporting both:
+
+ export OPENSSL_loongarchcap=0x30
+
+Disable LASX while retaining other detected capabilities:
+
+ export OPENSSL_loongarchcap='~0x20'
+
+Disable both LSX and LASX:
+
+ export OPENSSL_loongarchcap='~0x30'
+
+=head1 COPYRIGHT
+
+Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
+
+Licensed under the Apache License 2.0 (the "License"). You may not use
+this file except in compliance with the License. You can obtain a copy
+in the file LICENSE in the source distribution or at
+L<https://www.openssl.org/source/license.html>.
+
+=cut
diff --git a/doc/man7/openssl-env.pod b/doc/man7/openssl-env.pod
index e5ee825afb..816aa3d7aa 100644
--- a/doc/man7/openssl-env.pod
+++ b/doc/man7/openssl-env.pod
@@ -275,14 +275,14 @@ the L<openssl(1)> program also transcodes the command-line parameters
from the current code page to UTF-8.
This environment variable is only checked on Microsoft Windows platforms.
-=item B<OPENSSL_armcap>, B<OPENSSL_ia32cap>, B<OPENSSL_ppccap>, B<OPENSSL_riscvcap>, B<OPENSSL_s390xcap>, B<OPENSSL_sparcv9cap>
+=item B<OPENSSL_armcap>, B<OPENSSL_ia32cap>, B<OPENSSL_loongarchcap>, B<OPENSSL_ppccap>, B<OPENSSL_riscvcap>, B<OPENSSL_s390xcap>, B<OPENSSL_sparcv9cap>
OpenSSL supports a number of different algorithm implementations for
various machines and, by default, it determines which to use based on the
processor capabilities and run time feature enquiry. These environment
variables can be used to exert more control over this selection process.
-See L<OPENSSL_armcap(3)>, L<OPENSSL_ia32cap(3)>, L<OPENSSL_ppccap(3)>,
-L<OPENSSL_riscvcap(3)> and L<OPENSSL_s390xcap(3)>.
+See L<OPENSSL_armcap(3)>, L<OPENSSL_ia32cap(3)>, L<OPENSSL_loongarchcap(3)>,
+L<OPENSSL_ppccap(3)>, L<OPENSSL_riscvcap(3)> and L<OPENSSL_s390xcap(3)>.
These variables are not considered security-sensitive.
diff --git a/util/other.syms b/util/other.syms
index 0ece2234cc..d2e891d0e9 100644
--- a/util/other.syms
+++ b/util/other.syms
@@ -4,6 +4,7 @@
#
OPENSSL_armcap environment
OPENSSL_ia32cap environment
+OPENSSL_loongarchcap environment
OPENSSL_ppccap environment
OPENSSL_riscvcap environment
OPENSSL_s390xcap environment