ProcessorInfo.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Types.h>
  8. #include <Kernel/Arch/Processor.h>
  9. #include <Kernel/Arch/x86/CPUID.h>
  10. #include <Kernel/Arch/x86/ProcessorInfo.h>
  11. namespace Kernel {
  12. ProcessorInfo::ProcessorInfo(Processor const& processor)
  13. : m_vendor_id_string(build_vendor_id_string())
  14. , m_brand_string(build_brand_string())
  15. , m_features_string(build_features_string(processor))
  16. {
  17. CPUID cpuid(1);
  18. m_stepping = cpuid.eax() & 0xf;
  19. u32 model = (cpuid.eax() >> 4) & 0xf;
  20. u32 family = (cpuid.eax() >> 8) & 0xf;
  21. m_type = (cpuid.eax() >> 12) & 0x3;
  22. u32 extended_model = (cpuid.eax() >> 16) & 0xf;
  23. u32 extended_family = (cpuid.eax() >> 20) & 0xff;
  24. if (family == 15) {
  25. m_display_family = family + extended_family;
  26. m_display_model = model + (extended_model << 4);
  27. } else if (family == 6) {
  28. m_display_family = family;
  29. m_display_model = model + (extended_model << 4);
  30. } else {
  31. m_display_family = family;
  32. m_display_model = model;
  33. }
  34. }
  35. NonnullOwnPtr<KString> ProcessorInfo::build_vendor_id_string()
  36. {
  37. CPUID cpuid(0);
  38. StringBuilder builder;
  39. auto emit_u32 = [&](u32 value) {
  40. builder.appendff("{:c}{:c}{:c}{:c}",
  41. value & 0xff,
  42. (value >> 8) & 0xff,
  43. (value >> 16) & 0xff,
  44. (value >> 24) & 0xff);
  45. };
  46. emit_u32(cpuid.ebx());
  47. emit_u32(cpuid.edx());
  48. emit_u32(cpuid.ecx());
  49. return KString::must_create(builder.string_view());
  50. }
  51. NonnullOwnPtr<KString> ProcessorInfo::build_brand_string()
  52. {
  53. u32 max_extended_leaf = CPUID(0x80000000).eax();
  54. if (max_extended_leaf < 0x80000004)
  55. return KString::must_create({});
  56. alignas(u32) char buffer[48];
  57. u32* bufptr = reinterpret_cast<u32*>(buffer);
  58. auto copy_brand_string_part_to_buffer = [&](u32 i) {
  59. CPUID cpuid(0x80000002 + i);
  60. *bufptr++ = cpuid.eax();
  61. *bufptr++ = cpuid.ebx();
  62. *bufptr++ = cpuid.ecx();
  63. *bufptr++ = cpuid.edx();
  64. };
  65. copy_brand_string_part_to_buffer(0);
  66. copy_brand_string_part_to_buffer(1);
  67. copy_brand_string_part_to_buffer(2);
  68. return KString::must_create(buffer);
  69. }
  70. NonnullOwnPtr<KString> ProcessorInfo::build_features_string(Processor const& processor)
  71. {
  72. StringBuilder builder;
  73. bool first = true;
  74. for (auto feature = CPUFeature::Type(1u); feature != CPUFeature::__End; feature <<= 1u) {
  75. if (processor.has_feature(feature)) {
  76. if (first)
  77. first = false;
  78. else
  79. MUST(builder.try_append(' '));
  80. MUST(builder.try_append(cpu_feature_to_string_view(feature)));
  81. }
  82. }
  83. return KString::must_create(builder.string_view());
  84. }
  85. }