1 /*
   2  * Copyright (c) 2017, 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 
  25 #include "precompiled.hpp"
  26 #include "logging/log.hpp"
  27 #include "runtime/globals.hpp"
  28 #include "runtime/os.hpp"
  29 #include "runtime/safepointMechanism.hpp"
  30 #include <sys/mman.h>
  31 
  32 void SafepointMechanism::pd_initialize() {
  33   char* map_address = (char*)MAP_FAILED;
  34   const size_t page_size = os::vm_page_size();
  35   // Use optimized addresses for the polling page,
  36   // e.g. map it to a special 32-bit address.
  37   if (OptimizePollingPageLocation) {
  38     // architecture-specific list of address wishes:
  39     char* address_wishes[] = {
  40         // AIX: addresses lower than 0x30000000 don't seem to work on AIX.
  41         // PPC64: all address wishes are non-negative 32 bit values where
  42         // the lower 16 bits are all zero. we can load these addresses
  43         // with a single ppc_lis instruction.
  44         (char*) 0x30000000, (char*) 0x31000000,
  45         (char*) 0x32000000, (char*) 0x33000000,
  46         (char*) 0x40000000, (char*) 0x41000000,
  47         (char*) 0x42000000, (char*) 0x43000000,
  48         (char*) 0x50000000, (char*) 0x51000000,
  49         (char*) 0x52000000, (char*) 0x53000000,
  50         (char*) 0x60000000, (char*) 0x61000000,
  51         (char*) 0x62000000, (char*) 0x63000000
  52     };
  53     int address_wishes_length = sizeof(address_wishes)/sizeof(char*);
  54 
  55     // iterate over the list of address wishes:
  56     for (int i = 0; i < address_wishes_length; i++) {
  57       // Try to map with current address wish.
  58       // AIX: AIX needs MAP_FIXED if we provide an address and mmap will
  59       // fail if the address is already mapped.
  60       map_address = (char*) ::mmap(address_wishes[i] - (ssize_t)page_size,
  61                                    page_size, PROT_READ,
  62                                    MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
  63                                    -1, 0);
  64       log_debug(os)("SafePoint Polling  Page address: %p (wish) => %p",
  65                     address_wishes[i], map_address + (ssize_t)page_size);
  66 
  67       if (map_address + (ssize_t)page_size == address_wishes[i]) {
  68         // Map succeeded and map_address is at wished address, exit loop.
  69         break;
  70       }
  71 
  72       if (map_address != (char*)MAP_FAILED) {
  73         // Map succeeded, but polling_page is not at wished address, unmap and continue.
  74         ::munmap(map_address, page_size);
  75         map_address = (char*)MAP_FAILED;
  76       }
  77       // Map failed, continue loop.
  78     }
  79   }
  80   if (map_address == (char*)MAP_FAILED) {
  81     map_address = os::reserve_memory(page_size, NULL, page_size);
  82   }
  83   guarantee(map_address != (char*)MAP_FAILED, "SafepointMechanism::pd_initialize: failed to allocate polling page");
  84   os::set_polling_page((address)(map_address));
  85 }