From 363e941ed43c648adf4d6d0874077ddd80041e1f Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Thu, 22 Aug 2019 14:28:23 +0200 Subject: [PATCH] Add CPU info to the speed command summary Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/9669) --- apps/info.c | 7 ++++- apps/speed.c | 1 + apps/version.c | 28 +++++++++---------- crypto/cversion.c | 7 +++++ crypto/info.c | 52 ++++++++++++++++++++++++++++++++++-- doc/man1/openssl-info.pod | 5 ++++ doc/man1/openssl-version.pod | 15 +++++++++++ doc/man3/OpenSSL_version.pod | 24 ++++++++++++++++- include/openssl/crypto.h | 2 ++ 9 files changed, 122 insertions(+), 19 deletions(-) diff --git a/apps/info.c b/apps/info.c index d67ed87df3..4656141184 100644 --- a/apps/info.c +++ b/apps/info.c @@ -14,7 +14,7 @@ typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_CONFIGDIR, OPT_ENGINESDIR, OPT_MODULESDIR, OPT_DSOEXT, OPT_DIRNAMESEP, - OPT_LISTSEP, OPT_SEEDS + OPT_LISTSEP, OPT_SEEDS, OPT_CPUSETTINGS } OPTION_CHOICE; const OPTIONS info_options[] = { @@ -31,6 +31,7 @@ const OPTIONS info_options[] = { {"dirnamesep", OPT_DIRNAMESEP, '-', "Directory-filename separator"}, {"listsep", OPT_LISTSEP, '-', "List separator character"}, {"seeds", OPT_SEEDS, '-', "Seed sources"}, + {"cpusettings", OPT_CPUSETTINGS, '-', "CPU settings info"}, {NULL} }; @@ -79,6 +80,10 @@ opthelp: type = OPENSSL_INFO_SEED_SOURCE; dirty++; break; + case OPT_CPUSETTINGS: + type = OPENSSL_INFO_CPU_SETTINGS; + dirty++; + break; } } if (opt_num_rest() != 0) { diff --git a/apps/speed.c b/apps/speed.c index d71b823f2a..59594f0e49 100644 --- a/apps/speed.c +++ b/apps/speed.c @@ -3363,6 +3363,7 @@ int speed_main(int argc, char **argv) printf("%s ", BF_options()); #endif printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS)); + printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO)); } if (pr_header) { diff --git a/apps/version.c b/apps/version.c index caa3e76ffe..7350ccf78f 100644 --- a/apps/version.c +++ b/apps/version.c @@ -33,7 +33,7 @@ typedef enum OPTION_choice { OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, - OPT_B, OPT_D, OPT_E, OPT_M, OPT_F, OPT_O, OPT_P, OPT_V, OPT_A, OPT_R + OPT_B, OPT_D, OPT_E, OPT_M, OPT_F, OPT_O, OPT_P, OPT_V, OPT_A, OPT_R, OPT_C } OPTION_CHOICE; const OPTIONS version_options[] = { @@ -48,24 +48,15 @@ const OPTIONS version_options[] = { {"p", OPT_P, '-', "Show target build platform"}, {"r", OPT_R, '-', "Show random seeding options"}, {"v", OPT_V, '-', "Show library version"}, + {"c", OPT_C, '-', "Show CPU settings info"}, {NULL} }; -#if defined(OPENSSL_RAND_SEED_DEVRANDOM) || defined(OPENSSL_RAND_SEED_EGD) -static void printlist(const char *prefix, const char **dev) -{ - printf("%s (", prefix); - for ( ; *dev != NULL; dev++) - printf(" \"%s\"", *dev); - printf(" )"); -} -#endif - int version_main(int argc, char **argv) { int ret = 1, dirty = 0, seed = 0; int cflags = 0, version = 0, date = 0, options = 0, platform = 0, dir = 0; - int engdir = 0, moddir = 0; + int engdir = 0, moddir = 0, cpuinfo = 0; char *prog; OPTION_CHOICE o; @@ -108,9 +99,12 @@ opthelp: case OPT_V: dirty = version = 1; break; + case OPT_C: + dirty = cpuinfo = 1; + break; case OPT_A: seed = options = cflags = version = date = platform - = dir = engdir = moddir + = dir = engdir = moddir = cpuinfo = 1; break; } @@ -157,8 +151,12 @@ opthelp: printf("%s\n", OpenSSL_version(OPENSSL_ENGINES_DIR)); if (moddir) printf("%s\n", OpenSSL_version(OPENSSL_MODULES_DIR)); - if (seed) - printf("Seeding source: %s\n", OPENSSL_info(OPENSSL_INFO_SEED_SOURCE)); + if (seed) { + const char *src = OPENSSL_info(OPENSSL_INFO_SEED_SOURCE); + printf("Seeding source: %s\n", src ? src : "N/A"); + } + if (cpuinfo) + printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO)); ret = 0; end: return ret; diff --git a/crypto/cversion.c b/crypto/cversion.c index aef84e9c60..2a0dcf9445 100644 --- a/crypto/cversion.c +++ b/crypto/cversion.c @@ -43,6 +43,8 @@ const char *OPENSSL_version_build_metadata(void) return OPENSSL_VERSION_BUILD_METADATA_STR; } +extern char ossl_cpu_info_str[]; + const char *OpenSSL_version(int t) { switch (t) { @@ -76,6 +78,11 @@ const char *OpenSSL_version(int t) #else return "MODULESDIR: N/A"; #endif + case OPENSSL_CPU_INFO: + if (OPENSSL_info(OPENSSL_INFO_CPU_SETTINGS) != NULL) + return ossl_cpu_info_str; + else + return "CPUINFO: N/A"; } return "not available"; } diff --git a/crypto/info.c b/crypto/info.c index c5eb1fcba9..355827c451 100644 --- a/crypto/info.c +++ b/crypto/info.c @@ -7,18 +7,57 @@ * https://www.openssl.org/source/license.html */ -#include #include #include "internal/dso_conf.h" +#include "internal/thread_once.h" +#include "internal/cryptlib.h" #include "e_os.h" #include "buildinf.h" -#include "internal/thread_once.h" + +#if defined(__arm__) || defined(__arm) || defined(__aarch64__) +# include "arm_arch.h" +#endif + +/* extern declaration to avoid warning */ +extern char ossl_cpu_info_str[]; static char *seed_sources = NULL; + +char ossl_cpu_info_str[128] = ""; +#define CPUINFO_PREFIX "CPUINFO: " + static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT; DEFINE_RUN_ONCE_STATIC(init_info_strings) { +#if defined(OPENSSL_CPUID_OBJ) +# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || \ + defined(_M_AMD64) || defined(_M_X64) + const char *env; + + BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), + CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx", + (long long)OPENSSL_ia32cap_P[0] | + (long long)OPENSSL_ia32cap_P[1] << 32, + (long long)OPENSSL_ia32cap_P[2] | + (long long)OPENSSL_ia32cap_P[3] << 32); + if ((env = getenv("OPENSSL_ia32cap")) != NULL) + BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), + sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), + " env:%s", env); +# elif defined(__arm__) || defined(__arm) || defined(__aarch64__) + const char *env; + + BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str), + CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P); + if ((env = getenv("OPENSSL_armcap")) != NULL) + BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str), + sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str), + " env:%s", env); +# endif +#endif + { static char seeds[512] = ""; @@ -107,6 +146,15 @@ const char *OPENSSL_info(int t) } case OPENSSL_INFO_SEED_SOURCE: return seed_sources; + case OPENSSL_INFO_CPU_SETTINGS: + /* + * If successfully initialized, ossl_cpu_info_str will start + * with CPUINFO_PREFIX, if failed it will be an empty string. + * Strip away the CPUINFO_PREFIX which we don't need here. + */ + if (ossl_cpu_info_str[0] != '\0') + return ossl_cpu_info_str + strlen(CPUINFO_PREFIX); + break; default: break; } diff --git a/doc/man1/openssl-info.pod b/doc/man1/openssl-info.pod index 2b9383fc9d..637b60b267 100644 --- a/doc/man1/openssl-info.pod +++ b/doc/man1/openssl-info.pod @@ -15,6 +15,7 @@ B [B<-dirfilesep>] [B<-listsep]> [B<-seeds]> +[B<-cpusettings]> =head1 DESCRIPTION @@ -67,6 +68,10 @@ style lists. Outputs the randomness seed sources. +=item B<-cpusettings> + +Outputs the OpenSSL CPU settings info. + =back =head1 HISTORY diff --git a/doc/man1/openssl-version.pod b/doc/man1/openssl-version.pod index b2a235e0b7..278769423e 100644 --- a/doc/man1/openssl-version.pod +++ b/doc/man1/openssl-version.pod @@ -16,6 +16,9 @@ B [B<-p>] [B<-d>] [B<-e>] +[B<-m>] +[B<-r>] +[B<-c>] =head1 DESCRIPTION @@ -61,6 +64,18 @@ OPENSSLDIR setting. ENGINESDIR settings. +=item B<-m> + +MODULESDIR settings. + +=item B<-r> + +The random number generator source settings. + +=item B<-c> + +The OpenSSL CPU settings info. + =back =head1 NOTES diff --git a/doc/man3/OpenSSL_version.pod b/doc/man3/OpenSSL_version.pod index 9b8ecd2d2c..66abd3e4b7 100644 --- a/doc/man3/OpenSSL_version.pod +++ b/doc/man3/OpenSSL_version.pod @@ -125,6 +125,20 @@ if available or "OPENSSLDIR: N/A" otherwise. The "ENGINESDIR" setting of the library build in the form "ENGINESDIR: "..."" if available or "ENGINESDIR: N/A" otherwise. +=item OPENSSL_MODULES_DIR + +The "MODULESDIR" setting of the library build in the form "MODULESDIR: "..."" +if available or "MODULESDIR: N/A" otherwise. + +=item OPENSSL_CPU_INFO + +The current OpenSSL cpu settings. +This is the current setting of the cpu capability flags. It is usually +automatically configured but may be set via an environment variable. +The value has the same syntax as the environment variable. +For x86 the string looks like "CPUINFO: OPENSSL_ia32cap=0x123:0x456". +Or "CPUINFO: N/A" if not available, e.g. no-asm build. + =back For an unknown B, the text "not available" is returned. @@ -166,6 +180,14 @@ value of the environment variable C<$PATH> on Unix (where the separator is ":") or C<%PATH%> on Windows (where the separator is ";"). +=item OPENSSL_INFO_CPU_SETTINGS + +The current OpenSSL cpu settings. +This is the current setting of the cpu capability flags. It is usually +automatically configured but may be set via an environment variable. +The value has the same syntax as the environment variable. +For x86 the string looks like "OPENSSL_ia32cap=0x123:0x456". + =back For an unknown B, NULL is returned. @@ -224,7 +246,7 @@ with the exception of the L ones. =head1 COPYRIGHT -Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. +Copyright 2018-2019 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 diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h index e16e3e4743..05580ad93c 100644 --- a/include/openssl/crypto.h +++ b/include/openssl/crypto.h @@ -169,6 +169,7 @@ const char *OpenSSL_version(int type); # define OPENSSL_VERSION_STRING 6 # define OPENSSL_FULL_VERSION_STRING 7 # define OPENSSL_MODULES_DIR 8 +# define OPENSSL_CPU_INFO 9 const char *OPENSSL_info(int type); /* @@ -182,6 +183,7 @@ const char *OPENSSL_info(int type); # define OPENSSL_INFO_DIR_FILENAME_SEPARATOR 1005 # define OPENSSL_INFO_LIST_SEPARATOR 1006 # define OPENSSL_INFO_SEED_SOURCE 1007 +# define OPENSSL_INFO_CPU_SETTINGS 1008 int OPENSSL_issetugid(void); -- 2.39.2