xref: /aosp_15_r20/external/cpu_features/src/impl_x86_freebsd.c (revision eca53ba6d2e951e174b64682eaf56a36b8204c89)
1*eca53ba6SRoland Levillain // Copyright 2017 Google LLC
2*eca53ba6SRoland Levillain //
3*eca53ba6SRoland Levillain // Licensed under the Apache License, Version 2.0 (the "License");
4*eca53ba6SRoland Levillain // you may not use this file except in compliance with the License.
5*eca53ba6SRoland Levillain // You may obtain a copy of the License at
6*eca53ba6SRoland Levillain //
7*eca53ba6SRoland Levillain //    http://www.apache.org/licenses/LICENSE-2.0
8*eca53ba6SRoland Levillain //
9*eca53ba6SRoland Levillain // Unless required by applicable law or agreed to in writing, software
10*eca53ba6SRoland Levillain // distributed under the License is distributed on an "AS IS" BASIS,
11*eca53ba6SRoland Levillain // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eca53ba6SRoland Levillain // See the License for the specific language governing permissions and
13*eca53ba6SRoland Levillain // limitations under the License.
14*eca53ba6SRoland Levillain 
15*eca53ba6SRoland Levillain #include "cpu_features_macros.h"
16*eca53ba6SRoland Levillain 
17*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_ARCH_X86
18*eca53ba6SRoland Levillain #ifdef CPU_FEATURES_OS_FREEBSD
19*eca53ba6SRoland Levillain 
20*eca53ba6SRoland Levillain #include "impl_x86__base_implementation.inl"
21*eca53ba6SRoland Levillain 
OverrideOsPreserves(OsPreserves * os_preserves)22*eca53ba6SRoland Levillain static void OverrideOsPreserves(OsPreserves* os_preserves) {
23*eca53ba6SRoland Levillain   (void)os_preserves;
24*eca53ba6SRoland Levillain   // No override
25*eca53ba6SRoland Levillain }
26*eca53ba6SRoland Levillain 
27*eca53ba6SRoland Levillain #include "internal/filesystem.h"
28*eca53ba6SRoland Levillain #include "internal/stack_line_reader.h"
29*eca53ba6SRoland Levillain #include "internal/string_view.h"
30*eca53ba6SRoland Levillain 
DetectFeaturesFromOs(X86Info * info,X86Features * features)31*eca53ba6SRoland Levillain static void DetectFeaturesFromOs(X86Info* info, X86Features* features) {
32*eca53ba6SRoland Levillain   (void)info;
33*eca53ba6SRoland Levillain   // Handling FreeBSD platform through parsing /var/run/dmesg.boot.
34*eca53ba6SRoland Levillain   const int fd = CpuFeatures_OpenFile("/var/run/dmesg.boot");
35*eca53ba6SRoland Levillain   if (fd >= 0) {
36*eca53ba6SRoland Levillain     StackLineReader reader;
37*eca53ba6SRoland Levillain     StackLineReader_Initialize(&reader, fd);
38*eca53ba6SRoland Levillain     for (bool stop = false; !stop;) {
39*eca53ba6SRoland Levillain       const LineResult result = StackLineReader_NextLine(&reader);
40*eca53ba6SRoland Levillain       if (result.eof) stop = true;
41*eca53ba6SRoland Levillain       const StringView line = result.line;
42*eca53ba6SRoland Levillain       if (!CpuFeatures_StringView_StartsWith(line, str("  Features"))) continue;
43*eca53ba6SRoland Levillain       // Lines of interests are of the following form:
44*eca53ba6SRoland Levillain       // "  Features=0x1783fbff<PSE36,MMX,FXSR,SSE,SSE2,HTT>"
45*eca53ba6SRoland Levillain       // We first extract the comma separated values between angle brackets.
46*eca53ba6SRoland Levillain       StringView csv = result.line;
47*eca53ba6SRoland Levillain       int index = CpuFeatures_StringView_IndexOfChar(csv, '<');
48*eca53ba6SRoland Levillain       if (index >= 0) csv = CpuFeatures_StringView_PopFront(csv, index + 1);
49*eca53ba6SRoland Levillain       if (csv.size > 0 && CpuFeatures_StringView_Back(csv) == '>')
50*eca53ba6SRoland Levillain         csv = CpuFeatures_StringView_PopBack(csv, 1);
51*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSE", ',')) features->sse = true;
52*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSE2", ','))
53*eca53ba6SRoland Levillain         features->sse2 = true;
54*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSE3", ','))
55*eca53ba6SRoland Levillain         features->sse3 = true;
56*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSSE3", ','))
57*eca53ba6SRoland Levillain         features->ssse3 = true;
58*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSE4.1", ','))
59*eca53ba6SRoland Levillain         features->sse4_1 = true;
60*eca53ba6SRoland Levillain       if (CpuFeatures_StringView_HasWord(csv, "SSE4.2", ','))
61*eca53ba6SRoland Levillain         features->sse4_2 = true;
62*eca53ba6SRoland Levillain     }
63*eca53ba6SRoland Levillain     CpuFeatures_CloseFile(fd);
64*eca53ba6SRoland Levillain   }
65*eca53ba6SRoland Levillain }
66*eca53ba6SRoland Levillain 
67*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_OS_FREEBSD
68*eca53ba6SRoland Levillain #endif  // CPU_FEATURES_ARCH_X86
69