4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
27 #include <sys/zfs_context.h>
28 #include <sys/zfs_impl.h>
31 #include <sha2/sha2_impl.h>
32 #include <sys/asm_linkage.h>
35 extern void ASMABI E(uint32_t s[8], const void *, size_t); \
36 static inline void N(uint32_t s[8], const void *d, size_t b) { \
37 kfpu_begin(); E(s, d, b); kfpu_end(); \
40 /* some implementation is always okay */
41 static inline boolean_t
sha2_is_supported(void)
48 /* Users of ASMABI requires all calls to be from wrappers */
50 zfs_sha256_transform_x64(uint32_t s
[8], const void *, size_t);
53 tf_sha256_transform_x64(uint32_t s
[8], const void *d
, size_t b
)
55 zfs_sha256_transform_x64(s
, d
, b
);
58 const sha256_ops_t sha256_x64_impl
= {
59 .is_supported
= sha2_is_supported
,
60 .transform
= tf_sha256_transform_x64
,
64 #if defined(HAVE_SSSE3)
65 static boolean_t
sha2_have_ssse3(void)
67 return (kfpu_allowed() && zfs_ssse3_available());
70 TF(zfs_sha256_transform_ssse3
, tf_sha256_ssse3
);
71 const sha256_ops_t sha256_ssse3_impl
= {
72 .is_supported
= sha2_have_ssse3
,
73 .transform
= tf_sha256_ssse3
,
79 static boolean_t
sha2_have_avx(void)
81 return (kfpu_allowed() && zfs_avx_available());
84 TF(zfs_sha256_transform_avx
, tf_sha256_avx
);
85 const sha256_ops_t sha256_avx_impl
= {
86 .is_supported
= sha2_have_avx
,
87 .transform
= tf_sha256_avx
,
92 #if defined(HAVE_AVX2)
93 static boolean_t
sha2_have_avx2(void)
95 return (kfpu_allowed() && zfs_avx2_available());
98 TF(zfs_sha256_transform_avx2
, tf_sha256_avx2
);
99 const sha256_ops_t sha256_avx2_impl
= {
100 .is_supported
= sha2_have_avx2
,
101 .transform
= tf_sha256_avx2
,
106 #if defined(HAVE_SSE4_1)
107 static boolean_t
sha2_have_shani(void)
109 return (kfpu_allowed() && zfs_sse4_1_available() && \
110 zfs_shani_available());
113 TF(zfs_sha256_transform_shani
, tf_sha256_shani
);
114 const sha256_ops_t sha256_shani_impl
= {
115 .is_supported
= sha2_have_shani
,
116 .transform
= tf_sha256_shani
,
121 #elif defined(__aarch64__) || defined(__arm__)
122 extern void zfs_sha256_block_armv7(uint32_t s
[8], const void *, size_t);
123 const sha256_ops_t sha256_armv7_impl
= {
124 .is_supported
= sha2_is_supported
,
125 .transform
= zfs_sha256_block_armv7
,
130 static boolean_t
sha256_have_neon(void)
132 return (kfpu_allowed() && zfs_neon_available());
135 static boolean_t
sha256_have_armv8ce(void)
137 return (kfpu_allowed() && zfs_sha256_available());
140 TF(zfs_sha256_block_neon
, tf_sha256_neon
);
141 const sha256_ops_t sha256_neon_impl
= {
142 .is_supported
= sha256_have_neon
,
143 .transform
= tf_sha256_neon
,
147 TF(zfs_sha256_block_armv8
, tf_sha256_armv8ce
);
148 const sha256_ops_t sha256_armv8_impl
= {
149 .is_supported
= sha256_have_armv8ce
,
150 .transform
= tf_sha256_armv8ce
,
155 #elif defined(__PPC64__)
156 static boolean_t
sha256_have_isa207(void)
158 return (kfpu_allowed() && zfs_isa207_available());
161 TF(zfs_sha256_ppc
, tf_sha256_ppc
);
162 const sha256_ops_t sha256_ppc_impl
= {
163 .is_supported
= sha2_is_supported
,
164 .transform
= tf_sha256_ppc
,
168 TF(zfs_sha256_power8
, tf_sha256_power8
);
169 const sha256_ops_t sha256_power8_impl
= {
170 .is_supported
= sha256_have_isa207
,
171 .transform
= tf_sha256_power8
,
174 #endif /* __PPC64__ */
176 /* the two generic ones */
177 extern const sha256_ops_t sha256_generic_impl
;
179 /* array with all sha256 implementations */
180 static const sha256_ops_t
*const sha256_impls
[] = {
181 &sha256_generic_impl
,
182 #if defined(__x86_64)
185 #if defined(__x86_64) && defined(HAVE_SSSE3)
188 #if defined(__x86_64) && defined(HAVE_AVX)
191 #if defined(__x86_64) && defined(HAVE_AVX2)
194 #if defined(__x86_64) && defined(HAVE_SSE4_1)
197 #if defined(__aarch64__) || defined(__arm__)
204 #if defined(__PPC64__)
207 #endif /* __PPC64__ */
210 /* use the generic implementation functions */
211 #define IMPL_NAME "sha256"
212 #define IMPL_OPS_T sha256_ops_t
213 #define IMPL_ARRAY sha256_impls
214 #define IMPL_GET_OPS sha256_get_ops
215 #define ZFS_IMPL_OPS zfs_sha256_ops
216 #include <generic_impl.c>
220 #define IMPL_FMT(impl, i) (((impl) == (i)) ? "[%s] " : "%s ")
222 #if defined(__linux__)
225 sha256_param_get(char *buffer
, zfs_kernel_param_t
*unused
)
227 const uint32_t impl
= IMPL_READ(generic_impl_chosen
);
232 fmt
= IMPL_FMT(impl
, IMPL_CYCLE
);
233 cnt
+= sprintf(buffer
+ cnt
, fmt
, "cycle");
236 fmt
= IMPL_FMT(impl
, IMPL_FASTEST
);
237 cnt
+= sprintf(buffer
+ cnt
, fmt
, "fastest");
239 /* list all supported implementations */
241 for (uint32_t i
= 0; i
< generic_supp_impls_cnt
; ++i
) {
242 fmt
= IMPL_FMT(impl
, i
);
243 cnt
+= sprintf(buffer
+ cnt
, fmt
,
244 generic_supp_impls
[i
]->name
);
251 sha256_param_set(const char *val
, zfs_kernel_param_t
*unused
)
254 return (generic_impl_setname(val
));
257 #elif defined(__FreeBSD__)
259 #include <sys/sbuf.h>
262 sha256_param(ZFS_MODULE_PARAM_ARGS
)
267 if (req
->newptr
== NULL
) {
268 const uint32_t impl
= IMPL_READ(generic_impl_chosen
);
269 const int init_buflen
= 64;
273 s
= sbuf_new_for_sysctl(NULL
, NULL
, init_buflen
, req
);
276 fmt
= IMPL_FMT(impl
, IMPL_CYCLE
);
277 (void) sbuf_printf(s
, fmt
, "cycle");
280 fmt
= IMPL_FMT(impl
, IMPL_FASTEST
);
281 (void) sbuf_printf(s
, fmt
, "fastest");
283 /* list all supported implementations */
284 for (uint32_t i
= 0; i
< generic_supp_impls_cnt
; ++i
) {
285 fmt
= IMPL_FMT(impl
, i
);
286 (void) sbuf_printf(s
, fmt
, generic_supp_impls
[i
]->name
);
289 err
= sbuf_finish(s
);
297 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
302 return (-generic_impl_setname(buf
));
308 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs
, zfs_
, sha256_impl
,
309 sha256_param_set
, sha256_param_get
, ZMOD_RW
, \
310 "Select SHA256 implementation.");