LCOV - code coverage report
Current view: top level - lib/eal/x86 - rte_cpuflags.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 25 35 71.4 %
Date: 2026-11-01 19:11:22 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 22 54.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2015 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <eal_export.h>
       6                 :            : #include "rte_cpuflags.h"
       7                 :            : 
       8                 :            : #include <stdio.h>
       9                 :            : #include <errno.h>
      10                 :            : #include <stdint.h>
      11                 :            : #include <string.h>
      12                 :            : #include <stdbool.h>
      13                 :            : 
      14                 :            : #include "rte_cpuid.h"
      15                 :            : #include "rte_atomic.h"
      16                 :            : 
      17                 :            : #ifdef RTE_TOOLCHAIN_MSVC
      18                 :            : #include <immintrin.h>
      19                 :            : #endif
      20                 :            : 
      21                 :            : /*
      22                 :            :  * XCR0 state components that the OS must enable before
      23                 :            :  * the related instructions can execute without faulting.
      24                 :            :  */
      25                 :            : #define XSTATE_SSE      (UINT64_C(1) << 1)
      26                 :            : #define XSTATE_YMM      (UINT64_C(1) << 2)
      27                 :            : #define XSTATE_OPMASK   (UINT64_C(1) << 5)
      28                 :            : #define XSTATE_ZMM_H256 (UINT64_C(1) << 6)
      29                 :            : #define XSTATE_HI16_ZMM (UINT64_C(1) << 7)
      30                 :            : 
      31                 :            : #define XSTATE_AVX    (XSTATE_SSE | XSTATE_YMM)
      32                 :            : #define XSTATE_AVX512 (XSTATE_AVX | XSTATE_OPMASK | XSTATE_ZMM_H256 | XSTATE_HI16_ZMM)
      33                 :            : 
      34                 :            : /**
      35                 :            :  * Struct to hold a processor feature entry
      36                 :            :  */
      37                 :            : struct feature_entry {
      38                 :            :         uint32_t leaf;                          /**< cpuid leaf */
      39                 :            :         uint32_t subleaf;                       /**< cpuid subleaf */
      40                 :            :         uint32_t reg;                           /**< cpuid register */
      41                 :            :         uint32_t bit;                           /**< cpuid register bit */
      42                 :            : #define CPU_FLAG_NAME_MAX_LEN 64
      43                 :            :         char name[CPU_FLAG_NAME_MAX_LEN];       /**< String for printing */
      44                 :            :         bool has_value;
      45                 :            :         bool value;
      46                 :            :         uint64_t xstate;                        /**< XCR0 bits the OS must enable */
      47                 :            : };
      48                 :            : 
      49                 :            : #define FEAT_DEF(name, leaf, subleaf, reg, bit) \
      50                 :            :         [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name },
      51                 :            : 
      52                 :            : /*
      53                 :            :  * A VEX or EVEX encoded feature also needs OS support for its register state.
      54                 :            :  * Use FEAT_DEF_XSTATE for such a feature, with the XCR0 bits that it needs.
      55                 :            :  */
      56                 :            : #define FEAT_DEF_XSTATE(name, leaf, subleaf, reg, bit, xs) \
      57                 :            :         [RTE_CPUFLAG_##name] = {leaf, subleaf, reg, bit, #name, .xstate = xs },
      58                 :            : 
      59                 :            : struct feature_entry rte_cpu_feature_table[] = {
      60                 :            :         FEAT_DEF(SSE3, 0x00000001, 0, RTE_REG_ECX,  0)
      61                 :            :         FEAT_DEF(PCLMULQDQ, 0x00000001, 0, RTE_REG_ECX,  1)
      62                 :            :         FEAT_DEF(DTES64, 0x00000001, 0, RTE_REG_ECX,  2)
      63                 :            :         FEAT_DEF(MONITOR, 0x00000001, 0, RTE_REG_ECX,  3)
      64                 :            :         FEAT_DEF(DS_CPL, 0x00000001, 0, RTE_REG_ECX,  4)
      65                 :            :         FEAT_DEF(VMX, 0x00000001, 0, RTE_REG_ECX,  5)
      66                 :            :         FEAT_DEF(SMX, 0x00000001, 0, RTE_REG_ECX,  6)
      67                 :            :         FEAT_DEF(EIST, 0x00000001, 0, RTE_REG_ECX,  7)
      68                 :            :         FEAT_DEF(TM2, 0x00000001, 0, RTE_REG_ECX,  8)
      69                 :            :         FEAT_DEF(SSSE3, 0x00000001, 0, RTE_REG_ECX,  9)
      70                 :            :         FEAT_DEF(CNXT_ID, 0x00000001, 0, RTE_REG_ECX, 10)
      71                 :            :         FEAT_DEF_XSTATE(FMA, 0x00000001, 0, RTE_REG_ECX, 12, XSTATE_AVX)
      72                 :            :         FEAT_DEF(CMPXCHG16B, 0x00000001, 0, RTE_REG_ECX, 13)
      73                 :            :         FEAT_DEF(XTPR, 0x00000001, 0, RTE_REG_ECX, 14)
      74                 :            :         FEAT_DEF(PDCM, 0x00000001, 0, RTE_REG_ECX, 15)
      75                 :            :         FEAT_DEF(PCID, 0x00000001, 0, RTE_REG_ECX, 17)
      76                 :            :         FEAT_DEF(DCA, 0x00000001, 0, RTE_REG_ECX, 18)
      77                 :            :         FEAT_DEF(SSE4_1, 0x00000001, 0, RTE_REG_ECX, 19)
      78                 :            :         FEAT_DEF(SSE4_2, 0x00000001, 0, RTE_REG_ECX, 20)
      79                 :            :         FEAT_DEF(X2APIC, 0x00000001, 0, RTE_REG_ECX, 21)
      80                 :            :         FEAT_DEF(MOVBE, 0x00000001, 0, RTE_REG_ECX, 22)
      81                 :            :         FEAT_DEF(POPCNT, 0x00000001, 0, RTE_REG_ECX, 23)
      82                 :            :         FEAT_DEF(TSC_DEADLINE, 0x00000001, 0, RTE_REG_ECX, 24)
      83                 :            :         FEAT_DEF(AES, 0x00000001, 0, RTE_REG_ECX, 25)
      84                 :            :         FEAT_DEF(XSAVE, 0x00000001, 0, RTE_REG_ECX, 26)
      85                 :            :         FEAT_DEF(OSXSAVE, 0x00000001, 0, RTE_REG_ECX, 27)
      86                 :            :         FEAT_DEF_XSTATE(AVX, 0x00000001, 0, RTE_REG_ECX, 28, XSTATE_AVX)
      87                 :            :         FEAT_DEF_XSTATE(F16C, 0x00000001, 0, RTE_REG_ECX, 29, XSTATE_AVX)
      88                 :            :         FEAT_DEF(RDRAND, 0x00000001, 0, RTE_REG_ECX, 30)
      89                 :            :         FEAT_DEF(HYPERVISOR, 0x00000001, 0, RTE_REG_ECX, 31)
      90                 :            : 
      91                 :            :         FEAT_DEF(FPU, 0x00000001, 0, RTE_REG_EDX,  0)
      92                 :            :         FEAT_DEF(VME, 0x00000001, 0, RTE_REG_EDX,  1)
      93                 :            :         FEAT_DEF(DE, 0x00000001, 0, RTE_REG_EDX,  2)
      94                 :            :         FEAT_DEF(PSE, 0x00000001, 0, RTE_REG_EDX,  3)
      95                 :            :         FEAT_DEF(TSC, 0x00000001, 0, RTE_REG_EDX,  4)
      96                 :            :         FEAT_DEF(MSR, 0x00000001, 0, RTE_REG_EDX,  5)
      97                 :            :         FEAT_DEF(PAE, 0x00000001, 0, RTE_REG_EDX,  6)
      98                 :            :         FEAT_DEF(MCE, 0x00000001, 0, RTE_REG_EDX,  7)
      99                 :            :         FEAT_DEF(CX8, 0x00000001, 0, RTE_REG_EDX,  8)
     100                 :            :         FEAT_DEF(APIC, 0x00000001, 0, RTE_REG_EDX,  9)
     101                 :            :         FEAT_DEF(SEP, 0x00000001, 0, RTE_REG_EDX, 11)
     102                 :            :         FEAT_DEF(MTRR, 0x00000001, 0, RTE_REG_EDX, 12)
     103                 :            :         FEAT_DEF(PGE, 0x00000001, 0, RTE_REG_EDX, 13)
     104                 :            :         FEAT_DEF(MCA, 0x00000001, 0, RTE_REG_EDX, 14)
     105                 :            :         FEAT_DEF(CMOV, 0x00000001, 0, RTE_REG_EDX, 15)
     106                 :            :         FEAT_DEF(PAT, 0x00000001, 0, RTE_REG_EDX, 16)
     107                 :            :         FEAT_DEF(PSE36, 0x00000001, 0, RTE_REG_EDX, 17)
     108                 :            :         FEAT_DEF(PSN, 0x00000001, 0, RTE_REG_EDX, 18)
     109                 :            :         FEAT_DEF(CLFSH, 0x00000001, 0, RTE_REG_EDX, 19)
     110                 :            :         FEAT_DEF(DS, 0x00000001, 0, RTE_REG_EDX, 21)
     111                 :            :         FEAT_DEF(ACPI, 0x00000001, 0, RTE_REG_EDX, 22)
     112                 :            :         FEAT_DEF(MMX, 0x00000001, 0, RTE_REG_EDX, 23)
     113                 :            :         FEAT_DEF(FXSR, 0x00000001, 0, RTE_REG_EDX, 24)
     114                 :            :         FEAT_DEF(SSE, 0x00000001, 0, RTE_REG_EDX, 25)
     115                 :            :         FEAT_DEF(SSE2, 0x00000001, 0, RTE_REG_EDX, 26)
     116                 :            :         FEAT_DEF(SS, 0x00000001, 0, RTE_REG_EDX, 27)
     117                 :            :         FEAT_DEF(HTT, 0x00000001, 0, RTE_REG_EDX, 28)
     118                 :            :         FEAT_DEF(TM, 0x00000001, 0, RTE_REG_EDX, 29)
     119                 :            :         FEAT_DEF(PBE, 0x00000001, 0, RTE_REG_EDX, 31)
     120                 :            : 
     121                 :            :         FEAT_DEF(DIGTEMP, 0x00000006, 0, RTE_REG_EAX,  0)
     122                 :            :         FEAT_DEF(TRBOBST, 0x00000006, 0, RTE_REG_EAX,  1)
     123                 :            :         FEAT_DEF(ARAT, 0x00000006, 0, RTE_REG_EAX,  2)
     124                 :            :         FEAT_DEF(PLN, 0x00000006, 0, RTE_REG_EAX,  4)
     125                 :            :         FEAT_DEF(ECMD, 0x00000006, 0, RTE_REG_EAX,  5)
     126                 :            :         FEAT_DEF(PTM, 0x00000006, 0, RTE_REG_EAX,  6)
     127                 :            : 
     128                 :            :         FEAT_DEF(MPERF_APERF_MSR, 0x00000006, 0, RTE_REG_ECX,  0)
     129                 :            :         FEAT_DEF(ACNT2, 0x00000006, 0, RTE_REG_ECX,  1)
     130                 :            :         FEAT_DEF(ENERGY_EFF, 0x00000006, 0, RTE_REG_ECX,  3)
     131                 :            : 
     132                 :            :         FEAT_DEF(FSGSBASE, 0x00000007, 0, RTE_REG_EBX,  0)
     133                 :            :         FEAT_DEF(BMI1, 0x00000007, 0, RTE_REG_EBX,  3)
     134                 :            :         FEAT_DEF(HLE, 0x00000007, 0, RTE_REG_EBX,  4)
     135                 :            :         FEAT_DEF_XSTATE(AVX2, 0x00000007, 0, RTE_REG_EBX,  5, XSTATE_AVX)
     136                 :            :         FEAT_DEF(SMEP, 0x00000007, 0, RTE_REG_EBX,  7)
     137                 :            :         FEAT_DEF(BMI2, 0x00000007, 0, RTE_REG_EBX,  8)
     138                 :            :         FEAT_DEF(ERMS, 0x00000007, 0, RTE_REG_EBX,  9)
     139                 :            :         FEAT_DEF(INVPCID, 0x00000007, 0, RTE_REG_EBX, 10)
     140                 :            :         FEAT_DEF(RTM, 0x00000007, 0, RTE_REG_EBX, 11)
     141                 :            :         FEAT_DEF_XSTATE(AVX512F, 0x00000007, 0, RTE_REG_EBX, 16, XSTATE_AVX512)
     142                 :            :         FEAT_DEF_XSTATE(AVX512DQ, 0x00000007, 0, RTE_REG_EBX, 17, XSTATE_AVX512)
     143                 :            :         FEAT_DEF(RDSEED, 0x00000007, 0, RTE_REG_EBX, 18)
     144                 :            :         FEAT_DEF_XSTATE(AVX512IFMA, 0x00000007, 0, RTE_REG_EBX, 21, XSTATE_AVX512)
     145                 :            :         FEAT_DEF_XSTATE(AVX512CD, 0x00000007, 0, RTE_REG_EBX, 28, XSTATE_AVX512)
     146                 :            :         FEAT_DEF_XSTATE(AVX512BW, 0x00000007, 0, RTE_REG_EBX, 30, XSTATE_AVX512)
     147                 :            :         FEAT_DEF_XSTATE(AVX512VL, 0x00000007, 0, RTE_REG_EBX, 31, XSTATE_AVX512)
     148                 :            : 
     149                 :            :         FEAT_DEF_XSTATE(AVX512VBMI, 0x00000007, 0, RTE_REG_ECX,  1, XSTATE_AVX512)
     150                 :            :         FEAT_DEF(WAITPKG, 0x00000007, 0, RTE_REG_ECX,  5)
     151                 :            :         FEAT_DEF_XSTATE(AVX512VBMI2, 0x00000007, 0, RTE_REG_ECX,  6, XSTATE_AVX512)
     152                 :            :         FEAT_DEF(GFNI, 0x00000007, 0, RTE_REG_ECX,  8)
     153                 :            :         FEAT_DEF_XSTATE(VAES, 0x00000007, 0, RTE_REG_ECX,  9, XSTATE_AVX)
     154                 :            :         FEAT_DEF_XSTATE(VPCLMULQDQ, 0x00000007, 0, RTE_REG_ECX, 10, XSTATE_AVX)
     155                 :            :         FEAT_DEF_XSTATE(AVX512VNNI, 0x00000007, 0, RTE_REG_ECX, 11, XSTATE_AVX512)
     156                 :            :         FEAT_DEF_XSTATE(AVX512BITALG, 0x00000007, 0, RTE_REG_ECX, 12, XSTATE_AVX512)
     157                 :            :         FEAT_DEF_XSTATE(AVX512VPOPCNTDQ, 0x00000007, 0, RTE_REG_ECX, 14, XSTATE_AVX512)
     158                 :            :         FEAT_DEF(CLDEMOTE, 0x00000007, 0, RTE_REG_ECX, 25)
     159                 :            :         FEAT_DEF(MOVDIRI, 0x00000007, 0, RTE_REG_ECX, 27)
     160                 :            :         FEAT_DEF(MOVDIR64B, 0x00000007, 0, RTE_REG_ECX, 28)
     161                 :            : 
     162                 :            :         FEAT_DEF_XSTATE(AVX512VP2INTERSECT, 0x00000007, 0, RTE_REG_EDX,  8, XSTATE_AVX512)
     163                 :            : 
     164                 :            :         FEAT_DEF(LAHF_SAHF, 0x80000001, 0, RTE_REG_ECX,  0)
     165                 :            :         FEAT_DEF(LZCNT, 0x80000001, 0, RTE_REG_ECX,  5)
     166                 :            :         FEAT_DEF(MONITORX, 0x80000001, 0, RTE_REG_ECX,  29)
     167                 :            : 
     168                 :            :         FEAT_DEF(SYSCALL, 0x80000001, 0, RTE_REG_EDX, 11)
     169                 :            :         FEAT_DEF(XD, 0x80000001, 0, RTE_REG_EDX, 20)
     170                 :            :         FEAT_DEF(1GB_PG, 0x80000001, 0, RTE_REG_EDX, 26)
     171                 :            :         FEAT_DEF(RDTSCP, 0x80000001, 0, RTE_REG_EDX, 27)
     172                 :            :         FEAT_DEF(EM64T, 0x80000001, 0, RTE_REG_EDX, 29)
     173                 :            : 
     174                 :            :         FEAT_DEF(INVTSC, 0x80000007, 0, RTE_REG_EDX,  8)
     175                 :            : };
     176                 :            : 
     177                 :            : static uint64_t
     178                 :            : xcr0_read(void)
     179                 :            : {
     180                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     181                 :            :         return _xgetbv(0);
     182                 :            : #else
     183                 :            :         uint32_t eax, edx;
     184                 :            : 
     185                 :            :         /* use the raw mnemonic: _xgetbv() would need -mxsave */
     186                 :       2084 :         asm volatile("xgetbv" : "=a" (eax), "=d" (edx) : "c" (0));
     187                 :       2084 :         return ((uint64_t)edx << 32) | eax;
     188                 :            : #endif
     189                 :            : }
     190                 :            : 
     191                 :            : /*
     192                 :            :  * CPUID reports what the CPU implements, not what the OS enables.
     193                 :            :  * Check that the OS saves the register state that the feature uses.
     194                 :            :  */
     195                 :            : static bool
     196                 :       2084 : xstate_enabled(uint64_t xstate)
     197                 :            : {
     198                 :            :         /*
     199                 :            :          * XGETBV faults unless the OS has set CR4.OSXSAVE.
     200                 :            :          * The OSXSAVE entry must not have an xstate mask,
     201                 :            :          * else this call recurses without end.
     202                 :            :          */
     203         [ +  - ]:       2084 :         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_OSXSAVE) != 1)
     204                 :            :                 return false;
     205                 :            : 
     206                 :       2084 :         return (xcr0_read() & xstate) == xstate;
     207                 :            : }
     208                 :            : 
     209                 :            : RTE_EXPORT_SYMBOL(rte_cpu_get_flag_enabled)
     210                 :            : int
     211                 :     257873 : rte_cpu_get_flag_enabled(enum rte_cpu_flag_t feature)
     212                 :            : {
     213                 :            :         struct feature_entry *feat;
     214                 :            :         cpuid_registers_t regs;
     215                 :            :         unsigned int maxleaf;
     216                 :            :         bool value;
     217                 :            : 
     218         [ +  - ]:     257873 :         if ((unsigned int)feature >= RTE_DIM(rte_cpu_feature_table))
     219                 :            :                 /* Flag does not match anything in the feature tables */
     220                 :            :                 return -ENOENT;
     221                 :            : 
     222                 :            :         feat = &rte_cpu_feature_table[feature];
     223         [ +  + ]:     257873 :         if (feat->has_value)
     224                 :     250752 :                 return feat->value;
     225                 :            : 
     226         [ +  - ]:       7121 :         if (!feat->leaf)
     227                 :            :                 /* This entry in the table wasn't filled out! */
     228                 :            :                 return -EFAULT;
     229                 :            : 
     230                 :       7121 :         maxleaf = __get_cpuid_max(feat->leaf & 0x80000000, NULL);
     231                 :            : 
     232         [ -  + ]:       7121 :         if (maxleaf < feat->leaf) {
     233                 :          0 :                 feat->value = 0;
     234                 :          0 :                 goto out;
     235                 :            :         }
     236                 :            : 
     237                 :            : #ifdef RTE_TOOLCHAIN_MSVC
     238                 :            :         __cpuidex(regs, feat->leaf, feat->subleaf);
     239                 :            : #else
     240                 :       7121 :         __cpuid_count(feat->leaf, feat->subleaf,
     241                 :            :                          regs[RTE_REG_EAX], regs[RTE_REG_EBX],
     242                 :            :                          regs[RTE_REG_ECX], regs[RTE_REG_EDX]);
     243                 :            : #endif
     244                 :            : 
     245                 :            :         /* check if the feature is enabled */
     246                 :       7121 :         value = (regs[feat->reg] >> feat->bit) & 1;
     247                 :            : 
     248                 :            :         /* check if the OS enabled the register state for the feature */
     249   [ +  +  +  + ]:       7121 :         if (value && feat->xstate != 0)
     250                 :       2084 :                 value = xstate_enabled(feat->xstate);
     251                 :            : 
     252                 :       7121 :         feat->value = value;
     253                 :       7121 : out:
     254                 :       7121 :         rte_compiler_barrier();
     255                 :       7121 :         feat->has_value = true;
     256                 :       7121 :         return feat->value;
     257                 :            : }
     258                 :            : 
     259                 :            : RTE_EXPORT_SYMBOL(rte_cpu_get_flag_name)
     260                 :            : const char *
     261                 :          0 : rte_cpu_get_flag_name(enum rte_cpu_flag_t feature)
     262                 :            : {
     263         [ #  # ]:          0 :         if ((unsigned int)feature >= RTE_DIM(rte_cpu_feature_table))
     264                 :            :                 return NULL;
     265                 :          0 :         return rte_cpu_feature_table[feature].name;
     266                 :            : }
     267                 :            : 
     268                 :            : RTE_EXPORT_SYMBOL(rte_cpu_get_intrinsics_support)
     269                 :            : void
     270                 :        305 : rte_cpu_get_intrinsics_support(struct rte_cpu_intrinsics *intrinsics)
     271                 :            : {
     272                 :            :         memset(intrinsics, 0, sizeof(*intrinsics));
     273                 :            : 
     274         [ -  + ]:        305 :         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_WAITPKG)) {
     275                 :          0 :                 intrinsics->power_monitor = 1;
     276                 :          0 :                 intrinsics->power_pause = 1;
     277         [ #  # ]:          0 :                 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_RTM))
     278                 :          0 :                         intrinsics->power_monitor_multi = 1;
     279         [ -  + ]:        305 :         } else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_MONITORX)) {
     280                 :          0 :                 intrinsics->power_monitor = 1;
     281                 :            :         }
     282                 :        305 : }

Generated by: LCOV version 1.14