ProcessorInfo.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <AK/Types.h>
  28. #include <Kernel/Arch/i386/CPU.h>
  29. #include <Kernel/Arch/i386/ProcessorInfo.h>
  30. namespace Kernel {
  31. ProcessorInfo::ProcessorInfo(Processor& processor)
  32. : m_processor(processor)
  33. {
  34. u32 max_leaf;
  35. {
  36. CPUID cpuid(0);
  37. StringBuilder builder;
  38. auto emit_u32 = [&](u32 value) {
  39. builder.appendff("{:c}{:c}{:c}{:c}",
  40. value & 0xff,
  41. (value >> 8) & 0xff,
  42. (value >> 16) & 0xff,
  43. (value >> 24) & 0xff);
  44. };
  45. max_leaf = cpuid.eax();
  46. emit_u32(cpuid.ebx());
  47. emit_u32(cpuid.edx());
  48. emit_u32(cpuid.ecx());
  49. m_cpuid = builder.build();
  50. }
  51. {
  52. ASSERT(max_leaf >= 1);
  53. CPUID cpuid(1);
  54. m_stepping = cpuid.eax() & 0xf;
  55. u32 model = (cpuid.eax() >> 4) & 0xf;
  56. u32 family = (cpuid.eax() >> 8) & 0xf;
  57. m_type = (cpuid.eax() >> 12) & 0x3;
  58. u32 extended_model = (cpuid.eax() >> 16) & 0xf;
  59. u32 extended_family = (cpuid.eax() >> 20) & 0xff;
  60. if (family == 15) {
  61. m_display_family = family + extended_family;
  62. m_display_model = model + (extended_model << 4);
  63. } else if (family == 6) {
  64. m_display_family = family;
  65. m_display_model = model + (extended_model << 4);
  66. } else {
  67. m_display_family = family;
  68. m_display_model = model;
  69. }
  70. }
  71. u32 max_extended_leaf = CPUID(0x80000000).eax();
  72. if (max_extended_leaf >= 0x80000004) {
  73. alignas(u32) char buffer[48];
  74. u32* bufptr = reinterpret_cast<u32*>(buffer);
  75. auto copy_brand_string_part_to_buffer = [&](u32 i) {
  76. CPUID cpuid(0x80000002 + i);
  77. *bufptr++ = cpuid.eax();
  78. *bufptr++ = cpuid.ebx();
  79. *bufptr++ = cpuid.ecx();
  80. *bufptr++ = cpuid.edx();
  81. };
  82. copy_brand_string_part_to_buffer(0);
  83. copy_brand_string_part_to_buffer(1);
  84. copy_brand_string_part_to_buffer(2);
  85. m_brandstr = buffer;
  86. }
  87. // Cache the CPU feature string
  88. m_features = m_processor.features_string();
  89. }
  90. }