2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/crypto.h>
15 #include "internal/cryptlib.h"
17 #define OPENSSL_RISCVCAP_IMPL
18 #include "crypto/riscv_arch.h"
20 static void parse_env(const char *envstr);
21 static void strtoupper(char *str);
23 uint32_t OPENSSL_rdtsc(void)
28 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
33 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
38 static void strtoupper(char *str)
40 for (char *x = str; *x; ++x)
44 /* parse_env() parses a RISC-V architecture string. An example of such a string
45 * is "rv64gc_zba_zbb_zbc_zbs". Currently, the rv64gc part is ignored
46 * and we simply search for "_[extension]" in the arch string to see if we
47 * should enable a given extension.
50 static void parse_env(const char *envstr)
52 char envstrupper[BUFLEN];
55 /* Convert env str to all uppercase */
56 OPENSSL_strlcpy(envstrupper, envstr, sizeof(envstrupper));
57 strtoupper(envstrupper);
59 for (size_t i = 0; i < kRISCVNumCaps; ++i) {
60 /* Prefix capability with underscore in preparation for search */
61 BIO_snprintf(buf, BUFLEN, "_%s", RISCV_capabilities[i].name);
62 if (strstr(envstrupper, buf) != NULL) {
63 /* Match, set relevant bit in OPENSSL_riscvcap_P[] */
64 OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |=
65 (1 << RISCV_capabilities[i].bit_offset);
70 # if defined(__GNUC__) && __GNUC__>=2
71 __attribute__ ((constructor))
73 void OPENSSL_cpuid_setup(void)
76 static int trigger = 0;
82 if ((e = getenv("OPENSSL_riscvcap"))) {