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