1 /*
  2  * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "gc/shared/gcLogPrecious.hpp"
 28 #include "gc/shared/gc_globals.hpp"
 29 #include "gc/z/zGlobals.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "runtime/os.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 #include "utilities/powerOfTwo.hpp"
 34 
 35 #ifdef LINUX
 36 #include <sys/mman.h>
 37 #endif // LINUX
 38 
 39 //
 40 // The heap can have three different layouts, depending on the max heap size.
 41 //
 42 // Address Space & Pointer Layout 1
 43 // --------------------------------
 44 //
 45 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
 46 //  .                                .
 47 //  .                                .
 48 //  .                                .
 49 //  +--------------------------------+ 0x0000014000000000 (20TB)
 50 //  |         Remapped View          |
 51 //  +--------------------------------+ 0x0000010000000000 (16TB)
 52 //  .                                .
 53 //  +--------------------------------+ 0x00000c0000000000 (12TB)
 54 //  |         Marked1 View           |
 55 //  +--------------------------------+ 0x0000080000000000 (8TB)
 56 //  |         Marked0 View           |
 57 //  +--------------------------------+ 0x0000040000000000 (4TB)
 58 //  .                                .
 59 //  +--------------------------------+ 0x0000000000000000
 60 //
 61 //   6                  4 4  4 4
 62 //   3                  6 5  2 1                                             0
 63 //  +--------------------+----+-----------------------------------------------+
 64 //  |00000000 00000000 00|1111|11 11111111 11111111 11111111 11111111 11111111|
 65 //  +--------------------+----+-----------------------------------------------+
 66 //  |                    |    |
 67 //  |                    |    * 41-0 Object Offset (42-bits, 4TB address space)
 68 //  |                    |
 69 //  |                    * 45-42 Metadata Bits (4-bits)  0001 = Marked0      (Address view 4-8TB)
 70 //  |                                                    0010 = Marked1      (Address view 8-12TB)
 71 //  |                                                    0100 = Remapped     (Address view 16-20TB)
 72 //  |                                                    1000 = Finalizable  (Address view N/A)
 73 //  |
 74 //  * 63-46 Fixed (18-bits, always zero)
 75 //
 76 //
 77 // Address Space & Pointer Layout 2
 78 // --------------------------------
 79 //
 80 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
 81 //  .                                .
 82 //  .                                .
 83 //  .                                .
 84 //  +--------------------------------+ 0x0000280000000000 (40TB)
 85 //  |         Remapped View          |
 86 //  +--------------------------------+ 0x0000200000000000 (32TB)
 87 //  .                                .
 88 //  +--------------------------------+ 0x0000180000000000 (24TB)
 89 //  |         Marked1 View           |
 90 //  +--------------------------------+ 0x0000100000000000 (16TB)
 91 //  |         Marked0 View           |
 92 //  +--------------------------------+ 0x0000080000000000 (8TB)
 93 //  .                                .
 94 //  +--------------------------------+ 0x0000000000000000
 95 //
 96 //   6                 4 4  4 4
 97 //   3                 7 6  3 2                                              0
 98 //  +------------------+-----+------------------------------------------------+
 99 //  |00000000 00000000 0|1111|111 11111111 11111111 11111111 11111111 11111111|
100 //  +-------------------+----+------------------------------------------------+
101 //  |                   |    |
102 //  |                   |    * 42-0 Object Offset (43-bits, 8TB address space)
103 //  |                   |
104 //  |                   * 46-43 Metadata Bits (4-bits)  0001 = Marked0      (Address view 8-16TB)
105 //  |                                                   0010 = Marked1      (Address view 16-24TB)
106 //  |                                                   0100 = Remapped     (Address view 32-40TB)
107 //  |                                                   1000 = Finalizable  (Address view N/A)
108 //  |
109 //  * 63-47 Fixed (17-bits, always zero)
110 //
111 //
112 // Address Space & Pointer Layout 3
113 // --------------------------------
114 //
115 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
116 //  .                                .
117 //  .                                .
118 //  .                                .
119 //  +--------------------------------+ 0x0000500000000000 (80TB)
120 //  |         Remapped View          |
121 //  +--------------------------------+ 0x0000400000000000 (64TB)
122 //  .                                .
123 //  +--------------------------------+ 0x0000300000000000 (48TB)
124 //  |         Marked1 View           |
125 //  +--------------------------------+ 0x0000200000000000 (32TB)
126 //  |         Marked0 View           |
127 //  +--------------------------------+ 0x0000100000000000 (16TB)
128 //  .                                .
129 //  +--------------------------------+ 0x0000000000000000
130 //
131 //   6               4  4  4 4
132 //   3               8  7  4 3                                               0
133 //  +------------------+----+-------------------------------------------------+
134 //  |00000000 00000000 |1111|1111 11111111 11111111 11111111 11111111 11111111|
135 //  +------------------+----+-------------------------------------------------+
136 //  |                  |    |
137 //  |                  |    * 43-0 Object Offset (44-bits, 16TB address space)
138 //  |                  |
139 //  |                  * 47-44 Metadata Bits (4-bits)  0001 = Marked0      (Address view 16-32TB)
140 //  |                                                  0010 = Marked1      (Address view 32-48TB)
141 //  |                                                  0100 = Remapped     (Address view 64-80TB)
142 //  |                                                  1000 = Finalizable  (Address view N/A)
143 //  |
144 //  * 63-48 Fixed (16-bits, always zero)
145 //
146 
147 // Default value if probing is not implemented for a certain platform: 128TB
148 static const size_t DEFAULT_MAX_ADDRESS_BIT = 47;
149 // Minimum value returned, if probing fails: 64GB
150 static const size_t MINIMUM_MAX_ADDRESS_BIT = 36;
151 
152 static size_t probe_valid_max_address_bit() {
153 #ifdef LINUX
154   size_t max_address_bit = 0;
155   const size_t page_size = os::vm_page_size();
156   for (size_t i = DEFAULT_MAX_ADDRESS_BIT; i > MINIMUM_MAX_ADDRESS_BIT; --i) {
157     const uintptr_t base_addr = ((uintptr_t) 1U) << i;
158     if (msync((void*)base_addr, page_size, MS_ASYNC) == 0) {
159       // msync suceeded, the address is valid, and maybe even already mapped.
160       max_address_bit = i;
161       break;
162     }
163     if (errno != ENOMEM) {
164       // Some error occured. This should never happen, but msync
165       // has some undefined behavior, hence ignore this bit.
166 #ifdef ASSERT
167       fatal("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno));
168 #else // ASSERT
169       log_warning_p(gc)("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno));
170 #endif // ASSERT
171       continue;
172     }
173     // Since msync failed with ENOMEM, the page might not be mapped.
174     // Try to map it, to see if the address is valid.
175     void* const result_addr = mmap((void*) base_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
176     if (result_addr != MAP_FAILED) {
177       munmap(result_addr, page_size);
178     }
179     if ((uintptr_t) result_addr == base_addr) {
180       // address is valid
181       max_address_bit = i;
182       break;
183     }
184   }
185   if (max_address_bit == 0) {
186     // probing failed, allocate a very high page and take that bit as the maximum
187     const uintptr_t high_addr = ((uintptr_t) 1U) << DEFAULT_MAX_ADDRESS_BIT;
188     void* const result_addr = mmap((void*) high_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
189     if (result_addr != MAP_FAILED) {
190       max_address_bit = BitsPerSize_t - count_leading_zeros((size_t) result_addr) - 1;
191       munmap(result_addr, page_size);
192     }
193   }
194   log_info_p(gc, init)("Probing address space for the highest valid bit: " SIZE_FORMAT, max_address_bit);
195   return MAX2(max_address_bit, MINIMUM_MAX_ADDRESS_BIT);
196 #else // LINUX
197   return DEFAULT_MAX_ADDRESS_BIT;
198 #endif // LINUX
199 }
200 
201 size_t ZPlatformAddressOffsetBits() {
202   const static size_t valid_max_address_offset_bits = probe_valid_max_address_bit() + 1;
203   const size_t max_address_offset_bits = valid_max_address_offset_bits - 3;
204   const size_t min_address_offset_bits = max_address_offset_bits - 2;
205   const size_t address_offset = round_up_power_of_2(MaxHeapSize * ZVirtualToPhysicalRatio);
206   const size_t address_offset_bits = log2i_exact(address_offset);
207   return clamp(address_offset_bits, min_address_offset_bits, max_address_offset_bits);
208 }
209 
210 size_t ZPlatformAddressMetadataShift() {
211   return ZPlatformAddressOffsetBits();
212 }