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 bool map(uintptr_t start, size_t size) {
  42   const void* const res = mmap((void*)start, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
  43   if (res == MAP_FAILED) {
  44     // Failed to reserve memory
  45     return false;
  46   }
  47 
  48   if ((uintptr_t)res != start) {
  49     // Failed to reserve memory at the requested address
  50     unmap((uintptr_t)res, size);
  51     return false;
  52   }
  53 
  54   // Success
  55   return true;
  56 }
  57 
  58 bool ZVirtualMemoryManager::reserve_contiguous_platform(uintptr_t start, size_t size) {
  59   // Reserve address views
  60   const uintptr_t marked0 = ZAddress::marked0(start);
  61   const uintptr_t marked1 = ZAddress::marked1(start);
  62   const uintptr_t remapped = ZAddress::remapped(start);
  63 
  64   if (!map(marked0, size)) {
  65     return false;
  66   }
  67 
  68   if (!map(marked1, size)) {
  69     unmap(marked0, size);
  70     return false;
  71   }
  72 
  73   if (!map(remapped, size)) {
  74     unmap(marked0, size);
  75     unmap(marked1, size);
  76     return false;
  77   }
  78 
  79   // Register address views with native memory tracker
  80   nmt_reserve(marked0, size);
  81   nmt_reserve(marked1, size);
  82   nmt_reserve(remapped, size);
  83 
  84   return true;
  85 }