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