Add LoongArch64 cpuid and OPENSSL_loongarchcap_P

Loongarch64 architecture defines 128 bit vector extension lsx and 256 bit
vector extension lasx. The cpucfg instruction can be used to obtain whether
the CPU has a corresponding extension. This part of code is added to prepare
for the subsequent addition of corresponding vector instruction optimization.

Signed-off-by: zhuchen <zhuchen@loongson.cn>

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19364)
This commit is contained in:
zhuchen 2022-09-29 19:50:52 +08:00 committed by Pauli
parent e1e93f7a07
commit 7f2d6188c7
5 changed files with 155 additions and 0 deletions

View File

@ -830,6 +830,7 @@ my %targets = (
"linux64-loongarch64" => {
inherit_from => [ "linux-generic64"],
perlasm_scheme => "linux64",
asm_arch => 'loongarch64',
},
#### IA-32 targets...

View File

@ -54,6 +54,7 @@ IF[{- !$disabled{asm} && $config{processor} ne '386' -}]
$CPUIDASM_riscv64=riscvcap.c riscv64cpuid.s
$CPUIDASM_riscv32=riscvcap.c riscv32cpuid.s
$CPUIDASM_loongarch64=loongarchcap.c loongarch64cpuid.s
# Now that we have defined all the arch specific variables, use the
# appropriate one, and define the appropriate macros
IF[$CPUIDASM_{- $target{asm_arch} -}]
@ -139,6 +140,7 @@ INCLUDE[s390xcpuid.o]=.
GENERATE[riscv64cpuid.s]=riscv64cpuid.pl
GENERATE[riscv32cpuid.s]=riscv32cpuid.pl
GENERATE[loongarch64cpuid.s]=loongarch64cpuid.pl
IF[{- $config{target} =~ /^(?:Cygwin|mingw|VC-|BC-)/ -}]
SHARED_SOURCE[../libcrypto]=dllmain.c
ENDIF

113
crypto/loongarch64cpuid.pl Normal file
View File

@ -0,0 +1,113 @@
#! /usr/bin/env perl
# Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
# $output is the last argument if it looks like a file (it has an extension)
# $flavour is the first argument if it doesn't look like a file
($zero,$ra,$tp,$sp)=map("\$r$_",(0..3));
($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$r$_",(4..11));
($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9)=map("\$r$_",(12..21));
($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$r$_",(23..30));
($vr0,$vr1,$vr2,$vr3,$vr4,$vr5,$vr6,$vr7,$vr8,$vr9,$vr10,$vr11,$vr12,$vr13,$vr14,$vr15,$vr16,$vr17,$vr18,$vr19)=map("\$vr$_",(0..19));
($fp)=map("\$r$_",(22));
for (@ARGV) { $output=$_ if (/\w[\w\-]*\.\w+$/); }
open STDOUT,">$output";
while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
open STDOUT,">$output";
{
my ($in_a,$in_b,$len,$m,$temp1,$temp2) = ($a0,$a1,$a2,$t0,$t1,$t2);
$code.=<<___;
################################################################################
# int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
################################################################################
.text
.balign 16
.globl CRYPTO_memcmp
.type CRYPTO_memcmp,\@function
CRYPTO_memcmp:
li.d $m,0
beqz $len,2f # len == 0
1:
ld.bu $temp1,$in_a,0
ld.bu $temp2,$in_b,0
addi.d $in_a,$in_a,1
addi.d $in_b,$in_b,1
addi.d $len,$len,-1
xor $temp1,$temp1,$temp2
or $m,$m,$temp1
blt $zero,$len,1b
2:
move $a0,$m
jr $ra
___
}
{
my ($ptr,$len,$temp1,$temp2) = ($a0,$a1,$t0,$t1);
$code.=<<___;
################################################################################
# void OPENSSL_cleanse(void *ptr, size_t len)
################################################################################
.text
.balign 16
.globl OPENSSL_cleanse
.type OPENSSL_cleanse,\@function
OPENSSL_cleanse:
beqz $len,2f # len == 0, return
srli.d $temp1,$len,4
bnez $temp1,3f # len > 15
1: # Store <= 15 individual bytes
st.b $zero,$ptr,0
addi.d $ptr,$ptr,1
addi.d $len,$len,-1
bnez $len,1b
2:
jr $ra
3: # Store individual bytes until we are aligned
andi $temp1,$ptr,0x7
beqz $temp1,4f
st.b $zero,$ptr,0
addi.d $ptr,$ptr,1
addi.d $len,$len,-1
b 3b
4: # Store aligned dwords
li.d $temp2,8
4:
st.d $zero,$ptr,0
addi.d $ptr,$ptr,8
addi.d $len,$len,-8
bge $len,$temp2,4b # if len>=8 loop
bnez $len,1b # if len<8 and len != 0, store remaining bytes
jr $ra
___
}
{
$code.=<<___;
################################################################################
# uint32_t OPENSSL_rdtsc(void)
################################################################################
.text
.balign 16
.globl OPENSSL_rdtsc
.type OPENSSL_rdtsc,\@function
OPENSSL_rdtsc:
move $a0,$zero
jr $ra
___
}
$code =~ s/\`([^\`]*)\`/eval($1)/gem;
print $code;
close STDOUT or die "error closing STDOUT: $!";

17
crypto/loongarch_arch.h Normal file
View File

@ -0,0 +1,17 @@
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#ifndef OSSL_CRYPTO_LOONGARCH_ARCH_H
# define OSSL_CRYPTO_LOONGARCH_ARCH_H
extern unsigned int OPENSSL_loongarchcap_P;
# define LOONGARCH_CFG2 0x02
# define LOONGARCH_CFG2_LSX (1<<6)
# define LOONGARCH_CFG2_LASX (1<<7)
#endif

22
crypto/loongarchcap.c Normal file
View File

@ -0,0 +1,22 @@
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "loongarch_arch.h"
unsigned int OPENSSL_loongarchcap_P = 0;
void OPENSSL_cpuid_setup(void)
{
unsigned int reg;
__asm__ volatile(
"cpucfg %0, %1 \n\t"
: "+&r"(reg)
: "r"(LOONGARCH_CFG2)
);
OPENSSL_loongarchcap_P = reg;
}