55 mach_vm_address_t remap_addr = to_addr; 56 vm_prot_t remap_cur_prot; 57 vm_prot_t remap_max_prot; 58 59 // Remap memory to an additional location 60 const kern_return_t res = mach_vm_remap(mach_task_self(), 61 &remap_addr, 62 size, 63 0 /* mask */, 64 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | vm_flags_superpage(), 65 mach_task_self(), 66 from_addr, 67 FALSE /* copy */, 68 &remap_cur_prot, 69 &remap_max_prot, 70 VM_INHERIT_COPY); 71 72 return (res == KERN_SUCCESS) ? ZErrno(0) : ZErrno(EINVAL); 73 } 74 75 ZPhysicalMemoryBacking::ZPhysicalMemoryBacking() : 76 _base(0), 77 _size(0), 78 _initialized(false) { 79 80 // Reserve address space for backing memory 81 _base = (uintptr_t)os::reserve_memory(MaxHeapSize); 82 if (_base == 0) { 83 // Failed 84 log_error(gc)("Failed to reserve address space for backing memory"); 85 return; 86 } 87 88 // Successfully initialized 89 _initialized = true; 90 } 91 92 bool ZPhysicalMemoryBacking::is_initialized() const { 93 return _initialized; 94 } 95 96 void ZPhysicalMemoryBacking::warn_commit_limits(size_t max) const { 97 // Does nothing 98 } 99 100 size_t ZPhysicalMemoryBacking::size() const { 101 return _size; 102 } 103 104 bool ZPhysicalMemoryBacking::commit_inner(size_t offset, size_t length) { 105 assert(is_aligned(offset, os::vm_page_size()), "Invalid offset"); 106 assert(is_aligned(length, os::vm_page_size()), "Invalid length"); 107 108 log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)", 109 offset / M, (offset + length) / M, length / M); 110 111 const uintptr_t addr = _base + offset; 112 const void* const res = mmap((void*)addr, length, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 113 if (res == MAP_FAILED) { 114 ZErrno err; 115 log_error(gc)("Failed to commit memory (%s)", err.to_string()); 116 return false; 117 } 118 119 const size_t end = offset + length; 120 if (end > _size) { 121 // Record new size 122 _size = end; 123 } 124 125 // Success 126 return true; 127 } 128 129 size_t ZPhysicalMemoryBacking::commit(size_t offset, size_t length) { 130 // Try to commit the whole region 131 if (commit_inner(offset, length)) { 132 // Success 133 return length; 134 } 135 136 // Failed, try to commit as much as possible 137 size_t start = offset; 138 size_t end = offset + length; 139 140 for (;;) { 141 length = align_down((end - start) / 2, ZGranuleSize); 142 if (length == 0) { | 55 mach_vm_address_t remap_addr = to_addr; 56 vm_prot_t remap_cur_prot; 57 vm_prot_t remap_max_prot; 58 59 // Remap memory to an additional location 60 const kern_return_t res = mach_vm_remap(mach_task_self(), 61 &remap_addr, 62 size, 63 0 /* mask */, 64 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | vm_flags_superpage(), 65 mach_task_self(), 66 from_addr, 67 FALSE /* copy */, 68 &remap_cur_prot, 69 &remap_max_prot, 70 VM_INHERIT_COPY); 71 72 return (res == KERN_SUCCESS) ? ZErrno(0) : ZErrno(EINVAL); 73 } 74 75 ZPhysicalMemoryBacking::ZPhysicalMemoryBacking(size_t max_capacity) : 76 _base(0), 77 _initialized(false) { 78 79 // Reserve address space for backing memory 80 _base = (uintptr_t)os::reserve_memory(max_capacity); 81 if (_base == 0) { 82 // Failed 83 log_error(gc)("Failed to reserve address space for backing memory"); 84 return; 85 } 86 87 // Successfully initialized 88 _initialized = true; 89 } 90 91 bool ZPhysicalMemoryBacking::is_initialized() const { 92 return _initialized; 93 } 94 95 void ZPhysicalMemoryBacking::warn_commit_limits(size_t max) const { 96 // Does nothing 97 } 98 99 bool ZPhysicalMemoryBacking::commit_inner(size_t offset, size_t length) { 100 assert(is_aligned(offset, os::vm_page_size()), "Invalid offset"); 101 assert(is_aligned(length, os::vm_page_size()), "Invalid length"); 102 103 log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)", 104 offset / M, (offset + length) / M, length / M); 105 106 const uintptr_t addr = _base + offset; 107 const void* const res = mmap((void*)addr, length, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 108 if (res == MAP_FAILED) { 109 ZErrno err; 110 log_error(gc)("Failed to commit memory (%s)", err.to_string()); 111 return false; 112 } 113 114 // Success 115 return true; 116 } 117 118 size_t ZPhysicalMemoryBacking::commit(size_t offset, size_t length) { 119 // Try to commit the whole region 120 if (commit_inner(offset, length)) { 121 // Success 122 return length; 123 } 124 125 // Failed, try to commit as much as possible 126 size_t start = offset; 127 size_t end = offset + length; 128 129 for (;;) { 130 length = align_down((end - start) / 2, ZGranuleSize); 131 if (length == 0) { |