ProcessorInfo.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/x86/CPUID.h>
  9. #include <Kernel/Arch/x86/Processor.h>
  10. #include <Kernel/Arch/x86/ProcessorInfo.h>
  11. namespace Kernel {
  12. ProcessorInfo::ProcessorInfo(Processor& processor)
  13. : m_processor(processor)
  14. {
  15. u32 max_leaf;
  16. {
  17. CPUID cpuid(0);
  18. StringBuilder builder;
  19. auto emit_u32 = [&](u32 value) {
  20. builder.appendff("{:c}{:c}{:c}{:c}",
  21. value & 0xff,
  22. (value >> 8) & 0xff,
  23. (value >> 16) & 0xff,
  24. (value >> 24) & 0xff);
  25. };
  26. max_leaf = cpuid.eax();
  27. emit_u32(cpuid.ebx());
  28. emit_u32(cpuid.edx());
  29. emit_u32(cpuid.ecx());
  30. m_cpuid = builder.build();
  31. }
  32. {
  33. VERIFY(max_leaf >= 1);
  34. CPUID cpuid(1);
  35. m_stepping = cpuid.eax() & 0xf;
  36. u32 model = (cpuid.eax() >> 4) & 0xf;
  37. u32 family = (cpuid.eax() >> 8) & 0xf;
  38. m_type = (cpuid.eax() >> 12) & 0x3;
  39. u32 extended_model = (cpuid.eax() >> 16) & 0xf;
  40. u32 extended_family = (cpuid.eax() >> 20) & 0xff;
  41. if (family == 15) {
  42. m_display_family = family + extended_family;
  43. m_display_model = model + (extended_model << 4);
  44. } else if (family == 6) {
  45. m_display_family = family;
  46. m_display_model = model + (extended_model << 4);
  47. } else {
  48. m_display_family = family;
  49. m_display_model = model;
  50. }
  51. }
  52. u32 max_extended_leaf = CPUID(0x80000000).eax();
  53. if (max_extended_leaf >= 0x80000004) {
  54. alignas(u32) char buffer[48];
  55. u32* bufptr = reinterpret_cast<u32*>(buffer);
  56. auto copy_brand_string_part_to_buffer = [&](u32 i) {
  57. CPUID cpuid(0x80000002 + i);
  58. *bufptr++ = cpuid.eax();
  59. *bufptr++ = cpuid.ebx();
  60. *bufptr++ = cpuid.ecx();
  61. *bufptr++ = cpuid.edx();
  62. };
  63. copy_brand_string_part_to_buffer(0);
  64. copy_brand_string_part_to_buffer(1);
  65. copy_brand_string_part_to_buffer(2);
  66. m_brandstr = buffer;
  67. }
  68. // Cache the CPU feature string
  69. m_features = m_processor.features_string();
  70. }
  71. }