Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / crypto / riscvcap.c
1 /*
2  * Copyright 2022 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 static void parse_env(const char *envstr);
21 static void strtoupper(char *str);
22
23 uint32_t OPENSSL_rdtsc(void)
24 {
25     return 0;
26 }
27
28 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
29 {
30     return 0;
31 }
32
33 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
34 {
35     return 0;
36 }
37
38 static void strtoupper(char *str)
39 {
40     for (char *x = str; *x; ++x)
41         *x = toupper(*x);
42 }
43
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.
48  */
49 #define BUFLEN 256
50 static void parse_env(const char *envstr)
51 {
52     char envstrupper[BUFLEN];
53     char buf[BUFLEN];
54
55     /* Convert env str to all uppercase */
56     OPENSSL_strlcpy(envstrupper, envstr, sizeof(envstrupper));
57     strtoupper(envstrupper);
58
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);
66         }
67     }
68 }
69
70 # if defined(__GNUC__) && __GNUC__>=2
71 __attribute__ ((constructor))
72 # endif
73 void OPENSSL_cpuid_setup(void)
74 {
75     char *e;
76     static int trigger = 0;
77
78     if (trigger != 0)
79         return;
80     trigger = 1;
81
82     if ((e = getenv("OPENSSL_riscvcap"))) {
83         parse_env(e);
84         return;
85     }
86 }