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 void ZVirtualMemoryManager::initialize_os() {
  33   // Does nothing
  34 }
  35 
  36 static void unmap(uintptr_t start, size_t size) {
  37   const int res = munmap((void*)start, size);
  38   assert(res == 0, "Failed to unmap memory");
  39 }
  40 
  41 static uintptr_t map(uintptr_t start, size_t size, uintptr_t *mmap_result) {
  42   const void* const res = mmap((void*)start, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
  43   *mmap_result = (uintptr_t) res;
  44   if (res == MAP_FAILED) {
  45     // Failed to reserve memory
  46     return false;
  47   }
  48 
  49   if ((uintptr_t)res != start) {
  50     // Failed to reserve memory at the requested address
  51     unmap((uintptr_t)res, size);
  52     return false;
  53   }
  54 
  55   // Success
  56   return true;
  57 }
  58 
  59 bool ZVirtualMemoryManager::reserve_contiguous_platform(uintptr_t start, size_t size) {
  60   // Reserve address views
  61   const uintptr_t marked0 = ZAddress::marked0(start);
  62   const uintptr_t marked1 = ZAddress::marked1(start);
  63   const uintptr_t remapped = ZAddress::remapped(start);
  64 
  65   uintptr_t mmap_result;
  66 
  67   if (!map(marked0, size, &mmap_result)) {
  68     size_t address_bits = sizeof(size_t) * CHAR_BIT - (size_t)__builtin_clzll((unsigned long long)mmap_result);
  69     if (address_bits > _address_bits_seen) {
  70       _address_bits_seen = address_bits;
  71     }
  72     return false;
  73   }
  74 
  75   if (!map(marked1, size, &mmap_result)) {
  76     size_t address_bits = sizeof(size_t) * CHAR_BIT - (size_t)__builtin_clzll((unsigned long long)mmap_result);
  77     if (address_bits > _address_bits_seen) {
  78       _address_bits_seen = address_bits;
  79     }
  80     unmap(marked0, size);
  81     return false;
  82   }
  83 
  84   if (!map(remapped, size, &mmap_result)) {
  85     size_t address_bits = sizeof(size_t) * CHAR_BIT - (size_t)__builtin_clzll((unsigned long long)mmap_result);
  86     if (address_bits > _address_bits_seen) {
  87       _address_bits_seen = address_bits;
  88     }
  89     unmap(marked0, size);
  90     unmap(marked1, size);
  91     return false;
  92   }
  93 
  94   // Register address views with native memory tracker
  95   nmt_reserve(marked0, size);
  96   nmt_reserve(marked1, size);
  97   nmt_reserve(remapped, size);
  98 
  99   return true;
 100 }