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