ProcessorInfo.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/Assertions.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/Types.h>
  31. #include <Kernel/Arch/i386/CPU.h>
  32. #include <Kernel/Arch/i386/ProcessorInfo.h>
  33. namespace Kernel {
  34. ProcessorInfo::ProcessorInfo(Processor& processor):
  35. m_processor(processor)
  36. {
  37. {
  38. CPUID cpuid(0);
  39. StringBuilder builder;
  40. auto emit_u32 = [&](u32 value) {
  41. builder.appendf("%c%c%c%c",
  42. value & 0xff,
  43. (value >> 8) & 0xff,
  44. (value >> 16) & 0xff,
  45. (value >> 24) & 0xff);
  46. };
  47. emit_u32(cpuid.ebx());
  48. emit_u32(cpuid.edx());
  49. emit_u32(cpuid.ecx());
  50. m_cpuid = builder.build();
  51. }
  52. {
  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. {
  72. // FIXME: Check first that this is supported by calling CPUID with eax=0x80000000
  73. // and verifying that the returned eax>=0x80000004.
  74. alignas(u32) char buffer[48];
  75. u32* bufptr = reinterpret_cast<u32*>(buffer);
  76. auto copy_brand_string_part_to_buffer = [&](u32 i) {
  77. CPUID cpuid(0x80000002 + i);
  78. *bufptr++ = cpuid.eax();
  79. *bufptr++ = cpuid.ebx();
  80. *bufptr++ = cpuid.ecx();
  81. *bufptr++ = cpuid.edx();
  82. };
  83. copy_brand_string_part_to_buffer(0);
  84. copy_brand_string_part_to_buffer(1);
  85. copy_brand_string_part_to_buffer(2);
  86. m_brandstr = buffer;
  87. }
  88. // Cache the CPU feature string
  89. m_features = m_processor.features_string();
  90. }
  91. }
  92. #include <AK/String.h>