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 #include "precompiled.hpp"
 26 #include "gc/shared/gcLogPrecious.hpp"
 27 #include "gc/shared/gc_globals.hpp"
 28 #include "gc/z/zGlobals.hpp"
 29 #include "runtime/globals.hpp"
 30 #include "runtime/os.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/powerOfTwo.hpp"
 33 
 34 #ifdef LINUX
 35 #include <sys/mman.h>
 36 #endif // LINUX
 37 
 38 //
 39 // The heap can have three different layouts, depending on the max heap size.
 40 //
 41 // Address Space & Pointer Layout 1
 42 // --------------------------------
 43 //
 44 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
 45 //  .                                .
 46 //  .                                .
 47 //  .                                .
 48 //  +--------------------------------+ 0x0000014000000000 (20TB)
 49 //  |         Remapped View          |
 50 //  +--------------------------------+ 0x0000010000000000 (16TB)
 51 //  .                                .
 52 //  +--------------------------------+ 0x00000c0000000000 (12TB)
 53 //  |         Marked1 View           |
 54 //  +--------------------------------+ 0x0000080000000000 (8TB)
 55 //  |         Marked0 View           |
 56 //  +--------------------------------+ 0x0000040000000000 (4TB)
 57 //  .                                .
 58 //  +--------------------------------+ 0x0000000000000000
 59 //
 60 //   6                  4 4  4 4
 61 //   3                  6 5  2 1                                             0
 62 //  +--------------------+----+-----------------------------------------------+
 63 //  |00000000 00000000 00|1111|11 11111111 11111111 11111111 11111111 11111111|
 64 //  +--------------------+----+-----------------------------------------------+
 65 //  |                    |    |
 66 //  |                    |    * 41-0 Object Offset (42-bits, 4TB address space)
 67 //  |                    |
 68 //  |                    * 45-42 Metadata Bits (4-bits)  0001 = Marked0      (Address view 4-8TB)
 69 //  |                                                    0010 = Marked1      (Address view 8-12TB)
 70 //  |                                                    0100 = Remapped     (Address view 16-20TB)
 71 //  |                                                    1000 = Finalizable  (Address view N/A)
 72 //  |
 73 //  * 63-46 Fixed (18-bits, always zero)
 74 //
 75 //
 76 // Address Space & Pointer Layout 2
 77 // --------------------------------
 78 //
 79 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
 80 //  .                                .
 81 //  .                                .
 82 //  .                                .
 83 //  +--------------------------------+ 0x0000280000000000 (40TB)
 84 //  |         Remapped View          |
 85 //  +--------------------------------+ 0x0000200000000000 (32TB)
 86 //  .                                .
 87 //  +--------------------------------+ 0x0000180000000000 (24TB)
 88 //  |         Marked1 View           |
 89 //  +--------------------------------+ 0x0000100000000000 (16TB)
 90 //  |         Marked0 View           |
 91 //  +--------------------------------+ 0x0000080000000000 (8TB)
 92 //  .                                .
 93 //  +--------------------------------+ 0x0000000000000000
 94 //
 95 //   6                 4 4  4 4
 96 //   3                 7 6  3 2                                              0
 97 //  +------------------+-----+------------------------------------------------+
 98 //  |00000000 00000000 0|1111|111 11111111 11111111 11111111 11111111 11111111|
 99 //  +-------------------+----+------------------------------------------------+
100 //  |                   |    |
101 //  |                   |    * 42-0 Object Offset (43-bits, 8TB address space)
102 //  |                   |
103 //  |                   * 46-43 Metadata Bits (4-bits)  0001 = Marked0      (Address view 8-16TB)
104 //  |                                                   0010 = Marked1      (Address view 16-24TB)
105 //  |                                                   0100 = Remapped     (Address view 32-40TB)
106 //  |                                                   1000 = Finalizable  (Address view N/A)
107 //  |
108 //  * 63-47 Fixed (17-bits, always zero)
109 //
110 //
111 // Address Space & Pointer Layout 3
112 // --------------------------------
113 //
114 //  +--------------------------------+ 0x00007FFFFFFFFFFF (127TB)
115 //  .                                .
116 //  .                                .
117 //  .                                .
118 //  +--------------------------------+ 0x0000500000000000 (80TB)
119 //  |         Remapped View          |
120 //  +--------------------------------+ 0x0000400000000000 (64TB)
121 //  .                                .
122 //  +--------------------------------+ 0x0000300000000000 (48TB)
123 //  |         Marked1 View           |
124 //  +--------------------------------+ 0x0000200000000000 (32TB)
125 //  |         Marked0 View           |
126 //  +--------------------------------+ 0x0000100000000000 (16TB)
127 //  .                                .
128 //  +--------------------------------+ 0x0000000000000000
129 //
130 //   6               4  4  4 4
131 //   3               8  7  4 3                                               0
132 //  +------------------+----+-------------------------------------------------+
133 //  |00000000 00000000 |1111|1111 11111111 11111111 11111111 11111111 11111111|
134 //  +------------------+----+-------------------------------------------------+
135 //  |                  |    |
136 //  |                  |    * 43-0 Object Offset (44-bits, 16TB address space)
137 //  |                  |
138 //  |                  * 47-44 Metadata Bits (4-bits)  0001 = Marked0      (Address view 16-32TB)
139 //  |                                                  0010 = Marked1      (Address view 32-48TB)
140 //  |                                                  0100 = Remapped     (Address view 64-80TB)
141 //  |                                                  1000 = Finalizable  (Address view N/A)
142 //  |
143 //  * 63-48 Fixed (16-bits, always zero)
144 //
145 
146 // Default value if probing is not implemented for a certain platform: 128TB
147 static const size_t DEFAULT_MAX_ADDRESS_BIT = 47;
148 // Minimum value returned, if probing fails: 64GB
149 static const size_t MINIMUM_MAX_ADDRESS_BIT = 36;
150 
151 static size_t probe_valid_max_address_bit() {
152 #ifdef LINUX
153   size_t max_address_bit = 0;
154   const size_t page_size = os::vm_page_size();
155   for (size_t i = DEFAULT_MAX_ADDRESS_BIT; i > MINIMUM_MAX_ADDRESS_BIT; --i) {
156     const uintptr_t base_addr = ((uintptr_t) 1U) << i;
157     if (msync((void*)base_addr, page_size, MS_ASYNC) == 0) {
158       // msync suceeded, the address is valid, and maybe even already mapped.
159       max_address_bit = i;
160       break;
161     }
162     if (errno != ENOMEM) {
163       // Some error occured. This should never happen, but msync
164       // has some undefined behavior, hence ignore this bit.
165 #ifdef ASSERT
166       fatal("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno));
167 #else // ASSERT
168       log_warning_p(gc)("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno));
169 #endif // ASSERT
170       continue;
171     }
172     // Since msync failed with ENOMEM, the page might not be mapped.
173     // Try to map it, to see if the address is valid.
174     void* const result_addr = mmap((void*) base_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
175     if (result_addr != MAP_FAILED) {
176       munmap(result_addr, page_size);
177     }
178     if ((uintptr_t) result_addr == base_addr) {
179       // address is valid
180       max_address_bit = i;
181       break;
182     }
183   }
184   if (max_address_bit == 0) {
185     // probing failed, allocate a very high page and take that bit as the maximum
186     const uintptr_t high_addr = ((uintptr_t) 1U) << DEFAULT_MAX_ADDRESS_BIT;
187     void* const result_addr = mmap((void*) high_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
188     if (result_addr != MAP_FAILED) {
189       max_address_bit = BitsPerSize_t - count_leading_zeros((size_t) result_addr) - 1;
190       munmap(result_addr, page_size);
191     }
192   }
193   log_info_p(gc, init)("Probing address space for the highest valid bit: " SIZE_FORMAT, max_address_bit);
194   return MAX2(max_address_bit, MINIMUM_MAX_ADDRESS_BIT);
195 #else // LINUX
196   return DEFAULT_MAX_ADDRESS_BIT;
197 #endif // LINUX
198 }
199 
200 size_t ZPlatformAddressOffsetBits() {
201   const static size_t valid_max_address_offset_bits = probe_valid_max_address_bit() + 1;
202   const size_t max_address_offset_bits = valid_max_address_offset_bits - 3;
203   const size_t min_address_offset_bits = max_address_offset_bits - 2;
204   const size_t address_offset = round_up_power_of_2(MaxHeapSize * ZVirtualToPhysicalRatio);
205   const size_t address_offset_bits = log2i_exact(address_offset);
206   return clamp(address_offset_bits, min_address_offset_bits, max_address_offset_bits);
207 }
208 
209 size_t ZPlatformAddressMetadataShift() {
210   return ZPlatformAddressOffsetBits();
211 }