2
0

riscv32cpuid.pl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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,0x3
  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 words
  69. li $temp2,4
  70. 4:
  71. sw x0,0($ptr)
  72. addi $ptr,$ptr,4
  73. addi $len,$len,-4
  74. bge $len,$temp2,4b # if len>=4 loop
  75. bnez $len,1b # if len<4 and len != 0, store remaining bytes
  76. ret
  77. ___
  78. }
  79. print $code;
  80. close STDOUT or die "error closing STDOUT: $!";