xref: /freebsd/crypto/openssl/crypto/riscvcap.c (revision e7be843b4a162e68651d3911f0357ed464915629)
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] |=
80                 (1 << RISCV_capabilities[i].bit_offset);
81         }
82     }
83 }
84 
85 #ifdef OSSL_RISCV_HWPROBE
riscv_hwprobe(struct riscv_hwprobe * pairs,size_t pair_count,size_t cpu_count,unsigned long * cpus,unsigned int flags)86 static long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
87                           size_t cpu_count, unsigned long *cpus,
88                           unsigned int flags)
89 {
90     return syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_count, cpus, flags);
91 }
92 
hwprobe_to_cap(void)93 static void hwprobe_to_cap(void)
94 {
95     long ret;
96     struct riscv_hwprobe pairs[OSSL_RISCV_HWPROBE_PAIR_COUNT] = {
97         OSSL_RISCV_HWPROBE_PAIR_CONTENT
98     };
99 
100     ret = riscv_hwprobe(pairs, OSSL_RISCV_HWPROBE_PAIR_COUNT, 0, NULL, 0);
101     /* if hwprobe syscall does not exist, ret would be -ENOSYS */
102     if (ret == 0) {
103         for (size_t i = 0; i < kRISCVNumCaps; ++i) {
104             for (size_t j = 0; j != OSSL_RISCV_HWPROBE_PAIR_COUNT; ++j) {
105                 if (pairs[j].key == RISCV_capabilities[i].hwprobe_key
106                         && (pairs[j].value & RISCV_capabilities[i].hwprobe_value)
107                            != 0)
108                     if (!IS_IN_DEPEND_VECTOR(RISCV_capabilities[i].bit_offset) || VECTOR_CAPABLE)
109                         /* Match, set relevant bit in OPENSSL_riscvcap_P[] */
110                         OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |=
111                             (1 << RISCV_capabilities[i].bit_offset);
112             }
113         }
114     }
115 }
116 #endif /* OSSL_RISCV_HWPROBE */
117 
riscv_vlen(void)118 size_t riscv_vlen(void)
119 {
120     return vlen;
121 }
122 
123 # if defined(__GNUC__) && __GNUC__>=2
124 __attribute__ ((constructor))
125 # endif
OPENSSL_cpuid_setup(void)126 void OPENSSL_cpuid_setup(void)
127 {
128     char *e;
129     static int trigger = 0;
130 
131     if (trigger != 0)
132         return;
133     trigger = 1;
134 
135     if ((e = getenv("OPENSSL_riscvcap"))) {
136         parse_env(e);
137     }
138 #ifdef OSSL_RISCV_HWPROBE
139     else {
140         OPENSSL_riscv_hwcap_P = getauxval(AT_HWCAP);
141         hwprobe_to_cap();
142     }
143 #endif
144 
145     if (RISCV_HAS_V()) {
146         vlen = riscv_vlen_asm();
147     }
148 }
149