Kernel: Convert Processor::features_string() API to KString

This commit is contained in:
Brian Gianforcaro 2022-01-09 03:01:16 -08:00 committed by Andreas Kling
parent f8ad3cc6a0
commit faf1fa0a84
Notes: sideshowbarker 2024-07-17 23:00:03 +09:00
5 changed files with 39 additions and 39 deletions

View file

@ -19,6 +19,7 @@
#include <Kernel/Arch/x86/PageDirectory.h>
#include <Kernel/Arch/x86/TSS.h>
#include <Kernel/Forward.h>
#include <Kernel/KString.h>
#include <AK/Platform.h>
VALIDATE_IS_X86()
@ -123,7 +124,7 @@ class Processor {
void cpu_detect();
void cpu_setup();
String features_string() const;
NonnullOwnPtr<KString> features_string() const;
public:
Processor() = default;

View file

@ -8,6 +8,7 @@
#include <AK/String.h>
#include <AK/Types.h>
#include <Kernel/KString.h>
#include <AK/Platform.h>
VALIDATE_IS_X86()
@ -20,7 +21,7 @@ class ProcessorInfo {
Processor& m_processor;
String m_cpuid;
String m_brandstr;
String m_features;
NonnullOwnPtr<KString> m_features;
u32 m_display_model;
u32 m_display_family;
u32 m_stepping;
@ -32,7 +33,7 @@ public:
const String& cpuid() const { return m_cpuid; }
const String& brandstr() const { return m_brandstr; }
const String& features() const { return m_features; }
StringView features() const { return m_features->view(); }
u32 display_model() const { return m_display_model; }
u32 display_family() const { return m_display_family; }
u32 stepping() const { return m_stepping; }

View file

@ -249,69 +249,69 @@ UNMAP_AFTER_INIT void Processor::cpu_setup()
#endif
}
String Processor::features_string() const
NonnullOwnPtr<KString> Processor::features_string() const
{
StringBuilder builder;
auto feature_to_str =
[](CPUFeature f) -> const char* {
[](CPUFeature f) -> StringView {
switch (f) {
case CPUFeature::NX:
return "nx";
return "nx"sv;
case CPUFeature::PAE:
return "pae";
return "pae"sv;
case CPUFeature::PGE:
return "pge";
return "pge"sv;
case CPUFeature::RDRAND:
return "rdrand";
return "rdrand"sv;
case CPUFeature::RDSEED:
return "rdseed";
return "rdseed"sv;
case CPUFeature::SMAP:
return "smap";
return "smap"sv;
case CPUFeature::SMEP:
return "smep";
return "smep"sv;
case CPUFeature::SSE:
return "sse";
return "sse"sv;
case CPUFeature::TSC:
return "tsc";
return "tsc"sv;
case CPUFeature::RDTSCP:
return "rdtscp";
return "rdtscp"sv;
case CPUFeature::CONSTANT_TSC:
return "constant_tsc";
return "constant_tsc"sv;
case CPUFeature::NONSTOP_TSC:
return "nonstop_tsc";
return "nonstop_tsc"sv;
case CPUFeature::UMIP:
return "umip";
return "umip"sv;
case CPUFeature::SEP:
return "sep";
return "sep"sv;
case CPUFeature::SYSCALL:
return "syscall";
return "syscall"sv;
case CPUFeature::MMX:
return "mmx";
return "mmx"sv;
case CPUFeature::FXSR:
return "fxsr";
return "fxsr"sv;
case CPUFeature::SSE2:
return "sse2";
return "sse2"sv;
case CPUFeature::SSE3:
return "sse3";
return "sse3"sv;
case CPUFeature::SSSE3:
return "ssse3";
return "ssse3"sv;
case CPUFeature::SSE4_1:
return "sse4.1";
return "sse4.1"sv;
case CPUFeature::SSE4_2:
return "sse4.2";
return "sse4.2"sv;
case CPUFeature::XSAVE:
return "xsave";
return "xsave"sv;
case CPUFeature::AVX:
return "avx";
return "avx"sv;
case CPUFeature::LM:
return "lm";
return "lm"sv;
case CPUFeature::HYPERVISOR:
return "hypervisor";
return "hypervisor"sv;
// no default statement here intentionally so that we get
// a warning if a new feature is forgotten to be added here
}
// Shouldn't ever happen
return "???";
return "???"sv;
};
bool first = true;
for (u32 flag = 1; flag != 0; flag <<= 1) {
@ -319,12 +319,12 @@ String Processor::features_string() const
if (first)
first = false;
else
builder.append(' ');
MUST(builder.try_append(' '));
auto str = feature_to_str(static_cast<CPUFeature>(flag));
builder.append(str, strlen(str));
MUST(builder.try_append(str));
}
}
return builder.build();
return KString::must_create(builder.string_view());
}
UNMAP_AFTER_INIT void Processor::early_initialize(u32 cpu)

View file

@ -14,6 +14,7 @@ namespace Kernel {
ProcessorInfo::ProcessorInfo(Processor& processor)
: m_processor(processor)
, m_features(m_processor.features_string())
{
u32 max_leaf;
{
@ -70,9 +71,6 @@ ProcessorInfo::ProcessorInfo(Processor& processor)
copy_brand_string_part_to_buffer(2);
m_brandstr = buffer;
}
// Cache the CPU feature string
m_features = m_processor.features_string();
}
}

View file

@ -563,7 +563,7 @@ private:
obj.add("family", info.display_family());
auto features_array = obj.add_array("features");
for (auto& feature : info.features().split(' '))
for (auto& feature : info.features().split_view(' '))
features_array.add(feature);
features_array.finish();