Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0364634

Browse files
committed
third_party/marl: Implement page-based functions for Fuchsia
Mostly copypasta from: src/Reactor/ExecutableMemory.cpp Will be upstreamed to marl if the autorollers are happy. Bug: b/140546382 Change-Id: I6cf0dc8638e37f17d097ea645c9a84cef1368a3d Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/37131 Reviewed-by: Ben Clayton <bclayton@google.com> Tested-by: Ben Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
1 parent 5b0608c commit 0364634

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

third_party/marl/src/memory.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,48 @@ inline void protectPage(void* addr) {
5050
MARL_ASSERT(res == 0, "Failed to protect page at %p", addr);
5151
}
5252
} // anonymous namespace
53+
#elif defined(__Fuchsia__)
54+
#include <unistd.h>
55+
#include <zircon/process.h>
56+
#include <zircon/syscalls.h>
57+
namespace {
58+
// This was a static in pageSize(), but due to the following TSAN false-positive
59+
// bug, this has been moved out to a global.
60+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68338
61+
const size_t kPageSize = sysconf(_SC_PAGESIZE);
62+
inline size_t pageSize() {
63+
return kPageSize;
64+
}
65+
inline void* allocatePages(size_t count) {
66+
auto length = count * kPageSize;
67+
zx_handle_t vmo;
68+
if (zx_vmo_create(length, 0, &vmo) != ZX_OK) {
69+
return nullptr;
70+
}
71+
zx_vaddr_t reservation;
72+
zx_status_t status =
73+
zx_vmar_map(zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
74+
vmo, 0, length, &reservation);
75+
zx_handle_close(vmo);
76+
(void)status;
77+
MARL_ASSERT(status == ZX_OK, "Failed to allocate %d pages", int(count));
78+
return reinterpret_cast<void*>(reservation);
79+
}
80+
inline void freePages(void* ptr, size_t count) {
81+
auto length = count * kPageSize;
82+
zx_status_t status = zx_vmar_unmap(zx_vmar_root_self(),
83+
reinterpret_cast<zx_vaddr_t>(ptr), length);
84+
(void)status;
85+
MARL_ASSERT(status == ZX_OK, "Failed to free %d pages at %p", int(count),
86+
ptr);
87+
}
88+
inline void protectPage(void* addr) {
89+
zx_status_t status = zx_vmar_protect(
90+
zx_vmar_root_self(), 0, reinterpret_cast<zx_vaddr_t>(addr), kPageSize);
91+
(void)status;
92+
MARL_ASSERT(status == ZX_OK, "Failed to protect page at %p", addr);
93+
}
94+
} // anonymous namespace
5395
#elif defined(_WIN32)
5496
#define WIN32_LEAN_AND_MEAN 1
5597
#include <Windows.h>
@@ -82,7 +124,6 @@ inline void protectPage(void* addr) {
82124
}
83125
} // anonymous namespace
84126
#else
85-
// TODO: Fuchsia support
86127
#error "Page based allocation not implemented for this platform"
87128
#endif
88129

0 commit comments

Comments
 (0)