| 1 |
/* |
| 2 |
* Copyright 2022-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <ctype.h> |
| 13 |
#include <stdint.h> |
| 14 |
#include <openssl/crypto.h> |
| 15 |
#include "internal/cryptlib.h" |
| 16 |
|
| 17 |
#define OPENSSL_RISCVCAP_IMPL |
| 18 |
#include "crypto/riscv_arch.h" |
| 19 |
|
| 20 |
#ifdef OSSL_RISCV_HWPROBE |
| 21 |
#include <unistd.h> |
| 22 |
#include <sys/syscall.h> |
| 23 |
#include <asm/hwprobe.h> |
| 24 |
#include <sys/auxv.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
extern size_t riscv_vlen_asm(void); |
| 28 |
|
| 29 |
static size_t vlen = 0; |
| 30 |
|
| 31 |
#ifdef OSSL_RISCV_HWPROBE |
| 32 |
unsigned int OPENSSL_riscv_hwcap_P = 0; |
| 33 |
#endif |
| 34 |
|
| 35 |
uint32_t OPENSSL_rdtsc(void) |
| 36 |
{ |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt) |
| 41 |
{ |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max) |
| 46 |
{ |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static void strtoupper(const char *str, char *dst, size_t dstlen) |
| 51 |
{ |
| 52 |
for (size_t i = 0; i < dstlen; i++) { |
| 53 |
if (i == dstlen - 1 || str[i] == '\0') { |
| 54 |
dst[i] = '\0'; |
| 55 |
break; |
| 56 |
} |
| 57 |
dst[i] = toupper((unsigned char)str[i]); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* parse_env() parses a RISC-V architecture string. An example of such a string |
| 62 |
* is "rv64gc_zba_zbb_zbc_zbs". Currently, the rv64gc part is ignored |
| 63 |
* and we simply search for "_[extension]" in the arch string to see if we |
| 64 |
* should enable a given extension. |
| 65 |
*/ |
| 66 |
#define BUFLEN 256 |
| 67 |
static void parse_env(const char *envstr) |
| 68 |
{ |
| 69 |
char envstrupper[BUFLEN]; |
| 70 |
char buf[BUFLEN]; |
| 71 |
|
| 72 |
/* Convert env str to all uppercase */ |
| 73 |
strtoupper(envstr, envstrupper, sizeof(envstrupper)); |
| 74 |
|
| 75 |
for (size_t i = 0; i < kRISCVNumCaps; ++i) { |
| 76 |
size_t len = strlen(RISCV_capabilities[i].name); |
| 77 |
/* Prefix capability with underscore in preparation for search */ |
| 78 |
/* |
| 79 |
* Avoid using higher level library functions which may require |
| 80 |
* library initialization (such as BIO_snprintf) as this may be called |
| 81 |
* in a constructor before library initialization |
| 82 |
*/ |
| 83 |
if (len < BUFLEN - 1) { |
| 84 |
buf[0] = '_'; |
| 85 |
memcpy(buf + 1, RISCV_capabilities[i].name, len); |
| 86 |
buf[len + 1] = '\0'; |
| 87 |
if (strstr(envstrupper, buf) != NULL) { |
| 88 |
/* Match, set relevant bit in OPENSSL_riscvcap_P[] */ |
| 89 |
OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |= (1 << RISCV_capabilities[i].bit_offset); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef OSSL_RISCV_HWPROBE |
| 96 |
static long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, |
| 97 |
size_t cpu_count, unsigned long *cpus, |
| 98 |
unsigned int flags) |
| 99 |
{ |
| 100 |
return syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_count, cpus, flags); |
| 101 |
} |
| 102 |
|
| 103 |
static void hwprobe_to_cap(void) |
| 104 |
{ |
| 105 |
long ret; |
| 106 |
struct riscv_hwprobe pairs[OSSL_RISCV_HWPROBE_PAIR_COUNT] = { |
| 107 |
OSSL_RISCV_HWPROBE_PAIR_CONTENT |
| 108 |
}; |
| 109 |
|
| 110 |
ret = riscv_hwprobe(pairs, OSSL_RISCV_HWPROBE_PAIR_COUNT, 0, NULL, 0); |
| 111 |
/* if hwprobe syscall does not exist, ret would be -ENOSYS */ |
| 112 |
if (ret == 0) { |
| 113 |
for (size_t i = 0; i < kRISCVNumCaps; ++i) { |
| 114 |
for (size_t j = 0; j != OSSL_RISCV_HWPROBE_PAIR_COUNT; ++j) { |
| 115 |
if (pairs[j].key == RISCV_capabilities[i].hwprobe_key |
| 116 |
&& (pairs[j].value & RISCV_capabilities[i].hwprobe_value) |
| 117 |
!= 0) |
| 118 |
if (!IS_IN_DEPEND_VECTOR(RISCV_capabilities[i].bit_offset) || VECTOR_CAPABLE) |
| 119 |
/* Match, set relevant bit in OPENSSL_riscvcap_P[] */ |
| 120 |
OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |= (1 << RISCV_capabilities[i].bit_offset); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
#endif /* OSSL_RISCV_HWPROBE */ |
| 126 |
|
| 127 |
size_t riscv_vlen(void) |
| 128 |
{ |
| 129 |
return vlen; |
| 130 |
} |
| 131 |
|
| 132 |
#if defined(__GNUC__) && __GNUC__ >= 2 |
| 133 |
__attribute__((constructor)) |
| 134 |
#endif |
| 135 |
void OPENSSL_cpuid_setup(void) |
| 136 |
{ |
| 137 |
char *e; |
| 138 |
static int trigger = 0; |
| 139 |
|
| 140 |
if (trigger != 0) |
| 141 |
return; |
| 142 |
trigger = 1; |
| 143 |
|
| 144 |
if ((e = getenv("OPENSSL_riscvcap"))) { |
| 145 |
parse_env(e); |
| 146 |
} |
| 147 |
#ifdef OSSL_RISCV_HWPROBE |
| 148 |
else { |
| 149 |
OPENSSL_riscv_hwcap_P = getauxval(AT_HWCAP); |
| 150 |
hwprobe_to_cap(); |
| 151 |
} |
| 152 |
#endif |
| 153 |
|
| 154 |
if (RISCV_HAS_V()) { |
| 155 |
vlen = riscv_vlen_asm(); |
| 156 |
} |
| 157 |
} |