page_allocator_internals_fuchsia.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // This file implements memory allocation primitives for PageAllocator using
  6. // Fuchsia's VMOs (Virtual Memory Objects). VMO API is documented in
  7. // https://fuchsia.dev/fuchsia-src/zircon/objects/vm_object . A VMO is a kernel
  8. // object that corresponds to a set of memory pages. VMO pages may be mapped
  9. // to an address space. The code below creates VMOs for each memory allocations
  10. // and maps them to the default address space of the current process.
  11. #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_INTERNALS_FUCHSIA_H_
  12. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_INTERNALS_FUCHSIA_H_
  13. #include <lib/zx/vmar.h>
  14. #include <lib/zx/vmo.h>
  15. #include <cstdint>
  16. #include "base/allocator/partition_allocator/page_allocator.h"
  17. #include "base/allocator/partition_allocator/partition_alloc_base/fuchsia/fuchsia_logging.h"
  18. #include "base/allocator/partition_allocator/partition_alloc_check.h"
  19. #include "base/allocator/partition_allocator/partition_alloc_notreached.h"
  20. namespace partition_alloc::internal {
  21. namespace {
  22. // Returns VMO name for a PageTag.
  23. const char* PageTagToName(PageTag tag) {
  24. switch (tag) {
  25. case PageTag::kBlinkGC:
  26. return "cr_blink_gc";
  27. case PageTag::kPartitionAlloc:
  28. return "cr_partition_alloc";
  29. case PageTag::kChromium:
  30. return "cr_chromium";
  31. case PageTag::kV8:
  32. return "cr_v8";
  33. default:
  34. PA_DCHECK(false);
  35. return "";
  36. }
  37. }
  38. zx_vm_option_t PageAccessibilityToZxVmOptions(
  39. PageAccessibilityConfiguration accessibility) {
  40. switch (accessibility) {
  41. case PageAccessibilityConfiguration::kRead:
  42. return ZX_VM_PERM_READ;
  43. case PageAccessibilityConfiguration::kReadWrite:
  44. case PageAccessibilityConfiguration::kReadWriteTagged:
  45. return ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
  46. case PageAccessibilityConfiguration::kReadExecuteProtected:
  47. case PageAccessibilityConfiguration::kReadExecute:
  48. return ZX_VM_PERM_READ | ZX_VM_PERM_EXECUTE;
  49. case PageAccessibilityConfiguration::kReadWriteExecute:
  50. return ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_PERM_EXECUTE;
  51. default:
  52. PA_NOTREACHED();
  53. [[fallthrough]];
  54. case PageAccessibilityConfiguration::kInaccessible:
  55. return 0;
  56. }
  57. }
  58. } // namespace
  59. // zx_vmar_map() will fail if the VMO cannot be mapped at |vmar_offset|, i.e.
  60. // |hint| is not advisory.
  61. constexpr bool kHintIsAdvisory = false;
  62. std::atomic<int32_t> s_allocPageErrorCode{0};
  63. uintptr_t SystemAllocPagesInternal(uintptr_t hint,
  64. size_t length,
  65. PageAccessibilityConfiguration accessibility,
  66. PageTag page_tag) {
  67. zx::vmo vmo;
  68. zx_status_t status = zx::vmo::create(length, 0, &vmo);
  69. if (status != ZX_OK) {
  70. PA_ZX_DLOG(INFO, status) << "zx_vmo_create";
  71. return 0;
  72. }
  73. const char* vmo_name = PageTagToName(page_tag);
  74. status = vmo.set_property(ZX_PROP_NAME, vmo_name, strlen(vmo_name));
  75. // VMO names are used only for debugging, so failure to set a name is not
  76. // fatal.
  77. PA_ZX_DCHECK(status == ZX_OK, status);
  78. if (page_tag == PageTag::kV8) {
  79. // V8 uses JIT. Call zx_vmo_replace_as_executable() to allow code execution
  80. // in the new VMO.
  81. status = vmo.replace_as_executable(zx::resource(), &vmo);
  82. if (status != ZX_OK) {
  83. PA_ZX_DLOG(INFO, status) << "zx_vmo_replace_as_executable";
  84. return 0;
  85. }
  86. }
  87. zx_vm_option_t options = PageAccessibilityToZxVmOptions(accessibility);
  88. uint64_t vmar_offset = 0;
  89. if (hint) {
  90. vmar_offset = hint;
  91. options |= ZX_VM_SPECIFIC;
  92. }
  93. uint64_t address;
  94. status =
  95. zx::vmar::root_self()->map(options, vmar_offset, vmo,
  96. /*vmo_offset=*/0, length, &address);
  97. if (status != ZX_OK) {
  98. // map() is expected to fail if |hint| is set to an already-in-use location.
  99. if (!hint) {
  100. PA_ZX_DLOG(ERROR, status) << "zx_vmar_map";
  101. }
  102. return 0;
  103. }
  104. return address;
  105. }
  106. uintptr_t TrimMappingInternal(uintptr_t base_address,
  107. size_t base_length,
  108. size_t trim_length,
  109. PageAccessibilityConfiguration accessibility,
  110. size_t pre_slack,
  111. size_t post_slack) {
  112. PA_DCHECK(base_length == trim_length + pre_slack + post_slack);
  113. // Unmap head if necessary.
  114. if (pre_slack) {
  115. zx_status_t status = zx::vmar::root_self()->unmap(base_address, pre_slack);
  116. PA_ZX_CHECK(status == ZX_OK, status);
  117. }
  118. // Unmap tail if necessary.
  119. if (post_slack) {
  120. zx_status_t status = zx::vmar::root_self()->unmap(
  121. base_address + pre_slack + trim_length, post_slack);
  122. PA_ZX_CHECK(status == ZX_OK, status);
  123. }
  124. return base_address + pre_slack;
  125. }
  126. bool TrySetSystemPagesAccessInternal(
  127. uint64_t address,
  128. size_t length,
  129. PageAccessibilityConfiguration accessibility) {
  130. zx_status_t status = zx::vmar::root_self()->protect(
  131. PageAccessibilityToZxVmOptions(accessibility), address, length);
  132. return status == ZX_OK;
  133. }
  134. void SetSystemPagesAccessInternal(
  135. uint64_t address,
  136. size_t length,
  137. PageAccessibilityConfiguration accessibility) {
  138. zx_status_t status = zx::vmar::root_self()->protect(
  139. PageAccessibilityToZxVmOptions(accessibility), address, length);
  140. PA_ZX_CHECK(status == ZX_OK, status);
  141. }
  142. void FreePagesInternal(uint64_t address, size_t length) {
  143. zx_status_t status = zx::vmar::root_self()->unmap(address, length);
  144. PA_ZX_CHECK(status == ZX_OK, status);
  145. }
  146. void DiscardSystemPagesInternal(uint64_t address, size_t length) {
  147. // TODO(https://crbug.com/1022062): Mark pages as discardable, rather than
  148. // forcibly de-committing them immediately, when Fuchsia supports it.
  149. zx_status_t status = zx::vmar::root_self()->op_range(
  150. ZX_VMO_OP_DECOMMIT, address, length, nullptr, 0);
  151. PA_ZX_CHECK(status == ZX_OK, status);
  152. }
  153. void DecommitSystemPagesInternal(
  154. uint64_t address,
  155. size_t length,
  156. PageAccessibilityDisposition accessibility_disposition) {
  157. if (accessibility_disposition ==
  158. PageAccessibilityDisposition::kRequireUpdate) {
  159. SetSystemPagesAccess(address, length,
  160. PageAccessibilityConfiguration::kInaccessible);
  161. }
  162. // TODO(https://crbug.com/1022062): Review whether this implementation is
  163. // still appropriate once DiscardSystemPagesInternal() migrates to a "lazy"
  164. // discardable API.
  165. DiscardSystemPagesInternal(address, length);
  166. }
  167. void DecommitAndZeroSystemPagesInternal(uintptr_t address, size_t length) {
  168. SetSystemPagesAccess(address, length,
  169. PageAccessibilityConfiguration::kInaccessible);
  170. // TODO(https://crbug.com/1022062): this implementation will likely no longer
  171. // be appropriate once DiscardSystemPagesInternal() migrates to a "lazy"
  172. // discardable API.
  173. DiscardSystemPagesInternal(address, length);
  174. }
  175. void RecommitSystemPagesInternal(
  176. uintptr_t address,
  177. size_t length,
  178. PageAccessibilityConfiguration accessibility,
  179. PageAccessibilityDisposition accessibility_disposition) {
  180. // On Fuchsia systems, the caller needs to simply read the memory to recommit
  181. // it. However, if decommit changed the permissions, recommit has to change
  182. // them back.
  183. if (accessibility_disposition ==
  184. PageAccessibilityDisposition::kRequireUpdate) {
  185. SetSystemPagesAccess(address, length, accessibility);
  186. }
  187. }
  188. bool TryRecommitSystemPagesInternal(
  189. uintptr_t address,
  190. size_t length,
  191. PageAccessibilityConfiguration accessibility,
  192. PageAccessibilityDisposition accessibility_disposition) {
  193. // On Fuchsia systems, the caller needs to simply read the memory to recommit
  194. // it. However, if decommit changed the permissions, recommit has to change
  195. // them back.
  196. if (accessibility_disposition ==
  197. PageAccessibilityDisposition::kRequireUpdate) {
  198. return TrySetSystemPagesAccess(address, length, accessibility);
  199. }
  200. return true;
  201. }
  202. } // namespace partition_alloc::internal
  203. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_INTERNALS_FUCHSIA_H_