Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/os/bsd/gc/z/zPhysicalMemoryBacking_bsd.cpp
66645 views
1
/*
2
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include "precompiled.hpp"
25
#include "gc/shared/gcLogPrecious.hpp"
26
#include "gc/z/zErrno.hpp"
27
#include "gc/z/zGlobals.hpp"
28
#include "gc/z/zLargePages.inline.hpp"
29
#include "gc/z/zPhysicalMemory.inline.hpp"
30
#include "gc/z/zPhysicalMemoryBacking_bsd.hpp"
31
#include "logging/log.hpp"
32
#include "runtime/globals.hpp"
33
#include "runtime/os.hpp"
34
#include "utilities/align.hpp"
35
#include "utilities/debug.hpp"
36
37
#include <TargetConditionals.h>
38
#include <mach/mach.h>
39
#if TARGET_OS_IPHONE
40
extern "C" kern_return_t mach_vm_remap(vm_map_t target_task,
41
mach_vm_address_t *target_address,
42
mach_vm_size_t size,
43
mach_vm_offset_t mask,
44
int flags,
45
vm_map_t src_task,
46
mach_vm_address_t src_address,
47
boolean_t copy,
48
vm_prot_t *cur_protection,
49
vm_prot_t *max_protection,
50
vm_inherit_t inheritance);
51
#else
52
#include <mach/mach_vm.h>
53
#endif
54
#include <sys/mman.h>
55
#include <sys/types.h>
56
57
// The backing is represented by a reserved virtual address space, in which
58
// we commit and uncommit physical memory. Multi-mapping the different heap
59
// views is done by simply remapping the backing memory using mach_vm_remap().
60
61
static int vm_flags_superpage() {
62
if (!ZLargePages::is_explicit()) {
63
return 0;
64
}
65
66
const int page_size_in_megabytes = ZGranuleSize >> 20;
67
return page_size_in_megabytes << VM_FLAGS_SUPERPAGE_SHIFT;
68
}
69
70
static ZErrno mremap(uintptr_t from_addr, uintptr_t to_addr, size_t size) {
71
mach_vm_address_t remap_addr = to_addr;
72
vm_prot_t remap_cur_prot;
73
vm_prot_t remap_max_prot;
74
75
// Remap memory to an additional location
76
const kern_return_t res = mach_vm_remap(mach_task_self(),
77
&remap_addr,
78
size,
79
0 /* mask */,
80
VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | vm_flags_superpage(),
81
mach_task_self(),
82
from_addr,
83
FALSE /* copy */,
84
&remap_cur_prot,
85
&remap_max_prot,
86
VM_INHERIT_COPY);
87
88
return (res == KERN_SUCCESS) ? ZErrno(0) : ZErrno(EINVAL);
89
}
90
91
ZPhysicalMemoryBacking::ZPhysicalMemoryBacking(size_t max_capacity) :
92
_base(0),
93
_initialized(false) {
94
95
// Reserve address space for backing memory
96
_base = (uintptr_t)os::reserve_memory(max_capacity);
97
if (_base == 0) {
98
// Failed
99
log_error_pd(gc)("Failed to reserve address space for backing memory");
100
return;
101
}
102
103
// Successfully initialized
104
_initialized = true;
105
}
106
107
bool ZPhysicalMemoryBacking::is_initialized() const {
108
return _initialized;
109
}
110
111
void ZPhysicalMemoryBacking::warn_commit_limits(size_t max_capacity) const {
112
// Does nothing
113
}
114
115
bool ZPhysicalMemoryBacking::commit_inner(size_t offset, size_t length) const {
116
assert(is_aligned(offset, os::vm_page_size()), "Invalid offset");
117
assert(is_aligned(length, os::vm_page_size()), "Invalid length");
118
119
log_trace(gc, heap)("Committing memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
120
offset / M, (offset + length) / M, length / M);
121
122
const uintptr_t addr = _base + offset;
123
const void* const res = mmap((void*)addr, length, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
124
if (res == MAP_FAILED) {
125
ZErrno err;
126
log_error(gc)("Failed to commit memory (%s)", err.to_string());
127
return false;
128
}
129
130
// Success
131
return true;
132
}
133
134
size_t ZPhysicalMemoryBacking::commit(size_t offset, size_t length) const {
135
// Try to commit the whole region
136
if (commit_inner(offset, length)) {
137
// Success
138
return length;
139
}
140
141
// Failed, try to commit as much as possible
142
size_t start = offset;
143
size_t end = offset + length;
144
145
for (;;) {
146
length = align_down((end - start) / 2, ZGranuleSize);
147
if (length == 0) {
148
// Done, don't commit more
149
return start - offset;
150
}
151
152
if (commit_inner(start, length)) {
153
// Success, try commit more
154
start += length;
155
} else {
156
// Failed, try commit less
157
end -= length;
158
}
159
}
160
}
161
162
size_t ZPhysicalMemoryBacking::uncommit(size_t offset, size_t length) const {
163
assert(is_aligned(offset, os::vm_page_size()), "Invalid offset");
164
assert(is_aligned(length, os::vm_page_size()), "Invalid length");
165
166
log_trace(gc, heap)("Uncommitting memory: " SIZE_FORMAT "M-" SIZE_FORMAT "M (" SIZE_FORMAT "M)",
167
offset / M, (offset + length) / M, length / M);
168
169
const uintptr_t start = _base + offset;
170
const void* const res = mmap((void*)start, length, PROT_NONE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
171
if (res == MAP_FAILED) {
172
ZErrno err;
173
log_error(gc)("Failed to uncommit memory (%s)", err.to_string());
174
return 0;
175
}
176
177
return length;
178
}
179
180
void ZPhysicalMemoryBacking::map(uintptr_t addr, size_t size, uintptr_t offset) const {
181
const ZErrno err = mremap(_base + offset, addr, size);
182
if (err) {
183
fatal("Failed to remap memory (%s)", err.to_string());
184
}
185
}
186
187
void ZPhysicalMemoryBacking::unmap(uintptr_t addr, size_t size) const {
188
// Note that we must keep the address space reservation intact and just detach
189
// the backing memory. For this reason we map a new anonymous, non-accessible
190
// and non-reserved page over the mapping instead of actually unmapping.
191
const void* const res = mmap((void*)addr, size, PROT_NONE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
192
if (res == MAP_FAILED) {
193
ZErrno err;
194
fatal("Failed to map memory (%s)", err.to_string());
195
}
196
}
197
198