CPUInfo.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObjectSerializer.h>
  7. #if ARCH(X86_64)
  8. # include <Kernel/Arch/x86_64/ProcessorInfo.h>
  9. #endif
  10. #include <Kernel/FileSystem/SysFS/Subsystems/Kernel/CPUInfo.h>
  11. #include <Kernel/Sections.h>
  12. namespace Kernel {
  13. UNMAP_AFTER_INIT SysFSCPUInformation::SysFSCPUInformation(SysFSDirectory const& parent_directory)
  14. : SysFSGlobalInformation(parent_directory)
  15. {
  16. }
  17. UNMAP_AFTER_INIT NonnullLockRefPtr<SysFSCPUInformation> SysFSCPUInformation::must_create(SysFSDirectory const& parent_directory)
  18. {
  19. return adopt_lock_ref_if_nonnull(new (nothrow) SysFSCPUInformation(parent_directory)).release_nonnull();
  20. }
  21. ErrorOr<void> SysFSCPUInformation::try_generate(KBufferBuilder& builder)
  22. {
  23. #if ARCH(X86_64)
  24. auto array = TRY(JsonArraySerializer<>::try_create(builder));
  25. TRY(Processor::try_for_each(
  26. [&](Processor& proc) -> ErrorOr<void> {
  27. auto& info = proc.info();
  28. auto obj = TRY(array.add_object());
  29. TRY(obj.add("processor"sv, proc.id()));
  30. TRY(obj.add("vendor_id"sv, info.vendor_id_string()));
  31. TRY(obj.add("family"sv, info.display_family()));
  32. if (!info.hypervisor_vendor_id_string().is_null())
  33. TRY(obj.add("hypervisor_vendor_id"sv, info.hypervisor_vendor_id_string()));
  34. auto features_array = TRY(obj.add_array("features"sv));
  35. auto keep_empty = SplitBehavior::KeepEmpty;
  36. ErrorOr<void> result; // FIXME: Make this nicer
  37. info.features_string().for_each_split_view(' ', keep_empty, [&](StringView feature) {
  38. if (result.is_error())
  39. return;
  40. result = features_array.add(feature);
  41. });
  42. TRY(result);
  43. TRY(features_array.finish());
  44. TRY(obj.add("model"sv, info.display_model()));
  45. TRY(obj.add("stepping"sv, info.stepping()));
  46. TRY(obj.add("type"sv, info.type()));
  47. TRY(obj.add("brand"sv, info.brand_string()));
  48. auto caches = TRY(obj.add_object("caches"sv));
  49. auto add_cache_info = [&](StringView name, ProcessorInfo::Cache const& cache) -> ErrorOr<void> {
  50. auto cache_object = TRY(caches.add_object(name));
  51. TRY(cache_object.add("size"sv, cache.size));
  52. TRY(cache_object.add("line_size"sv, cache.line_size));
  53. TRY(cache_object.finish());
  54. return {};
  55. };
  56. if (info.l1_data_cache().has_value())
  57. TRY(add_cache_info("l1_data"sv, *info.l1_data_cache()));
  58. if (info.l1_instruction_cache().has_value())
  59. TRY(add_cache_info("l1_instruction"sv, *info.l1_instruction_cache()));
  60. if (info.l2_cache().has_value())
  61. TRY(add_cache_info("l2"sv, *info.l2_cache()));
  62. if (info.l3_cache().has_value())
  63. TRY(add_cache_info("l3"sv, *info.l3_cache()));
  64. TRY(caches.finish());
  65. TRY(obj.finish());
  66. return {};
  67. }));
  68. TRY(array.finish());
  69. return {};
  70. #elif ARCH(AARCH64)
  71. (void)builder;
  72. dmesgln("TODO: Implement ProcessorInfo for AArch64!");
  73. return Error::from_errno(EINVAL);
  74. #else
  75. # error Unknown architecture
  76. #endif
  77. }
  78. }