ProcessorInfo.cpp 2.2 KB

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