Skip to content

Commit

Permalink
Add SHA2 SIMD feature tests for FreeBSD
Browse files Browse the repository at this point in the history
These are added:
- zfs_neon_available() for arm and aarch64
- zfs_sha256_available() for arm and aarch64
- zfs_sha512_available() for aarch64
- zfs_shani_available() for x86_64

Changes:
- simd_powerpc.h: change license from CDDL to BSD

Tested-by: Rich Ercolani <[email protected]>
Tested-by: Sebastian Gottschall <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Tino Reichardt <[email protected]>
Closes openzfs#13741
  • Loading branch information
mcmilk authored and sdimitro committed Mar 8, 2023
1 parent e67bdcc commit 0b58c9c
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 28 deletions.
2 changes: 2 additions & 0 deletions include/os/freebsd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ noinst_HEADERS = \
%D%/spl/sys/sid.h \
%D%/spl/sys/sig.h \
%D%/spl/sys/simd.h \
%D%/spl/sys/simd_aarch64.h \
%D%/spl/sys/simd_arm.h \
%D%/spl/sys/simd_powerpc.h \
%D%/spl/sys/simd_x86.h \
%D%/spl/sys/spl_condvar.h \
Expand Down
6 changes: 6 additions & 0 deletions include/os/freebsd/spl/sys/mod_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
#define blake3_param_set_args(var) \
CTLTYPE_STRING, NULL, 0, blake3_param, "A"

#define sha256_param_set_args(var) \
CTLTYPE_STRING, NULL, 0, sha256_param, "A"

#define sha512_param_set_args(var) \
CTLTYPE_STRING, NULL, 0, sha512_param, "A"

#include <sys/kernel.h>
#define module_init(fn) \
static void \
Expand Down
6 changes: 6 additions & 0 deletions include/os/freebsd/spl/sys/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#if defined(__amd64__) || defined(__i386__)
#include <sys/simd_x86.h>

#elif defined(__arm__)
#include <sys/simd_arm.h>

#elif defined(__aarch64__)
#include <sys/simd_aarch64.h>

#elif defined(__powerpc__)
#include <sys/simd_powerpc.h>

Expand Down
83 changes: 83 additions & 0 deletions include/os/freebsd/spl/sys/simd_aarch64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2022 Tino Reichardt <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

/*
* SIMD support:
*
* Following functions should be called to determine whether CPU feature
* is supported. All functions are usable in kernel and user space.
* If a SIMD algorithm is using more than one instruction set
* all relevant feature test functions should be called.
*
* Supported features:
* zfs_neon_available()
* zfs_sha256_available()
* zfs_sha512_available()
*/

#ifndef _FREEBSD_SIMD_AARCH64_H
#define _FREEBSD_SIMD_AARCH64_H

#include <sys/types.h>
#include <machine/elf.h>

#define kfpu_allowed() 1
#define kfpu_initialize(tsk) do {} while (0)
#define kfpu_begin() do {} while (0)
#define kfpu_end() do {} while (0)
#define kfpu_init() (0)
#define kfpu_fini() do {} while (0)

/*
* Check if NEON is available
*/
static inline boolean_t
zfs_neon_available(void)
{
return (elf_hwcap & HWCAP_FP);
}

/*
* Check if SHA256 is available
*/
static inline boolean_t
zfs_sha256_available(void)
{
return (elf_hwcap & HWCAP_SHA2);
}

/*
* Check if SHA512 is available
*/
static inline boolean_t
zfs_sha512_available(void)
{
return (elf_hwcap & HWCAP_SHA512);
}

#endif /* _FREEBSD_SIMD_AARCH64_H */
73 changes: 73 additions & 0 deletions include/os/freebsd/spl/sys/simd_arm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2022 Tino Reichardt <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

/*
* SIMD support:
*
* Following functions should be called to determine whether CPU feature
* is supported. All functions are usable in kernel and user space.
* If a SIMD algorithm is using more than one instruction set
* all relevant feature test functions should be called.
*
* Supported features:
* zfs_neon_available()
* zfs_sha256_available()
*/

#ifndef _FREEBSD_SIMD_ARM_H
#define _FREEBSD_SIMD_ARM_H

#include <sys/types.h>
#include <machine/elf.h>

#define kfpu_allowed() 1
#define kfpu_initialize(tsk) do {} while (0)
#define kfpu_begin() do {} while (0)
#define kfpu_end() do {} while (0)
#define kfpu_init() (0)
#define kfpu_fini() do {} while (0)

/*
* Check if NEON is available
*/
static inline boolean_t
zfs_neon_available(void)
{
return (elf_hwcap & HWCAP_NEON);
}

/*
* Check if SHA256 is available
*/
static inline boolean_t
zfs_sha256_available(void)
{
return (elf_hwcap2 & HWCAP2_SHA2);
}

#endif /* _FREEBSD_SIMD_ARM_H */
50 changes: 22 additions & 28 deletions include/os/freebsd/spl/sys/simd_powerpc.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
* Copyright (C) 2022 Tino Reichardt <[email protected]>
* All rights reserved.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* CDDL HEADER END
*/

/*
* Copyright (C) 2022 Tino Reichardt <[email protected]>
* $FreeBSD$
*/

/*
* USER API:
*
* Kernel fpu methods:
* kfpu_allowed()
* kfpu_begin()
* kfpu_end()
* kfpu_init()
* kfpu_fini()
*
* SIMD support:
*
* Following functions should be called to determine whether CPU feature
Expand Down
13 changes: 13 additions & 0 deletions include/os/freebsd/spl/sys/simd_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ zfs_avx2_available(void)
return (has_avx2 && __ymm_enabled());
}

/*
* Check if SHA_NI instruction set is available
*/
static inline boolean_t
zfs_shani_available(void)
{
boolean_t has_shani;

has_shani = (cpu_stdext_feature & CPUID_STDEXT_SHA) != 0;

return (has_shani && __ymm_enabled());
}

/*
* AVX-512 family of instruction sets:
*
Expand Down

0 comments on commit 0b58c9c

Please sign in to comment.