2
0

riscvcap.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <stdint.h>
  13. #include <openssl/crypto.h>
  14. #include "internal/cryptlib.h"
  15. #define OPENSSL_RISCVCAP_IMPL
  16. #include "crypto/riscv_arch.h"
  17. static void parse_env(const char *envstr);
  18. static void strtoupper(char *str);
  19. uint32_t OPENSSL_rdtsc(void)
  20. {
  21. return 0;
  22. }
  23. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  24. {
  25. return 0;
  26. }
  27. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  28. {
  29. return 0;
  30. }
  31. static void strtoupper(char *str)
  32. {
  33. for (char *x = str; *x; ++x)
  34. *x = toupper(*x);
  35. }
  36. /* parse_env() parses a RISC-V architecture string. An example of such a string
  37. * is "rv64gc_zba_zbb_zbc_zbs". Currently, the rv64gc part is ignored
  38. * and we simply search for "_[extension]" in the arch string to see if we
  39. * should enable a given extension.
  40. */
  41. #define BUFLEN 256
  42. static void parse_env(const char *envstr)
  43. {
  44. char envstrupper[BUFLEN];
  45. char buf[BUFLEN];
  46. /* Convert env str to all uppercase */
  47. OPENSSL_strlcpy(envstrupper, envstr, sizeof(envstrupper));
  48. strtoupper(envstrupper);
  49. for (size_t i = 0; i < kRISCVNumCaps; ++i) {
  50. /* Prefix capability with underscore in preparation for search */
  51. BIO_snprintf(buf, BUFLEN, "_%s", RISCV_capabilities[i].name);
  52. if (strstr(envstrupper, buf) != NULL) {
  53. /* Match, set relevant bit in OPENSSL_riscvcap_P[] */
  54. OPENSSL_riscvcap_P[RISCV_capabilities[i].index] |=
  55. (1 << RISCV_capabilities[i].bit_offset);
  56. }
  57. }
  58. }
  59. # if defined(__GNUC__) && __GNUC__>=2
  60. __attribute__ ((constructor))
  61. # endif
  62. void OPENSSL_cpuid_setup(void)
  63. {
  64. char *e;
  65. static int trigger = 0;
  66. if (trigger != 0)
  67. return;
  68. trigger = 1;
  69. if ((e = getenv("OPENSSL_riscvcap"))) {
  70. parse_env(e);
  71. return;
  72. }
  73. }