Path: blob/main/lib/libc/amd64/string/amd64_archlevel.c
39491 views
/*-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#include <sys/types.h>2930#include <machine/atomic.h>31#include <machine/cpufunc.h>32#include <machine/specialreg.h>3334#include <stddef.h>35#include <string.h>3637#include "amd64_archlevel.h"38#include "libc_private.h"3940#define ARCHLEVEL_ENV "ARCHLEVEL"4142static volatile int amd64_archlevel = X86_64_UNDEFINED;4344static const struct archlevel {45char name[10];46/* CPUID feature bits that need to be present */47u_int feat_edx, feat_ecx, amd_ecx, ext_ebx;48} levels[] = {49{50.name = "scalar",51.feat_edx = 0,52.feat_ecx = 0,53.amd_ecx = 0,54.ext_ebx = 0,55}, {56#define FEAT_EDX_BASELINE (CPUID_FPU | CPUID_CX8 | CPUID_CMOV | CPUID_MMX | \57CPUID_FXSR | CPUID_SSE | CPUID_SSE2)58.name = "baseline",59.feat_edx = FEAT_EDX_BASELINE,60.feat_ecx = 0,61.amd_ecx = 0,62.ext_ebx = 0,63}, {64#define FEAT_ECX_V2 (CPUID2_SSE3 | CPUID2_SSSE3 | CPUID2_CX16 | CPUID2_SSE41 | \65CPUID2_SSE42 | CPUID2_POPCNT)66#define AMD_ECX_V2 AMDID2_LAHF67.name = "x86-64-v2",68.feat_edx = FEAT_EDX_BASELINE,69.feat_ecx = FEAT_ECX_V2,70.amd_ecx = AMD_ECX_V2,71.ext_ebx = 0,72}, {73#define FEAT_ECX_V3 (FEAT_ECX_V2 | CPUID2_FMA | CPUID2_MOVBE | \74CPUID2_OSXSAVE | CPUID2_AVX | CPUID2_F16C)75#define AMD_ECX_V3 (AMD_ECX_V2 | AMDID2_ABM)76#define EXT_EBX_V3 (CPUID_STDEXT_BMI1 | CPUID_STDEXT_AVX2 | CPUID_STDEXT_BMI2)77.name = "x86-64-v3",78.feat_edx = FEAT_EDX_BASELINE,79.feat_ecx = FEAT_ECX_V3,80.amd_ecx = AMD_ECX_V3,81.ext_ebx = EXT_EBX_V3,82}, {83#define EXT_EBX_V4 (EXT_EBX_V3 | CPUID_STDEXT_AVX512F | \84CPUID_STDEXT_AVX512DQ | CPUID_STDEXT_AVX512CD | \85CPUID_STDEXT_AVX512BW | CPUID_STDEXT_AVX512VL)86.name = "x86-64-v4",87.feat_edx = FEAT_EDX_BASELINE,88.feat_ecx = FEAT_ECX_V3,89.amd_ecx = AMD_ECX_V3,90.ext_ebx = EXT_EBX_V4,91}92};9394static int95supported_archlevel(u_int feat_edx, u_int feat_ecx, u_int ext_ebx, u_int ext_ecx)96{97int level;98u_int p[4], max_leaf;99u_int amd_ecx = 0;100101(void)ext_ecx;102103do_cpuid(0x80000000, p);104max_leaf = p[0];105106if (max_leaf >= 0x80000001) {107do_cpuid(0x80000001, p);108amd_ecx = p[2];109}110111for (level = X86_64_BASELINE; level <= X86_64_MAX; level++) {112const struct archlevel *lvl = &levels[level];113114if ((lvl->feat_edx & feat_edx) != lvl->feat_edx ||115(lvl->feat_ecx & feat_ecx) != lvl->feat_ecx ||116(lvl->amd_ecx & amd_ecx) != lvl->amd_ecx ||117(lvl->ext_ebx & ext_ebx) != lvl->ext_ebx)118return (level - 1);119}120121return (X86_64_MAX);122}123124static int125match_archlevel(const char *str, int *force)126{127int level, want_force = 0;128129*force = 0;130131if (str[0] == '!') {132str++;133want_force = 1;134}135136for (level = 0; level <= X86_64_MAX; level++) {137size_t i;138const char *candidate = levels[level].name;139140/* can't use strcmp here: would recurse during ifunc resolution */141for (i = 0; str[i] == candidate[i]; i++)142/* suffixes starting with : or + are ignored for future extensions */143if (str[i] == '\0' || str[i] == ':' || str[i] == '+') {144if (want_force)145*force = 1;146147return (level);148}149}150151return (X86_64_UNDEFINED);152}153154/*155* We can't use getenv(), strcmp(), and a bunch of other functions here as156* they may in turn call SIMD-optimised string functions.157*158* *force is set to 1 if the architecture level is valid and begins with a !159* and to 0 otherwise.160*/161static int162env_archlevel(int *force)163{164size_t i;165166if (environ == NULL)167return (X86_64_UNDEFINED);168169for (i = 0; environ[i] != NULL; i++) {170size_t j;171172for (j = 0; environ[i][j] == ARCHLEVEL_ENV "="[j]; j++)173if (environ[i][j] == '=')174return (match_archlevel(&environ[i][j + 1], force));175}176177*force = 0;178179return (X86_64_UNDEFINED);180181}182183/*184* Determine the architecture level by checking the CPU capabilities185* and the environment:186*187* 1. If environment variable ARCHLEVEL starts with a ! and is followed188* by a valid architecture level, that level is returned.189* 2. Else if ARCHLEVEL is set to a valid architecture level that is190* supported by the CPU, that level is returned.191* 3. Else the highest architecture level supported by the CPU is192* returned.193*194* Valid architecture levels are those defined in the levels array.195* The architecture level "scalar" indicates that SIMD enhancements196* shall not be used.197*/198static int199archlevel(u_int feat_edx, u_int feat_ecx, u_int ext_ebx, u_int ext_ecx)200{201int islevel, wantlevel, hwlevel, force;202203islevel = atomic_load_int(&amd64_archlevel);204if (islevel != X86_64_UNDEFINED)205return (islevel);206207wantlevel = env_archlevel(&force);208if (!force) {209hwlevel = supported_archlevel(feat_edx, feat_ecx, ext_ebx, ext_ecx);210if (wantlevel == X86_64_UNDEFINED || wantlevel > hwlevel)211wantlevel = hwlevel;212}213214/*215* Ensure amd64_archlevel is set only once and216* all calls agree on what it was set to.217*/218if (atomic_cmpset_int(&amd64_archlevel, islevel, wantlevel))219return (wantlevel);220else221return (atomic_load_int(&amd64_archlevel));222}223224/*225* Helper function for SIMD ifunc dispatch: select the highest level226* implementation up to the current architecture level.227*/228dlfunc_t229__archlevel_resolve(u_int feat_edx, u_int feat_ecx, u_int ext_ebx,230u_int ext_ecx, int32_t funcs[static X86_64_MAX + 1])231{232int level;233234for (level = archlevel(feat_edx, feat_ecx, ext_ebx, ext_ecx); level >= 0; level--)235if (funcs[level] != 0)236return (dlfunc_t)((uintptr_t)funcs + (ptrdiff_t)funcs[level]);237238/* no function is present -- what now? */239__builtin_trap();240}241242243