| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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(HOST_OS_LINUX) | 6 #if defined(HOST_OS_LINUX) |
| 7 | 7 |
| 8 #include "vm/virtual_memory.h" | 8 #include "vm/virtual_memory.h" |
| 9 | 9 |
| 10 #include <sys/mman.h> // NOLINT | 10 #include <sys/mman.h> // NOLINT |
| 11 #include <unistd.h> // NOLINT | 11 #include <unistd.h> // NOLINT |
| 12 | 12 |
| 13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
| 14 #include "platform/utils.h" | 14 #include "platform/utils.h" |
| 15 | 15 |
| 16 #include "vm/isolate.h" | 16 #include "vm/isolate.h" |
| 17 | 17 |
| 18 namespace dart { | 18 namespace dart { |
| 19 | 19 |
| 20 // standard MAP_FAILED causes "error: use of old-style cast" as it | 20 // standard MAP_FAILED causes "error: use of old-style cast" as it |
| 21 // defines MAP_FAILED as ((void *) -1) | 21 // defines MAP_FAILED as ((void *) -1) |
| 22 #undef MAP_FAILED | 22 #undef MAP_FAILED |
| 23 #define MAP_FAILED reinterpret_cast<void*>(-1) | 23 #define MAP_FAILED reinterpret_cast<void*>(-1) |
| 24 | 24 |
| 25 uword VirtualMemory::page_size_ = 0; | 25 uword VirtualMemory::page_size_ = 0; |
| 26 | 26 |
| 27 | |
| 28 void VirtualMemory::InitOnce() { | 27 void VirtualMemory::InitOnce() { |
| 29 page_size_ = getpagesize(); | 28 page_size_ = getpagesize(); |
| 30 } | 29 } |
| 31 | 30 |
| 32 | |
| 33 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { | 31 VirtualMemory* VirtualMemory::ReserveInternal(intptr_t size) { |
| 34 void* address = mmap(NULL, size, PROT_NONE, | 32 void* address = mmap(NULL, size, PROT_NONE, |
| 35 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); | 33 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0); |
| 36 if (address == MAP_FAILED) { | 34 if (address == MAP_FAILED) { |
| 37 return NULL; | 35 return NULL; |
| 38 } | 36 } |
| 39 MemoryRegion region(address, size); | 37 MemoryRegion region(address, size); |
| 40 return new VirtualMemory(region); | 38 return new VirtualMemory(region); |
| 41 } | 39 } |
| 42 | 40 |
| 43 | |
| 44 static void unmap(void* address, intptr_t size) { | 41 static void unmap(void* address, intptr_t size) { |
| 45 if (size == 0) { | 42 if (size == 0) { |
| 46 return; | 43 return; |
| 47 } | 44 } |
| 48 | 45 |
| 49 if (munmap(address, size) != 0) { | 46 if (munmap(address, size) != 0) { |
| 50 FATAL("munmap failed\n"); | 47 FATAL("munmap failed\n"); |
| 51 } | 48 } |
| 52 } | 49 } |
| 53 | 50 |
| 54 | |
| 55 VirtualMemory::~VirtualMemory() { | 51 VirtualMemory::~VirtualMemory() { |
| 56 if (vm_owns_region()) { | 52 if (vm_owns_region()) { |
| 57 unmap(address(), reserved_size_); | 53 unmap(address(), reserved_size_); |
| 58 } | 54 } |
| 59 } | 55 } |
| 60 | 56 |
| 61 | |
| 62 bool VirtualMemory::FreeSubSegment(int32_t handle, | 57 bool VirtualMemory::FreeSubSegment(int32_t handle, |
| 63 void* address, | 58 void* address, |
| 64 intptr_t size) { | 59 intptr_t size) { |
| 65 unmap(address, size); | 60 unmap(address, size); |
| 66 return true; | 61 return true; |
| 67 } | 62 } |
| 68 | 63 |
| 69 | |
| 70 bool VirtualMemory::Commit(uword addr, | 64 bool VirtualMemory::Commit(uword addr, |
| 71 intptr_t size, | 65 intptr_t size, |
| 72 bool executable, | 66 bool executable, |
| 73 const char* name) { | 67 const char* name) { |
| 74 ASSERT(Contains(addr)); | 68 ASSERT(Contains(addr)); |
| 75 ASSERT(Contains(addr + size) || (addr + size == end())); | 69 ASSERT(Contains(addr + size) || (addr + size == end())); |
| 76 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); | 70 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); |
| 77 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, | 71 void* address = mmap(reinterpret_cast<void*>(addr), size, prot, |
| 78 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); | 72 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); |
| 79 if (address == MAP_FAILED) { | 73 if (address == MAP_FAILED) { |
| 80 return false; | 74 return false; |
| 81 } | 75 } |
| 82 return true; | 76 return true; |
| 83 } | 77 } |
| 84 | 78 |
| 85 | |
| 86 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { | 79 bool VirtualMemory::Protect(void* address, intptr_t size, Protection mode) { |
| 87 ASSERT(Thread::Current()->IsMutatorThread() || | 80 ASSERT(Thread::Current()->IsMutatorThread() || |
| 88 Isolate::Current()->mutator_thread()->IsAtSafepoint()); | 81 Isolate::Current()->mutator_thread()->IsAtSafepoint()); |
| 89 uword start_address = reinterpret_cast<uword>(address); | 82 uword start_address = reinterpret_cast<uword>(address); |
| 90 uword end_address = start_address + size; | 83 uword end_address = start_address + size; |
| 91 uword page_address = Utils::RoundDown(start_address, PageSize()); | 84 uword page_address = Utils::RoundDown(start_address, PageSize()); |
| 92 int prot = 0; | 85 int prot = 0; |
| 93 switch (mode) { | 86 switch (mode) { |
| 94 case kNoAccess: | 87 case kNoAccess: |
| 95 prot = PROT_NONE; | 88 prot = PROT_NONE; |
| 96 break; | 89 break; |
| 97 case kReadOnly: | 90 case kReadOnly: |
| 98 prot = PROT_READ; | 91 prot = PROT_READ; |
| 99 break; | 92 break; |
| 100 case kReadWrite: | 93 case kReadWrite: |
| 101 prot = PROT_READ | PROT_WRITE; | 94 prot = PROT_READ | PROT_WRITE; |
| 102 break; | 95 break; |
| 103 case kReadExecute: | 96 case kReadExecute: |
| 104 prot = PROT_READ | PROT_EXEC; | 97 prot = PROT_READ | PROT_EXEC; |
| 105 break; | 98 break; |
| 106 case kReadWriteExecute: | 99 case kReadWriteExecute: |
| 107 prot = PROT_READ | PROT_WRITE | PROT_EXEC; | 100 prot = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 108 break; | 101 break; |
| 109 } | 102 } |
| 110 return (mprotect(reinterpret_cast<void*>(page_address), | 103 return (mprotect(reinterpret_cast<void*>(page_address), |
| 111 end_address - page_address, prot) == 0); | 104 end_address - page_address, prot) == 0); |
| 112 } | 105 } |
| 113 | 106 |
| 114 | |
| 115 } // namespace dart | 107 } // namespace dart |
| 116 | 108 |
| 117 #endif // defined(HOST_OS_LINUX) | 109 #endif // defined(HOST_OS_LINUX) |
| OLD | NEW |