Skip to content

Commit

Permalink
ppc64le: support OPENSSL_ppccap ENV variable
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed May 14, 2024
1 parent e3c4f7d commit 5678e02
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crypto/fipsmodule/cpucap/cpu_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ static void handle_cpu_env(uint32_t *out, const char *in) {
int sscanf_result;
uint64_t v;
if (hex) {
sscanf_result = sscanf(in + invert + 2, "%" PRIx64, &v);
sscanf_result = sscanf(in + skip_first_byte + 2, "%" PRIx64, &v);
} else {
sscanf_result = sscanf(in + invert, "%" PRIu64, &v);
sscanf_result = sscanf(in + skip_first_byte, "%" PRIu64, &v);
}

if (!sscanf_result) {
Expand Down
45 changes: 45 additions & 0 deletions crypto/fipsmodule/cpucap/cpu_ppc64le.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,56 @@
#define PPC_FEATURE2_HAS_VCRYPTO 0x02000000
#endif

static void handle_cpu_env(unsigned long *out, const char *in) {
OPENSSL_STATIC_ASSERT(sizeof(unsigned long) == 8, PPC64LE_UNSIGNED_LONG_NOT_8_BYTES);

const int invert = in[0] == '~';
const int or = in[0] == '|';
const int skip_first_byte = (invert || or) ? 1 : 0;
const int hex = in[skip_first_byte] == '0' && in[skip_first_byte+1] == 'x';

int sscanf_result;
uint64_t v;
if (hex) {
sscanf_result = sscanf(in + skip_first_byte + 2, "%" PRIx64, &v);
} else {
sscanf_result = sscanf(in + skip_first_byte, "%" PRIu64, &v);
}

if (!sscanf_result) {
return;
}

if (invert) {
*out &= ~v;
} else if (or) {
*out |= v;
} else {
*out = v;
}
}

extern uint8_t OPENSSL_cpucap_initialized;

void OPENSSL_cpuid_setup(void) {
OPENSSL_ppc64le_hwcap2 = getauxval(AT_HWCAP2);
OPENSSL_cpucap_initialized = 1;

// OPENSSL_ppccap is a 64-bit hex string which may start with "0x".
// Prior to the value, a '~' or '|' may be given.
//
// If the '~' prefix is present:
// the value is inverted and ANDed with the probed CPUID result
// If the '|' prefix is present:
// the value is ORed with the probed CPUID result
// Otherwise:
// the value is taken as the result of the CPUID
const char *env;
env = getenv("OPENSSL_ppccap");
if (env != NULL) {
handle_cpu_env(&OPENSSL_ppc64le_hwcap2, env);
}

}

int CRYPTO_is_PPC64LE_vcrypto_capable(void) {
Expand Down

0 comments on commit 5678e02

Please sign in to comment.