Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FreeBSD on x86 #163

Merged
merged 8 commits into from
Jul 2, 2021
Merged

Add support for FreeBSD on x86 #163

merged 8 commits into from
Jul 2, 2021

Conversation

gchatelet
Copy link
Collaborator

This is replacing #159 and fixes #158

@gchatelet gchatelet added the enhancement New feature or request label Jun 25, 2021
@gchatelet
Copy link
Collaborator Author

To submit when we have continuous integration for FreeBSD via Vagrant.

@gchatelet gchatelet requested a review from Mizux June 25, 2021 07:59
if (fd >= 0) {
StackLineReader reader;
StackLineReader_Initialize(&reader, fd);
for (;;) {
Copy link
Collaborator

@Mizux Mizux Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is there any difference with using while (true) { ?
EDIT: you're using for (;;) also just below so for consistency, we should at least use the same semantic in both cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference between for(;;) and while(true). We could use either one.

src/cpuinfo_x86.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@Mizux Mizux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two comments...

@gchatelet gchatelet mentioned this pull request Jun 25, 2021
#elif defined(CPU_FEATURES_OS_FREEBSD)
auto& fs = GetEmptyFilesystem();
fs.CreateFile("/var/run/dmesg.boot", R"(
---<<BOOT>>---
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this will be moot when the branch goes EOL at the end of September, FreeBSD 11 does not have this line in dmesg.boot.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. It was present in a dump of a FreeBSD 12.2-RELEASE r366954 GENERIC amd64

---<<BOOT>>---
Copyright (c) 1992-2020 The FreeBSD Project.
Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
	The Regents of the University of California. All rights reserved.
FreeBSD is a registered trademark of The FreeBSD Foundation.
FreeBSD 12.2-RELEASE r366954 GENERIC amd64
FreeBSD clang version 10.0.1 (git@github.com:llvm/llvm-project.git llvmorg-10.0.1-0-gef32c611aa2)
VT(vga): text 80x25
module vtnet already present!
CPU: Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz (1607.96-MHz K8-class CPU)
  Origin="GenuineIntel"  Id=0xa0660  Family=0x6  Model=0xa6  Stepping=0
  Features=0x1783fbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,MMX,FXSR,SSE,SSE2,HTT>
  Features2=0x5eda2203<SSE3,PCLMULQDQ,SSSE3,CX16,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,AESNI,XSAVE,OSXSAVE,AVX,RDRAND>
  AMD Features=0x28100800<SYSCALL,NX,RDTSCP,LM>
  AMD Features2=0x121<LAHF,ABM,Prefetch>
  Structured Extended Features=0x842421<FSGSBASE,AVX2,INVPCID,NFPUSG,RDSEED,CLFLUSHOPT>
  Structured Extended Features3=0x30000400<MD_CLEAR,L1DFL,ARCH_CAP>
  TSC: P-state invariant
real memory  = 2147418112 (2047 MB)
avail memory = 2044313600 (1949 MB)

FWIW we don't use this line but it's a representative input.

@vishwin
Copy link

vishwin commented Jun 25, 2021

Already failing on 14-CURRENT:

[  2% 5/110] /ccache/libexec/ccache/cc -DSTACK_LINE_READER_BUFFER_SIZE=1024 -D_GLIBCXX_USE_CXX11_ABI=1 -I/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/include -I/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/include/internal -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -Wall -Werror=incompatible-pointer-types -Werror=pointer-sign -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -fPIC -std=gnu99 -MD -MT cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o -MF cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o.d -o cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o -c /wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c
FAILED: cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o
/ccache/libexec/ccache/cc -DSTACK_LINE_READER_BUFFER_SIZE=1024 -D_GLIBCXX_USE_CXX11_ABI=1 -I/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/include -I/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/include/internal -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -Wall -Werror=incompatible-pointer-types -Werror=pointer-sign -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -fPIC -std=gnu99 -MD -MT cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o -MF cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o.d -o cpu_features/CMakeFiles/cpu_features.dir/src/cpuinfo_x86.c.o -c /wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:111:2: error: "Unsupported OS"
#error "Unsupported OS"
 ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1335:20: warning: implicit declaration of function 'CpuFeatures_OpenFile' is invalid in C99 [-Wimplicit-function-declaration]
    const int fd = CpuFeatures_OpenFile("/var/run/dmesg.boot");
                   ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1337:7: error: use of undeclared identifier 'StackLineReader'
      StackLineReader reader;
      ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1338:7: warning: implicit declaration of function 'StackLineReader_Initialize' is invalid in C99 [-Wimplicit-function-declaration]
      StackLineReader_Initialize(&reader, fd);
      ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1338:35: error: use of undeclared identifier 'reader'
      StackLineReader_Initialize(&reader, fd);
                                  ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1340:15: error: unknown type name 'LineResult'
        const LineResult result = StackLineReader_NextLine(&reader);
              ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1340:35: warning: implicit declaration of function 'StackLineReader_NextLine' is invalid in C99 [-Wimplicit-function-declaration]
        const LineResult result = StackLineReader_NextLine(&reader);
                                  ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1340:61: error: use of undeclared identifier 'reader'
        const LineResult result = StackLineReader_NextLine(&reader);
                                                            ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1341:13: warning: implicit declaration of function 'CpuFeatures_StringView_StartsWith' is invalid in C99 [-Wimplicit-function-declaration]
        if (CpuFeatures_StringView_StartsWith(result.line,
            ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1342:47: warning: implicit declaration of function 'str' is invalid in C99 [-Wimplicit-function-declaration]
                                              str("  Features=")) ||
                                              ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1354:15: warning: implicit declaration of function 'CpuFeatures_StringView_HasWord' is invalid in C99 [-Wimplicit-function-declaration]
          if (CpuFeatures_StringView_HasWord(value, "SSE"))
              ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1354:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSE"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1356:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSE2"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1358:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSE3"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1360:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSSE3"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1362:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSE4.1"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1364:46: error: use of undeclared identifier 'value'
          if (CpuFeatures_StringView_HasWord(value, "SSE4.2"))
                                             ^
/wrkdirs/overlays/devel/devel/volk/work/volk-2.5.0/cpu_features/src/cpuinfo_x86.c:1370:5: warning: implicit declaration of function 'CpuFeatures_CloseFile' is invalid in C99 [-Wimplicit-function-declaration]
    CpuFeatures_CloseFile(fd);
    ^
7 warnings and 11 errors generated.

@mldulaney
Copy link

mldulaney commented Jun 25, 2021

edit: @vishwin beat me to it.

@gchatelet
Copy link
Collaborator Author

Thx @mldulaney and @vishwin ! We're waiting to have at least one FreeBSD CI before moving on with this patch.
Stay tuned!

@Mizux
Copy link
Collaborator

Mizux commented Jun 30, 2021

@gchatelet FYI:

    default: /usr/home/vagrant/project/test/cpuinfo_x86_test.cc
    default: :
    default: 762:3: error: use of undeclared identifier 'fs'
    default:   fs.CreateFile("/var/run/dmesg.boot", R"(
    default:   ^
    default: /usr/home/vagrant/project/test/cpuinfo_x86_test.cc:837:3: error: use of undeclared identifier 'fs'
    default:   fs.CreateFile("/var/run/dmesg.boot", R"(
    default:   ^
    default: 2 errors generated.
    default: *** Error code 1
    default: 
    default: Stop.
    default: make[2]: stopped in /usr/home/vagrant/project/build
    default: *** Error code 1
    default: 
    default: Stop.
    default: make[1]: stopped in /usr/home/vagrant/project/build
    default: *** Error code 1
    default: 
    default: Stop.
    default: make: stopped in /usr/home/vagrant/project/build

ref: https://github.com/google/cpu_features/runs/2954585917?check_suite_focus=true#step:5:902

@mldulaney
Copy link

-- Looking for getauxval - not found
CMake Error at cpu_features/CMakeLists.txt:196 (message):
CMake step for googletest failed: No such file or directory

-- Configuring incomplete, errors occurred!

@evadot
Copy link

evadot commented Jun 30, 2021

Not familiar with this lib but you should never parse dmesg.boot or use some sysctl to obtain this info.
You should use elf_aux_info with AT_HWCAP/AT_HWCAP2.

@gchatelet
Copy link
Collaborator Author

Not familiar with this lib but you should never parse dmesg.boot or use some sysctl to obtain this info.
You should use elf_aux_info with AT_HWCAP/AT_HWCAP2.

Thx a lot @evadot. This is exactly what I was looking for. From the documentation: The elf_aux_info() function appeared in FreeBSD 12.0.. How would I get this information prior to FreeBSD 12?

@evadot
Copy link

evadot commented Jul 1, 2021

Not familiar with this lib but you should never parse dmesg.boot or use some sysctl to obtain this info.
You should use elf_aux_info with AT_HWCAP/AT_HWCAP2.

Thx a lot @evadot. This is exactly what I was looking for. From the documentation: The elf_aux_info() function appeared in FreeBSD 12.0.. How would I get this information prior to FreeBSD 12?

FreeBSD 11 will be EOL in september so I wouldn't bother.

1 similar comment
@evadot
Copy link

evadot commented Jul 1, 2021

Not familiar with this lib but you should never parse dmesg.boot or use some sysctl to obtain this info.
You should use elf_aux_info with AT_HWCAP/AT_HWCAP2.

Thx a lot @evadot. This is exactly what I was looking for. From the documentation: The elf_aux_info() function appeared in FreeBSD 12.0.. How would I get this information prior to FreeBSD 12?

FreeBSD 11 will be EOL in september so I wouldn't bother.

@gchatelet
Copy link
Collaborator Author

@evadot let me know if I understand this correctly.
From these lines here and here, only AT_HWCAP is set (AT_HWCAP2 is set to zero).

Now AT_HWCAP for x86 seems to return cpu_features which AFAIU can be interpreted with one of the CPUID_* masks here. This gives me access to XMM, MMX, SSE and SSE2. To retrieve cpu features for SSE3, SSSE3, SSE4.1 and SSE4.2 I'd need to get cpu_feature2 which - it seems - is not available through AT_HWCAP2. Am I missing something?

@evadot
Copy link

evadot commented Jul 1, 2021

@evadot let me know if I understand this correctly.
From these lines here and here, only AT_HWCAP is set (AT_HWCAP2 is set to zero).

Now AT_HWCAP for x86 seems to return cpu_features which AFAIU can be interpreted with one of the CPUID_* masks here. This gives me access to XMM, MMX, SSE and SSE2. To retrieve cpu features for SSE3, SSSE3, SSE4.1 and SSE4.2 I'd need to get cpu_feature2 which - it seems - is not available through AT_HWCAP2. Am I missing something?

That's for the linuxulator.
Apparently we do not expose AT_HWCAP on x86 but only for powerpc*, arm* and riscv.
You should use the cpuid directly on amd64, you can look at https://cgit.freebsd.org/src/tree/lib/csu/amd64/reloc.c for an example on how to do this.

@gchatelet
Copy link
Collaborator Author

@evadot let me know if I understand this correctly.
From these lines here and here, only AT_HWCAP is set (AT_HWCAP2 is set to zero).
Now AT_HWCAP for x86 seems to return cpu_features which AFAIU can be interpreted with one of the CPUID_* masks here. This gives me access to XMM, MMX, SSE and SSE2. To retrieve cpu features for SSE3, SSSE3, SSE4.1 and SSE4.2 I'd need to get cpu_feature2 which - it seems - is not available through AT_HWCAP2. Am I missing something?

That's for the linuxulator.
Apparently we do not expose AT_HWCAP on x86 but only for powerpc*, arm* and riscv.
You should use the cpuid directly on amd64, you can look at https://cgit.freebsd.org/src/tree/lib/csu/amd64/reloc.c for an example on how to do this.

Thx for your answer @evadot. This project already uses the cpuid instruction to query the presence/absence of some features at the cpu level, but we also need to know if the OS preserves these vector registers across context switches. For AVX enabled CPUs the OS has the possibility to use the XCR0 register. Then using cpuid is all you need.
Now the code here deals with pre-AVX cpus (Atom, Westmere, ...) where there is no way to ask the CPU whether the OS will save and restore vector registers across context switches. We have to query the OS to get this information. As far as I understand, the only way to get this information on FreeBSD is to parse /var/run/dmesg.boot. Let me know if you see another way.

An interesting sidenote, for Darwin - which is FreeBSD based - the OS disables AVX512 registers save/restore by default and enables them right after the first fault. Thus querying the CPU for AVX512 OS support fails even though it works (source).

src/cpuinfo_x86.c Outdated Show resolved Hide resolved
@gchatelet
Copy link
Collaborator Author

As discussed, let's submit this as-is to repair the build. I'll address the TODO in a subsequent PR.

@gchatelet gchatelet merged commit 1199437 into master Jul 2, 2021
@gchatelet gchatelet deleted the freebsd branch July 2, 2021 13:37
@mldulaney
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fails on FreeBSD: error: "Unsupported fallback detection of SSE OS support."
5 participants