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 "services/memTracker.hpp"
  31 #include <sys/mman.h>
  32 
  33 void SafepointMechanism::pd_initialize() {
  34   // No special code needed if we can use SIGTRAP
  35   if (ThreadLocalHandshakes && USE_POLL_BIT_ONLY) {
  36     default_initialize();
  37     return;
  38   }
  39 
  40   // Allocate one protected page
  41   char* map_address = (char*)MAP_FAILED;
  42   const size_t map_size = ThreadLocalHandshakes ? 2 * os::vm_page_size() : os::vm_page_size();
  43   const int prot  = PROT_READ;
  44   const int flags = MAP_PRIVATE | MAP_ANONYMOUS;
  45 
  46   // Use optimized addresses for the polling page,
  47   // e.g. map it to a special 32-bit address.
  48   if (OptimizePollingPageLocation) {
  49     // architecture-specific list of address wishes:
  50     char* address_wishes[] = {
  51         // AIX: addresses lower than 0x30000000 don't seem to work on AIX.
  52         // PPC64: all address wishes are non-negative 32 bit values where
  53         // the lower 16 bits are all zero. we can load these addresses
  54         // with a single ppc_lis instruction.
  55         (char*) 0x30000000, (char*) 0x31000000,
  56         (char*) 0x32000000, (char*) 0x33000000,
  57         (char*) 0x40000000, (char*) 0x41000000,
  58         (char*) 0x42000000, (char*) 0x43000000,
  59         (char*) 0x50000000, (char*) 0x51000000,
  60         (char*) 0x52000000, (char*) 0x53000000,
  61         (char*) 0x60000000, (char*) 0x61000000,
  62         (char*) 0x62000000, (char*) 0x63000000
  63     };
  64     int address_wishes_length = sizeof(address_wishes)/sizeof(char*);
  65 
  66     // iterate over the list of address wishes:
  67     for (int i = 0; i < address_wishes_length; i++) {
  68       // Try to map with current address wish.
  69       // AIX: AIX needs MAP_FIXED if we provide an address and mmap will
  70       // fail if the address is already mapped.
  71       map_address = (char*) ::mmap(address_wishes[i],
  72                                    map_size, prot,
  73                                    flags | MAP_FIXED,
  74                                    -1, 0);
  75       log_debug(os)("SafePoint Polling Page address: %p (wish) => %p",
  76                     address_wishes[i], map_address);
  77 
  78       if (map_address == address_wishes[i]) {
  79         // Map succeeded and map_address is at wished address, exit loop.
  80         break;
  81       }
  82 
  83       if (map_address != (char*)MAP_FAILED) {
  84         // Map succeeded, but polling_page is not at wished address, unmap and continue.
  85         ::munmap(map_address, map_size);
  86         map_address = (char*)MAP_FAILED;
  87       }
  88       // Map failed, continue loop.
  89     }
  90   }
  91   if (map_address == (char*)MAP_FAILED) {
  92     map_address = (char*) ::mmap(NULL, map_size, prot, flags, -1, 0);
  93   }
  94   guarantee(map_address != (char*)MAP_FAILED && map_address != NULL,
  95             "SafepointMechanism::pd_initialize: failed to allocate polling page");
  96   log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(map_address));
  97   os::set_polling_page((address)(map_address));
  98 
  99   // Register polling page with NMT.
 100   MemTracker::record_virtual_memory_reserve_and_commit(map_address, map_size, CALLER_PC, mtSafepoint);
 101 
 102   // Use same page for ThreadLocalHandshakes without SIGTRAP
 103   if (ThreadLocalHandshakes) {
 104     set_uses_thread_local_poll();
 105     os::make_polling_page_unreadable();
 106     intptr_t bad_page_val  = reinterpret_cast<intptr_t>(map_address),
 107              good_page_val = bad_page_val + os::vm_page_size();
 108     _poll_armed_value    = reinterpret_cast<void*>(bad_page_val  + poll_bit());
 109     _poll_disarmed_value = reinterpret_cast<void*>(good_page_val);
 110   }
 111 }