xref: /src/crypto/openssl/crypto/riscvcap.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2022-2025 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 void parse_env(const char *envstr);
30 static void strtoupper(char *str);
31 
32 static size_t vlen = 0;
33 
34 #ifdef OSSL_RISCV_HWPROBE
35 unsigned int OPENSSL_riscv_hwcap_P = 0;
36 #endif
37 
OPENSSL_rdtsc(void)38 uint32_t OPENSSL_rdtsc(void)
39 {
40     return 0;
41 }
42 
OPENSSL_instrument_bus(unsigned int * out,size_t cnt)43 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
44 {
45     return 0;
46 }
47 
OPENSSL_instrument_bus2(unsigned int * out,size_t cnt,size_t max)48 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
49 {
50     return 0;
51 }
52 
strtoupper(char * str)53 static void strtoupper(char *str)
54 {
55     for (char *x = str; *x; ++x)
56         *x = toupper((unsigned char)*x);
57 }
58 
59 /* parse_env() parses a RISC-V architecture string. An example of such a string
60  * is "rv64gc_zba_zbb_zbc_zbs". Currently, the rv64gc part is ignored
61  * and we simply search for "_[extension]" in the arch string to see if we
62  * should enable a given extension.
63  */
64 #define BUFLEN 256
parse_env(const char * envstr)65 static void parse_env(const char *envstr)
66 {
67     char envstrupper[BUFLEN];
68     char buf[BUFLEN];
69 
70     /* Convert env str to all uppercase */
71     OPENSSL_strlcpy(envstrupper, envstr, sizeof(envstrupper));
72     strtoupper(envstrupper);
73 
74     for (size_t i = 0; i < kRISCVNumCaps; ++i) {
75         /* Prefix capability with underscore in preparation for search */
76         BIO_snprintf(buf, BUFLEN, "_%s", RISCV_capabilities[i].name);
77         if (strstr(envstrupper, buf) != NULL) {
78             /* Match, set relevant bit in OPENSSL_riscvcap_P[] */
79             OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |= (1 << RISCV_capabilities[i].bit_offset);
80         }
81     }
82 }
83 
84 #ifdef OSSL_RISCV_HWPROBE
riscv_hwprobe(struct riscv_hwprobe * pairs,size_t pair_count,size_t cpu_count,unsigned long * cpus,unsigned int flags)85 static long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
86     size_t cpu_count, unsigned long *cpus,
87     unsigned int flags)
88 {
89     return syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_count, cpus, flags);
90 }
91 
hwprobe_to_cap(void)92 static void hwprobe_to_cap(void)
93 {
94     long ret;
95     struct riscv_hwprobe pairs[OSSL_RISCV_HWPROBE_PAIR_COUNT] = {
96         OSSL_RISCV_HWPROBE_PAIR_CONTENT
97     };
98 
99     ret = riscv_hwprobe(pairs, OSSL_RISCV_HWPROBE_PAIR_COUNT, 0, NULL, 0);
100     /* if hwprobe syscall does not exist, ret would be -ENOSYS */
101     if (ret == 0) {
102         for (size_t i = 0; i < kRISCVNumCaps; ++i) {
103             for (size_t j = 0; j != OSSL_RISCV_HWPROBE_PAIR_COUNT; ++j) {
104                 if (pairs[j].key == RISCV_capabilities[i].hwprobe_key
105                     && (pairs[j].value & RISCV_capabilities[i].hwprobe_value)
106                         != 0)
107                     if (!IS_IN_DEPEND_VECTOR(RISCV_capabilities[i].bit_offset) || VECTOR_CAPABLE)
108                         /* Match, set relevant bit in OPENSSL_riscvcap_P[] */
109                         OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |= (1 << RISCV_capabilities[i].bit_offset);
110             }
111         }
112     }
113 }
114 #endif /* OSSL_RISCV_HWPROBE */
115 
riscv_vlen(void)116 size_t riscv_vlen(void)
117 {
118     return vlen;
119 }
120 
121 #if defined(__GNUC__) && __GNUC__ >= 2
122 __attribute__((constructor))
123 #endif
OPENSSL_cpuid_setup(void)124 void OPENSSL_cpuid_setup(void)
125 {
126     char *e;
127     static int trigger = 0;
128 
129     if (trigger != 0)
130         return;
131     trigger = 1;
132 
133     if ((e = getenv("OPENSSL_riscvcap"))) {
134         parse_env(e);
135     }
136 #ifdef OSSL_RISCV_HWPROBE
137     else {
138         OPENSSL_riscv_hwcap_P = getauxval(AT_HWCAP);
139         hwprobe_to_cap();
140     }
141 #endif
142 
143     if (RISCV_HAS_V()) {
144         vlen = riscv_vlen_asm();
145     }
146 }
147