|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2021, 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/shared/gc_globals.hpp" |
| 27 | +#include "gc/z/zAddress.hpp" |
| 28 | +#include "gc/z/zBarrierSetAssembler.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 | +// Default value if probing is not implemented for a certain platform: 128TB |
| 40 | +static const size_t DEFAULT_MAX_ADDRESS_BIT = 47; |
| 41 | +// Minimum value returned, if probing fails: 64GB |
| 42 | +static const size_t MINIMUM_MAX_ADDRESS_BIT = 36; |
| 43 | + |
| 44 | +static size_t probe_valid_max_address_bit() { |
| 45 | +#ifdef LINUX |
| 46 | + size_t max_address_bit = 0; |
| 47 | + const size_t page_size = os::vm_page_size(); |
| 48 | + for (size_t i = DEFAULT_MAX_ADDRESS_BIT; i > MINIMUM_MAX_ADDRESS_BIT; --i) { |
| 49 | + const uintptr_t base_addr = ((uintptr_t) 1U) << i; |
| 50 | + if (msync((void*)base_addr, page_size, MS_ASYNC) == 0) { |
| 51 | + // msync suceeded, the address is valid, and maybe even already mapped. |
| 52 | + max_address_bit = i; |
| 53 | + break; |
| 54 | + } |
| 55 | + if (errno != ENOMEM) { |
| 56 | + // Some error occured. This should never happen, but msync |
| 57 | + // has some undefined behavior, hence ignore this bit. |
| 58 | +#ifdef ASSERT |
| 59 | + fatal("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno)); |
| 60 | +#else // ASSERT |
| 61 | + log_warning_p(gc)("Received '%s' while probing the address space for the highest valid bit", os::errno_name(errno)); |
| 62 | +#endif // ASSERT |
| 63 | + continue; |
| 64 | + } |
| 65 | + // Since msync failed with ENOMEM, the page might not be mapped. |
| 66 | + // Try to map it, to see if the address is valid. |
| 67 | + void* const result_addr = mmap((void*) base_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); |
| 68 | + if (result_addr != MAP_FAILED) { |
| 69 | + munmap(result_addr, page_size); |
| 70 | + } |
| 71 | + if ((uintptr_t) result_addr == base_addr) { |
| 72 | + // address is valid |
| 73 | + max_address_bit = i; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + if (max_address_bit == 0) { |
| 78 | + // probing failed, allocate a very high page and take that bit as the maximum |
| 79 | + const uintptr_t high_addr = ((uintptr_t) 1U) << DEFAULT_MAX_ADDRESS_BIT; |
| 80 | + void* const result_addr = mmap((void*) high_addr, page_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); |
| 81 | + if (result_addr != MAP_FAILED) { |
| 82 | + max_address_bit = BitsPerSize_t - count_leading_zeros((size_t) result_addr) - 1; |
| 83 | + munmap(result_addr, page_size); |
| 84 | + } |
| 85 | + } |
| 86 | + log_info_p(gc, init)("Probing address space for the highest valid bit: " SIZE_FORMAT, max_address_bit); |
| 87 | + return MAX2(max_address_bit, MINIMUM_MAX_ADDRESS_BIT); |
| 88 | +#else // LINUX |
| 89 | + return DEFAULT_MAX_ADDRESS_BIT; |
| 90 | +#endif // LINUX |
| 91 | +} |
| 92 | + |
| 93 | +size_t ZPlatformAddressOffsetBits() { |
| 94 | + const static size_t valid_max_address_offset_bits = probe_valid_max_address_bit() + 1; |
| 95 | + const size_t max_address_offset_bits = valid_max_address_offset_bits - 3; |
| 96 | + const size_t min_address_offset_bits = max_address_offset_bits - 2; |
| 97 | + const size_t address_offset = round_up_power_of_2(MaxHeapSize * ZVirtualToPhysicalRatio); |
| 98 | + const size_t address_offset_bits = log2i_exact(address_offset); |
| 99 | + return clamp(address_offset_bits, min_address_offset_bits, max_address_offset_bits); |
| 100 | +} |
| 101 | + |
| 102 | +size_t ZPlatformAddressHeapBaseShift() { |
| 103 | + return ZPlatformAddressOffsetBits(); |
| 104 | +} |
| 105 | + |
| 106 | +void ZGlobalsPointers::pd_set_good_masks() { |
| 107 | + BarrierSetAssembler::clear_patching_epoch(); |
| 108 | +} |
0 commit comments