/*-1* Copyright (c) 2023 The FreeBSD Foundation2*3* This software was developed by Robert Clausecker <[email protected]>4* under sponsorship from the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE26*/2728/* must be macros so they can be accessed from assembly */29#define X86_64_SCALAR 0 /* disable SIMD optimisations */30#define X86_64_BASELINE 1 /* CMOV, CX8, FPU, FXSR, MMX, OSFXSR, SSE, SSE2 */31#define X86_64_V2 2 /* CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSSE3, SSE4_1, SSE4_2 */32#define X86_64_V3 3 /* AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, OSXSAVE */33#define X86_64_V4 4 /* AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL */3435#define X86_64_MAX X86_64_V4 /* highest supported architecture level */36#define X86_64_UNDEFINED -1 /* architecture level not set yet */3738#ifndef __ASSEMBLER__39#include <dlfcn.h>4041dlfunc_t __archlevel_resolve(u_int, u_int, u_int, u_int,42int32_t[X86_64_MAX + 1]) __hidden;43#else44#include <machine/asm.h>4546#define ARCHRESOLVE(func) \47.globl CNAME(func); \48.type CNAME(func), @gnu_indirect_function; \49.set CNAME(func), __CONCAT(func,_resolver); \50ARCHENTRY(func, resolver); \51lea __CONCAT(func,_funcs)(%rip), %r8; \52jmp CNAME(__archlevel_resolve); \53ARCHEND(func, resolver)5455/*56* The func_funcs array stores the location of the implementations57* as the distance from the func_funcs array to the function. Due58* to compiling for the medium code model, a 32 bit integer suffices59* to hold the distance.60*61* Doing it this way both saves storage and avoids giving rtld62* relocations to process at load time.63*/64#define ARCHFUNCS(func) \65ARCHRESOLVE(func); \66.section .rodata; \67.align 4; \68__CONCAT(func,_funcs):6970#define NOARCHFUNC \71.4byte 07273#define ARCHFUNC(func, level) \74.4byte __CONCAT(__CONCAT(func,_),level) - __CONCAT(func,_funcs)7576#define ENDARCHFUNCS(func) \77.zero 4*(X86_64_MAX+1)-(.-__CONCAT(func,_funcs)); \78.size __CONCAT(func,_funcs), .-__CONCAT(func,_funcs)7980#define ARCHENTRY(func, level) \81_START_ENTRY; \82.type __CONCAT(__CONCAT(func,_),level), @function; \83__CONCAT(__CONCAT(func,_),level):; \84.cfi_startproc8586#define ARCHEND(func, level) \87END(__CONCAT(__CONCAT(func,_),level))8889#endif /* __ASSEMBLER__ */909192