Skip to content

Commit

Permalink
Upstream: SHA2 reworking and API for iterating over multiple implemen…
Browse files Browse the repository at this point in the history
…tations

The changes in the shared files to enable macOS support to PR
  • Loading branch information
lundman committed Mar 20, 2023
1 parent 2f12e62 commit 256294a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 57 deletions.
21 changes: 21 additions & 0 deletions lib/libspl/include/sys/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ static inline unsigned long getauxval(unsigned long key)
#define AT_HWCAP 16
#define AT_HWCAP2 26
extern unsigned long getauxval(unsigned long type);
#elif defined(__APPLE__)
#include <sys/sysctl.h>
#define AT_HWCAP 0
static inline unsigned long getauxval(unsigned long key)
{
(void) key;
/* HWCAP_ are all defined halfway down this file */
unsigned long val = 1 /* HWCAP_FP */;
int intval;
size_t intvallen = sizeof (intval);
int err;
err = sysctlbyname("hw.optional.arm.FEAT_SHA256",
&intval, &intvallen, NULL, 0);
if (err == 0 && intval != 0)
val |= 0x00000040; /* SHA256 */
err = sysctlbyname("hw.optional.arm.FEAT_SHA512",
&intval, &intvallen, NULL, 0);
if (err == 0 && intval != 0)
val |= 0x00200000; /* SHA512 */
return (val);
}
#endif /* __linux__ */
#endif /* arm || aarch64 || powerpc */

Expand Down
7 changes: 6 additions & 1 deletion module/icp/algs/sha2/sha256_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ sha256_param_set(const char *val, zfs_kernel_param_t *unused)
return (generic_impl_setname(val));
}

#elif defined(__FreeBSD__)
#elif defined(__FreeBSD__) || defined(__APPLE__)

#include <sys/sbuf.h>

Expand Down Expand Up @@ -272,7 +272,12 @@ sha256_param(ZFS_MODULE_PARAM_ARGS)
(void) sbuf_printf(s, fmt, generic_supp_impls[i]->name);
}

#ifdef __APPLE__
err = SYSCTL_OUT(req, s->s_buf, s->s_len);
sbuf_finish(s);
#else
err = sbuf_finish(s);
#endif
sbuf_delete(s);

return (err);
Expand Down
7 changes: 6 additions & 1 deletion module/icp/algs/sha2/sha512_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ sha512_param_set(const char *val, zfs_kernel_param_t *unused)
return (generic_impl_setname(val));
}

#elif defined(__FreeBSD__)
#elif defined(__FreeBSD__) || defined(__APPLE__)

#include <sys/sbuf.h>

Expand Down Expand Up @@ -248,7 +248,12 @@ sha512_param(ZFS_MODULE_PARAM_ARGS)
(void) sbuf_printf(s, fmt, generic_supp_impls[i]->name);
}

#ifdef __APPLE__
err = SYSCTL_OUT(req, s->s_buf, s->s_len);
sbuf_finish(s);
#else
err = sbuf_finish(s);
#endif
sbuf_delete(s);

return (err);
Expand Down
31 changes: 12 additions & 19 deletions module/icp/asm-aarch64/sha2/sha256-armv8.S
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
* - modified assembly to fit into OpenZFS
*/

#include <sys/asm_linkage.h>

#if defined(__aarch64__)

.text
SECTION_TEXT

.align 6
.type .LK256,%object
.balign 64
SET_OBJ(.LK256)
.LK256:
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
.long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
Expand All @@ -43,12 +45,9 @@
.long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
.long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
.long 0 //terminator
.size .LK256,.-.LK256
SET_SIZE(.LK256)

.globl zfs_sha256_block_armv7
.type zfs_sha256_block_armv7,%function
.align 6
zfs_sha256_block_armv7:
ENTRY_ALIGN(zfs_sha256_block_armv7, 64)
stp x29,x30,[sp,#-128]!
add x29,sp,#0

Expand Down Expand Up @@ -1009,12 +1008,9 @@ zfs_sha256_block_armv7:
ldp x27,x28,[x29,#80]
ldp x29,x30,[sp],#128
ret
.size zfs_sha256_block_armv7,.-zfs_sha256_block_armv7
SET_SIZE(zfs_sha256_block_armv7)

.globl zfs_sha256_block_armv8
.type zfs_sha256_block_armv8,%function
.align 6
zfs_sha256_block_armv8:
ENTRY_ALIGN(zfs_sha256_block_armv8, 64)
.Lv8_entry:
stp x29,x30,[sp,#-16]!
add x29,sp,#0
Expand Down Expand Up @@ -1149,12 +1145,9 @@ zfs_sha256_block_armv8:

ldr x29,[sp],#16
ret
.size zfs_sha256_block_armv8,.-zfs_sha256_block_armv8
SET_SIZE(zfs_sha256_block_armv8)

.globl zfs_sha256_block_neon
.type zfs_sha256_block_neon,%function
.align 4
zfs_sha256_block_neon:
ENTRY_ALIGN(zfs_sha256_block_neon, 16)
.Lneon_entry:
stp x29, x30, [sp, #-16]!
mov x29, sp
Expand Down Expand Up @@ -1994,6 +1987,6 @@ zfs_sha256_block_neon:
ldr x29,[x29]
add sp,sp,#16*4+16
ret
.size zfs_sha256_block_neon,.-zfs_sha256_block_neon
SET_SIZE(zfs_sha256_block_neon)

#endif
25 changes: 10 additions & 15 deletions module/icp/asm-aarch64/sha2/sha512-armv8.S
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
* Portions Copyright (c) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
* - modified assembly to fit into OpenZFS
*/
#include <sys/asm_linkage.h>

#if defined(__aarch64__)

.text
SECTION_TEXT

.align 6
.type .LK512,%object
.balign 64
SET_OBJ(.LK512)
.LK512:
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
Expand Down Expand Up @@ -67,12 +68,9 @@
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
.quad 0 // terminator
.size .LK512,.-.LK512
SET_SIZE(.LK512)

.globl zfs_sha512_block_armv7
.type zfs_sha512_block_armv7,%function
.align 6
zfs_sha512_block_armv7:
ENTRY_ALIGN(zfs_sha512_block_armv7, 64)
stp x29,x30,[sp,#-128]!
add x29,sp,#0

Expand Down Expand Up @@ -1033,13 +1031,10 @@ zfs_sha512_block_armv7:
ldp x27,x28,[x29,#80]
ldp x29,x30,[sp],#128
ret
.size zfs_sha512_block_armv7,.-zfs_sha512_block_armv7
SET_SIZE(zfs_sha512_block_armv7)


.globl zfs_sha512_block_armv8
.type zfs_sha512_block_armv8,%function
.align 6
zfs_sha512_block_armv8:
ENTRY_ALIGN(zfs_sha512_block_armv8, 64)
.Lv8_entry:
// Armv8.3-A PAuth: even though x30 is pushed to stack it is not popped later
stp x29,x30,[sp,#-16]!
Expand All @@ -1061,7 +1056,7 @@ zfs_sha512_block_armv8:
rev64 v23.16b,v23.16b
b .Loop_hw

.align 4
.balign 16
.Loop_hw:
ld1 {v24.2d},[x3],#16
subs x2,x2,#1
Expand Down Expand Up @@ -1554,5 +1549,5 @@ zfs_sha512_block_armv8:

ldr x29,[sp],#16
ret
.size zfs_sha512_block_armv8,.-zfs_sha512_block_armv8
SET_SIZE(zfs_sha512_block_armv8)
#endif
24 changes: 12 additions & 12 deletions module/icp/asm-x86_64/sha2/sha256-x86_64.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

SECTION_STATIC

.align 64
.type K256,@object
.balign 64
SET_OBJ(K256)
K256:
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
Expand Down Expand Up @@ -105,7 +105,7 @@ ENTRY_ALIGN(zfs_sha256_transform_x64, 16)
movl 24(%rdi),%r10d
movl 28(%rdi),%r11d
jmp .Lloop
.align 16
.balign 16
.Lloop:
movl %ebx,%edi
leaq K256(%rip),%rbp
Expand Down Expand Up @@ -622,7 +622,7 @@ ENTRY_ALIGN(zfs_sha256_transform_x64, 16)
addl %r12d,%eax
leaq 20(%rbp),%rbp
jmp .Lrounds_16_xx
.align 16
.balign 16
.Lrounds_16_xx:
movl 4(%rsp),%r13d
movl 56(%rsp),%r15d
Expand Down Expand Up @@ -1436,7 +1436,7 @@ ENTRY_ALIGN(zfs_sha256_transform_shani, 64)
punpcklqdq %xmm0,%xmm2
jmp .Loop_shani

.align 16
.balign 16
.Loop_shani:
movdqu (%rsi),%xmm3
movdqu 16(%rsi),%xmm4
Expand Down Expand Up @@ -1666,7 +1666,7 @@ ENTRY_ALIGN(zfs_sha256_transform_ssse3, 64)
movl 28(%rdi),%r11d

jmp .Lloop_ssse3
.align 16
.balign 16
.Lloop_ssse3:
movdqa K256+512(%rip),%xmm7
movdqu 0(%rsi),%xmm0
Expand Down Expand Up @@ -1696,7 +1696,7 @@ ENTRY_ALIGN(zfs_sha256_transform_ssse3, 64)
movl %r8d,%r13d
jmp .Lssse3_00_47

.align 16
.balign 16
.Lssse3_00_47:
subq $-128,%rbp
rorl $14,%r13d
Expand Down Expand Up @@ -2779,7 +2779,7 @@ ENTRY_ALIGN(zfs_sha256_transform_avx, 64)
vmovdqa K256+512+32(%rip),%xmm8
vmovdqa K256+512+64(%rip),%xmm9
jmp .Lloop_avx
.align 16
.balign 16
.Lloop_avx:
vmovdqa K256+512(%rip),%xmm7
vmovdqu 0(%rsi),%xmm0
Expand All @@ -2805,7 +2805,7 @@ ENTRY_ALIGN(zfs_sha256_transform_avx, 64)
movl %r8d,%r13d
jmp .Lavx_00_47

.align 16
.balign 16
.Lavx_00_47:
subq $-128,%rbp
vpalignr $4,%xmm0,%xmm1,%xmm4
Expand Down Expand Up @@ -3858,7 +3858,7 @@ ENTRY_ALIGN(zfs_sha256_transform_avx2, 64)
vmovdqa K256+512+32(%rip),%ymm8
vmovdqa K256+512+64(%rip),%ymm9
jmp .Loop_avx2
.align 16
.balign 16
.Loop_avx2:
vmovdqa K256+512(%rip),%ymm7
vmovdqu -64+0(%rsi),%xmm0
Expand Down Expand Up @@ -3900,7 +3900,7 @@ ENTRY_ALIGN(zfs_sha256_transform_avx2, 64)
subq $-32*4,%rbp
jmp .Lavx2_00_47

.align 16
.balign 16
.Lavx2_00_47:
leaq -64(%rsp),%rsp
.cfi_escape 0x0f,0x05,0x77,0x38,0x06,0x23,0x08
Expand Down Expand Up @@ -4842,7 +4842,7 @@ ENTRY_ALIGN(zfs_sha256_transform_avx2, 64)
xorl %ecx,%edi
movl %r9d,%r12d
jmp .Lower_avx2
.align 16
.balign 16
.Lower_avx2:
addl 0+16(%rbp),%r11d
andl %r8d,%r12d
Expand Down
18 changes: 9 additions & 9 deletions module/icp/asm-x86_64/sha2/sha512-x86_64.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

SECTION_STATIC

.align 64
.type K512,@object
.balign 64
SET_OBJ(K512)
K512:
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
Expand Down Expand Up @@ -148,7 +148,7 @@ ENTRY_ALIGN(zfs_sha512_transform_x64, 16)
movq 48(%rdi),%r10
movq 56(%rdi),%r11
jmp .Lloop
.align 16
.balign 16
.Lloop:
movq %rbx,%rdi
leaq K512(%rip),%rbp
Expand Down Expand Up @@ -665,7 +665,7 @@ ENTRY_ALIGN(zfs_sha512_transform_x64, 16)
addq %r12,%rax
leaq 24(%rbp),%rbp
jmp .Lrounds_16_xx
.align 16
.balign 16
.Lrounds_16_xx:
movq 8(%rsp),%r13
movq 112(%rsp),%r15
Expand Down Expand Up @@ -1501,7 +1501,7 @@ ENTRY_ALIGN(zfs_sha512_transform_avx, 64)
movq 48(%rdi),%r10
movq 56(%rdi),%r11
jmp .Lloop_avx
.align 16
.balign 16
.Lloop_avx:
vmovdqa K512+1280(%rip),%xmm11
vmovdqu 0(%rsi),%xmm0
Expand Down Expand Up @@ -1543,7 +1543,7 @@ ENTRY_ALIGN(zfs_sha512_transform_avx, 64)
movq %r8,%r13
jmp .Lavx_00_47

.align 16
.balign 16
.Lavx_00_47:
addq $256,%rbp
vpalignr $8,%xmm0,%xmm1,%xmm8
Expand Down Expand Up @@ -2670,7 +2670,7 @@ ENTRY_ALIGN(zfs_sha512_transform_avx2, 64)
movq 48(%rdi),%r10
movq 56(%rdi),%r11
jmp .Loop_avx2
.align 16
.balign 16
.Loop_avx2:
vmovdqu -128(%rsi),%xmm0
vmovdqu -128+16(%rsi),%xmm1
Expand Down Expand Up @@ -2732,7 +2732,7 @@ ENTRY_ALIGN(zfs_sha512_transform_avx2, 64)
addq $32*8,%rbp
jmp .Lavx2_00_47

.align 16
.balign 16
.Lavx2_00_47:
leaq -128(%rsp),%rsp
.cfi_escape 0x0f,0x06,0x77,0xf8,0x00,0x06,0x23,0x08
Expand Down Expand Up @@ -3750,7 +3750,7 @@ ENTRY_ALIGN(zfs_sha512_transform_avx2, 64)
xorq %rcx,%rdi
movq %r9,%r12
jmp .Lower_avx2
.align 16
.balign 16
.Lower_avx2:
addq 0+16(%rbp),%r11
andq %r8,%r12
Expand Down

0 comments on commit 256294a

Please sign in to comment.