|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 2019, 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/z/zAddress.inline.hpp" |
| 26 | +#include "gc/z/zVirtualMemory.hpp" |
| 27 | +#include "logging/log.hpp" |
| 28 | + |
| 29 | +#include <sys/mman.h> |
| 30 | +#include <sys/types.h> |
| 31 | + |
| 32 | +static void unmap(uintptr_t start, size_t size) { |
| 33 | + const int res = munmap((void*)start, size); |
| 34 | + assert(res == 0, "Failed to unmap memory"); |
| 35 | +} |
| 36 | + |
| 37 | +static bool map(uintptr_t start, size_t size) { |
| 38 | + const void* const res = mmap((void*)start, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0); |
| 39 | + if (res == MAP_FAILED) { |
| 40 | + // Failed to reserve memory |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if ((uintptr_t)res != start) { |
| 45 | + // Failed to reserve memory at the requested address |
| 46 | + unmap(start, size); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // Success |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +bool ZVirtualMemoryManager::reserve_platform(uintptr_t start, size_t size) { |
| 55 | + // Reserve address views |
| 56 | + const uintptr_t marked0 = ZAddress::marked0(start); |
| 57 | + const uintptr_t marked1 = ZAddress::marked1(start); |
| 58 | + const uintptr_t remapped = ZAddress::remapped(start); |
| 59 | + |
| 60 | + if (!map(marked0, size)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + if (!map(marked1, size)) { |
| 65 | + unmap(marked0, size); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if (!map(remapped, size)) { |
| 70 | + unmap(marked0, size); |
| 71 | + unmap(marked1, size); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Register address views with native memory tracker |
| 76 | + nmt_reserve(marked0, size); |
| 77 | + nmt_reserve(marked1, size); |
| 78 | + nmt_reserve(remapped, size); |
| 79 | + |
| 80 | + return true; |
| 81 | +} |
0 commit comments