PhysicalRegion.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/BuiltinWrappers.h>
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/RefPtr.h>
  9. #include <Kernel/Assertions.h>
  10. #include <Kernel/Memory/MemoryManager.h>
  11. #include <Kernel/Memory/PhysicalRegion.h>
  12. #include <Kernel/Memory/PhysicalZone.h>
  13. #include <Kernel/Random.h>
  14. namespace Kernel::Memory {
  15. static constexpr u32 next_power_of_two(u32 value)
  16. {
  17. value--;
  18. value |= value >> 1;
  19. value |= value >> 2;
  20. value |= value >> 4;
  21. value |= value >> 8;
  22. value |= value >> 16;
  23. value++;
  24. return value;
  25. }
  26. PhysicalRegion::~PhysicalRegion()
  27. {
  28. }
  29. PhysicalRegion::PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper)
  30. : m_lower(lower)
  31. , m_upper(upper)
  32. {
  33. m_pages = (m_upper.get() - m_lower.get()) / PAGE_SIZE;
  34. }
  35. void PhysicalRegion::initialize_zones()
  36. {
  37. size_t remaining_pages = m_pages;
  38. auto base_address = m_lower;
  39. auto make_zones = [&](size_t zone_size) -> size_t {
  40. size_t pages_per_zone = zone_size / PAGE_SIZE;
  41. size_t zone_count = 0;
  42. auto first_address = base_address;
  43. while (remaining_pages >= pages_per_zone) {
  44. m_zones.append(make<PhysicalZone>(base_address, pages_per_zone));
  45. base_address = base_address.offset(pages_per_zone * PAGE_SIZE);
  46. m_usable_zones.append(m_zones.last());
  47. remaining_pages -= pages_per_zone;
  48. ++zone_count;
  49. }
  50. if (zone_count)
  51. dmesgln(" * {}x PhysicalZone ({} MiB) @ {:016x}-{:016x}", zone_count, pages_per_zone / 256, first_address.get(), base_address.get() - pages_per_zone * PAGE_SIZE - 1);
  52. return zone_count;
  53. };
  54. // First make 16 MiB zones (with 4096 pages each)
  55. m_large_zones = make_zones(large_zone_size);
  56. // Then divide any remaining space into 1 MiB zones (with 256 pages each)
  57. m_small_zones = make_zones(small_zone_size);
  58. }
  59. OwnPtr<PhysicalRegion> PhysicalRegion::try_take_pages_from_beginning(unsigned page_count)
  60. {
  61. VERIFY(page_count > 0);
  62. VERIFY(page_count < m_pages);
  63. auto taken_lower = m_lower;
  64. auto taken_upper = taken_lower.offset((PhysicalPtr)page_count * PAGE_SIZE);
  65. m_lower = m_lower.offset((PhysicalPtr)page_count * PAGE_SIZE);
  66. m_pages = (m_upper.get() - m_lower.get()) / PAGE_SIZE;
  67. return try_create(taken_lower, taken_upper);
  68. }
  69. NonnullRefPtrVector<PhysicalPage> PhysicalRegion::take_contiguous_free_pages(size_t count)
  70. {
  71. auto rounded_page_count = next_power_of_two(count);
  72. auto order = count_trailing_zeroes(rounded_page_count);
  73. Optional<PhysicalAddress> page_base;
  74. for (auto& zone : m_usable_zones) {
  75. page_base = zone.allocate_block(order);
  76. if (page_base.has_value()) {
  77. if (zone.is_empty()) {
  78. // We've exhausted this zone, move it to the full zones list.
  79. m_full_zones.append(zone);
  80. }
  81. break;
  82. }
  83. }
  84. if (!page_base.has_value())
  85. return {};
  86. NonnullRefPtrVector<PhysicalPage> physical_pages;
  87. physical_pages.ensure_capacity(count);
  88. for (size_t i = 0; i < count; ++i)
  89. physical_pages.append(PhysicalPage::create(page_base.value().offset(i * PAGE_SIZE)));
  90. return physical_pages;
  91. }
  92. RefPtr<PhysicalPage> PhysicalRegion::take_free_page()
  93. {
  94. if (m_usable_zones.is_empty())
  95. return nullptr;
  96. auto& zone = *m_usable_zones.first();
  97. auto page = zone.allocate_block(0);
  98. VERIFY(page.has_value());
  99. if (zone.is_empty()) {
  100. // We've exhausted this zone, move it to the full zones list.
  101. m_full_zones.append(zone);
  102. }
  103. return PhysicalPage::create(page.value());
  104. }
  105. void PhysicalRegion::return_page(PhysicalAddress paddr)
  106. {
  107. auto large_zone_base = lower().get();
  108. auto small_zone_base = lower().get() + (m_large_zones * large_zone_size);
  109. size_t zone_index;
  110. if (paddr.get() < small_zone_base)
  111. zone_index = (paddr.get() - large_zone_base) / large_zone_size;
  112. else
  113. zone_index = m_large_zones + (paddr.get() - small_zone_base) / small_zone_size;
  114. auto& zone = m_zones[zone_index];
  115. VERIFY(zone.contains(paddr));
  116. zone.deallocate_block(paddr, 0);
  117. if (m_full_zones.contains(zone))
  118. m_usable_zones.append(zone);
  119. }
  120. }