OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/globals.h" | 5 #include "vm/globals.h" |
6 #if defined(TARGET_OS_FUCHSIA) | 6 #if defined(TARGET_OS_FUCHSIA) |
7 | 7 |
8 #include "vm/virtual_memory.h" | 8 #include "vm/virtual_memory.h" |
9 | 9 |
| 10 #include <magenta/status.h> |
10 #include <magenta/syscalls.h> | 11 #include <magenta/syscalls.h> |
11 #include <unistd.h> // NOLINT | 12 #include <sys/mman.h> |
| 13 #include <unistd.h> |
12 | 14 |
13 #include "platform/assert.h" | 15 #include "platform/assert.h" |
| 16 #include "vm/isolate.h" |
14 #include "vm/memory_region.h" | 17 #include "vm/memory_region.h" |
15 #include "vm/os.h" | 18 #include "vm/os.h" |
16 | 19 |
| 20 // #define VIRTUAL_MEMORY_LOGGING 1 |
| 21 #if defined(VIRTUAL_MEMORY_LOGGING) |
| 22 #define LOG_ERR(msg, ...) \ |
| 23 OS::PrintErr("VMVM: %s:%d: " msg, __FILE__, __LINE__, ##__VA_ARGS__) |
| 24 #define LOG_INFO(msg, ...) \ |
| 25 OS::Print("VMVM: %s:%d: " msg, __FILE__, __LINE__, ##__VA_ARGS__) |
| 26 #else |
| 27 #define LOG_ERR(msg, ...) |
| 28 #define LOG_INFO(msg, ...) |
| 29 #endif // defined(VIRTUAL_MEMORY_LOGGING) |
| 30 |
17 namespace dart { | 31 namespace dart { |
18 | 32 |
19 uword VirtualMemory::page_size_ = 0; | 33 uword VirtualMemory::page_size_ = 0; |
20 | 34 |
21 | 35 |
22 void VirtualMemory::InitOnce() { | 36 void VirtualMemory::InitOnce() { |
23 page_size_ = getpagesize(); | 37 page_size_ = getpagesize(); |
24 } | 38 } |
25 | 39 |
26 | 40 |
27 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { | 41 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { |
28 mx_handle_t vmo = MX_HANDLE_INVALID; | 42 mx_handle_t vmar = MX_HANDLE_INVALID; |
29 mx_status_t status = mx_vmo_create(size, 0u, &vmo); | 43 uword addr = 0; |
| 44 const uint32_t flags = MX_VM_FLAG_COMPACT | MX_VM_FLAG_CAN_MAP_SPECIFIC | |
| 45 MX_VM_FLAG_CAN_MAP_READ | MX_VM_FLAG_CAN_MAP_WRITE | |
| 46 MX_VM_FLAG_CAN_MAP_EXECUTE; |
| 47 mx_status_t status = |
| 48 mx_vmar_allocate(mx_vmar_root_self(), 0, size, flags, &vmar, &addr); |
30 if (status != NO_ERROR) { | 49 if (status != NO_ERROR) { |
31 return NULL; | 50 LOG_ERR("mx_vmar_allocate(size = %ld) failed: %s\n", size, |
32 } | 51 mx_status_get_string(status)); |
33 | |
34 // TODO(zra): map with PERM_NONE, when that works, and relax with | |
35 // Commit and Protect when they are implemented. | |
36 // Issue MG-161. | |
37 const int prot = | |
38 MX_VM_FLAG_PERM_READ | MX_VM_FLAG_PERM_WRITE | MX_VM_FLAG_PERM_EXECUTE; | |
39 uintptr_t addr; | |
40 status = mx_vmar_map(mx_vmar_root_self(), 0, vmo, 0, size, prot, &addr); | |
41 if (status != NO_ERROR) { | |
42 mx_handle_close(vmo); | |
43 FATAL("VirtualMemory::ReserveInternal FAILED"); | |
44 return NULL; | 52 return NULL; |
45 } | 53 } |
46 | 54 |
47 MemoryRegion region(reinterpret_cast<void*>(addr), size); | 55 MemoryRegion region(reinterpret_cast<void*>(addr), size); |
48 return new VirtualMemory(region, vmo); | 56 return new VirtualMemory(region, vmar); |
49 } | 57 } |
50 | 58 |
51 | 59 |
52 VirtualMemory::~VirtualMemory() { | 60 VirtualMemory::~VirtualMemory() { |
53 if (!embedder_allocated()) { | 61 if (!embedder_allocated()) { |
54 // TODO(zra): Use reserved_size_. | 62 mx_handle_t vmar = static_cast<mx_handle_t>(handle()); |
55 // Issue MG-162. | 63 mx_status_t status = mx_vmar_destroy(vmar); |
56 uintptr_t addr = reinterpret_cast<uintptr_t>(address()); | |
57 mx_status_t status = | |
58 mx_vmar_unmap(mx_vmar_root_self(), addr, 0 /*reserved_size_*/); | |
59 if (status != NO_ERROR) { | 64 if (status != NO_ERROR) { |
60 FATAL("VirtualMemory::~VirtualMemory: unamp FAILED"); | 65 LOG_ERR("mx_vmar_destroy failed: %s\n", mx_status_get_string(status)); |
61 } | 66 } |
62 | 67 status = mx_handle_close(vmar); |
63 status = mx_handle_close(handle()); | |
64 if (status != NO_ERROR) { | 68 if (status != NO_ERROR) { |
65 FATAL("VirtualMemory::~VirtualMemory: handle_close FAILED"); | 69 LOG_ERR("mx_handle_close failed: %s\n", mx_status_get_string(status)); |
66 } | 70 } |
67 } | 71 } |
68 } | 72 } |
69 | 73 |
70 | 74 |
71 bool VirtualMemory::FreeSubSegment(void* address, intptr_t size) { | 75 bool VirtualMemory::FreeSubSegment(int32_t handle, |
72 // TODO(zra): It should be possible to free a subsegment after | 76 void* address, |
73 // Issue MG-162 is addressed. | 77 intptr_t size) { |
74 return false; | 78 mx_handle_t vmar = static_cast<mx_handle_t>(handle); |
| 79 mx_status_t status = |
| 80 mx_vmar_unmap(vmar, reinterpret_cast<uintptr_t>(address), size); |
| 81 if (status != NO_ERROR) { |
| 82 LOG_ERR("mx_vmar_unmap failed: %s\n", mx_status_get_string(status)); |
| 83 return false; |
| 84 } |
| 85 return true; |
75 } | 86 } |
76 | 87 |
77 | 88 |
78 bool VirtualMemory::Commit(uword addr, intptr_t size, bool executable) { | 89 bool VirtualMemory::Commit(uword addr, intptr_t size, bool executable) { |
79 // TODO(zra): Implement when the protections for a mapping can be changed. | 90 ASSERT(Contains(addr)); |
80 // Issue MG-133. | 91 ASSERT(Contains(addr + size) || (addr + size == end())); |
| 92 mx_handle_t vmo = MX_HANDLE_INVALID; |
| 93 mx_status_t status = mx_vmo_create(size, 0u, &vmo); |
| 94 if (status != NO_ERROR) { |
| 95 LOG_ERR("mx_vmo_create(%ld) failed: %s\n", size, |
| 96 mx_status_get_string(status)); |
| 97 return false; |
| 98 } |
| 99 |
| 100 mx_handle_t vmar = static_cast<mx_handle_t>(handle()); |
| 101 const size_t offset = addr - start(); |
| 102 const uint32_t flags = MX_VM_FLAG_SPECIFIC | MX_VM_FLAG_PERM_READ | |
| 103 MX_VM_FLAG_PERM_WRITE | MX_VM_FLAG_PERM_EXECUTE; |
| 104 uintptr_t mapped_addr; |
| 105 status = mx_vmar_map(vmar, offset, vmo, 0, size, flags, &mapped_addr); |
| 106 if (status != NO_ERROR) { |
| 107 mx_handle_close(vmo); |
| 108 LOG_ERR("mx_vmar_map(%ld, %ld, %u) failed: %s\n", offset, size, flags, |
| 109 mx_status_get_string(status)); |
| 110 return false; |
| 111 } |
| 112 if (addr != mapped_addr) { |
| 113 LOG_ERR("mx_vmar_map: addr != mapped_addr: %lx != %lx\n", addr, |
| 114 mapped_addr); |
| 115 return false; |
| 116 } |
81 return true; | 117 return true; |
82 } | 118 } |
83 | 119 |
84 | 120 |
85 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { | 121 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
86 // TODO(zra): Implement when Fuchsia has an mprotect-like call. | |
87 return true; | 122 return true; |
88 } | 123 } |
89 | 124 |
90 } // namespace dart | 125 } // namespace dart |
91 | 126 |
92 #endif // defined(TARGET_OS_FUCHSIA) | 127 #endif // defined(TARGET_OS_FUCHSIA) |
OLD | NEW |