riscv64cpuid.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #! /usr/bin/env perl
  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. # $output is the last argument if it looks like a file (it has an extension)
  9. # $flavour is the first argument if it doesn't look like a file
  10. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  11. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  12. $output and open STDOUT,">$output";
  13. {
  14. my ($in_a,$in_b,$len,$x,$temp1,$temp2) = ('a0','a1','a2','t0','t1','t2');
  15. $code.=<<___;
  16. ################################################################################
  17. # int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
  18. ################################################################################
  19. .text
  20. .balign 16
  21. .globl CRYPTO_memcmp
  22. .type CRYPTO_memcmp,\@function
  23. CRYPTO_memcmp:
  24. li $x,0
  25. beqz $len,2f # len == 0
  26. 1:
  27. lbu $temp1,0($in_a)
  28. lbu $temp2,0($in_b)
  29. addi $in_a,$in_a,1
  30. addi $in_b,$in_b,1
  31. addi $len,$len,-1
  32. xor $temp1,$temp1,$temp2
  33. or $x,$x,$temp1
  34. bgtz $len,1b
  35. 2:
  36. mv a0,$x
  37. ret
  38. ___
  39. }
  40. {
  41. my ($ptr,$len,$temp1,$temp2) = ('a0','a1','t0','t1');
  42. $code.=<<___;
  43. ################################################################################
  44. # void OPENSSL_cleanse(void *ptr, size_t len)
  45. ################################################################################
  46. .text
  47. .balign 16
  48. .globl OPENSSL_cleanse
  49. .type OPENSSL_cleanse,\@function
  50. OPENSSL_cleanse:
  51. beqz $len,2f # len == 0, return
  52. srli $temp1,$len,4
  53. bnez $temp1,3f # len > 15
  54. 1: # Store <= 15 individual bytes
  55. sb x0,0($ptr)
  56. addi $ptr,$ptr,1
  57. addi $len,$len,-1
  58. bnez $len,1b
  59. 2:
  60. ret
  61. 3: # Store individual bytes until we are aligned
  62. andi $temp1,$ptr,0x7
  63. beqz $temp1,4f
  64. sb x0,0($ptr)
  65. addi $ptr,$ptr,1
  66. addi $len,$len,-1
  67. j 3b
  68. 4: # Store aligned dwords
  69. li $temp2,8
  70. 4:
  71. sd x0,0($ptr)
  72. addi $ptr,$ptr,8
  73. addi $len,$len,-8
  74. bge $len,$temp2,4b # if len>=8 loop
  75. bnez $len,1b # if len<8 and len != 0, store remaining bytes
  76. ret
  77. ___
  78. }
  79. {
  80. my ($ret) = ('a0');
  81. $code .= <<___;
  82. ################################################################################
  83. # size_t riscv_vlen_asm(void)
  84. # Return VLEN (i.e. the length of a vector register in bits).
  85. .p2align 3
  86. .globl riscv_vlen_asm
  87. .type riscv_vlen_asm,\@function
  88. riscv_vlen_asm:
  89. csrr $ret, vlenb
  90. slli $ret, $ret, 3
  91. ret
  92. .size riscv_vlen_asm,.-riscv_vlen_asm
  93. ___
  94. }
  95. print $code;
  96. close STDOUT or die "error closing STDOUT: $!";