1 /*
   2  * Copyright (c) 1997, 2013, 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 #ifndef OS_SOLARIS_VM_OS_SOLARIS_INLINE_HPP
  26 #define OS_SOLARIS_VM_OS_SOLARIS_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/os.hpp"
  31 
  32 #ifdef TARGET_OS_ARCH_solaris_x86
  33 # include "orderAccess_solaris_x86.inline.hpp"
  34 #endif
  35 #ifdef TARGET_OS_ARCH_solaris_sparc
  36 # include "orderAccess_solaris_sparc.inline.hpp"
  37 #endif
  38 
  39 // System includes
  40 #include <sys/param.h>
  41 #include <dlfcn.h>
  42 #include <sys/socket.h>
  43 #include <sys/poll.h>
  44 #include <sys/filio.h>
  45 #include <unistd.h>
  46 #include <netdb.h>
  47 #include <setjmp.h>
  48 
  49 inline const char* os::file_separator() { return "/"; }
  50 inline const char* os::line_separator() { return "\n"; }
  51 inline const char* os::path_separator() { return ":"; }
  52 
  53 // File names are case-sensitive on windows only
  54 inline int os::file_name_strcmp(const char* s1, const char* s2) {
  55   return strcmp(s1, s2);
  56 }
  57 
  58 inline bool os::uses_stack_guard_pages() {
  59   return true;
  60 }
  61 
  62 inline bool os::allocate_stack_guard_pages() {
  63   assert(uses_stack_guard_pages(), "sanity check");
  64   int r = thr_main() ;
  65   guarantee (r == 0 || r == 1, "CR6501650 or CR6493689") ;
  66   return r;
  67 }
  68 
  69 
  70 // On Solaris, reservations are made on a page by page basis, nothing to do.
  71 inline void os::pd_split_reserved_memory(char *base, size_t size,
  72                                       size_t split, bool realloc) {
  73 }
  74 
  75 
  76 // Bang the shadow pages if they need to be touched to be mapped.
  77 inline void os::bang_stack_shadow_pages() {
  78 }
  79 inline void os::dll_unload(void *lib) { ::dlclose(lib); }
  80 
  81 inline DIR* os::opendir(const char* dirname) {
  82   assert(dirname != NULL, "just checking");
  83   return ::opendir(dirname);
  84 }
  85 
  86 inline int os::readdir_buf_size(const char *path) {
  87   int size = pathconf(path, _PC_NAME_MAX);
  88   return (size < 0 ? MAXPATHLEN : size) + sizeof(dirent) + 1;
  89 }
  90 
  91 inline struct dirent* os::readdir(DIR* dirp, dirent* dbuf) {
  92   assert(dirp != NULL, "just checking");
  93 #if defined(_LP64) || defined(_GNU_SOURCE)
  94   dirent* p;
  95   int status;
  96 
  97   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
  98     errno = status;
  99     return NULL;
 100   } else
 101     return p;
 102 #else  // defined(_LP64) || defined(_GNU_SOURCE)
 103   return ::readdir_r(dirp, dbuf);
 104 #endif // defined(_LP64) || defined(_GNU_SOURCE)
 105 }
 106 
 107 inline int os::closedir(DIR *dirp) {
 108   assert(dirp != NULL, "argument is NULL");
 109   return ::closedir(dirp);
 110 }
 111 
 112 //////////////////////////////////////////////////////////////////////////////
 113 ////////////////////////////////////////////////////////////////////////////////
 114 
 115 // macros for interruptible io and system calls and system call restarting
 116 
 117 #define _INTERRUPTIBLE(_setup, _cmd, _result, _thread, _clear, _before, _after, _int_enable) \
 118 do { \
 119   _setup; \
 120   _before; \
 121   OSThread* _osthread = _thread->osthread(); \
 122   if (_int_enable && _thread->has_last_Java_frame()) { \
 123     /* this is java interruptible io stuff */ \
 124     if (os::is_interrupted(_thread, _clear))  { \
 125       os::Solaris::bump_interrupted_before_count(); \
 126       _result = OS_INTRPT; \
 127     } else { \
 128       /* _cmd always expands to an assignment to _result */ \
 129       if ((_cmd) < 0 && errno == EINTR  \
 130        && os::is_interrupted(_thread, _clear)) { \
 131         os::Solaris::bump_interrupted_during_count(); \
 132         _result = OS_INTRPT; \
 133       } \
 134     } \
 135   } else { \
 136     /* this is normal blocking io stuff */ \
 137     _cmd; \
 138   } \
 139   _after; \
 140 } while(false)
 141 
 142 // Interruptible io support + restarting of interrupted system calls
 143 
 144 #ifndef ASSERT
 145 
 146 #define INTERRUPTIBLE(_cmd, _result, _clear) do { \
 147   _INTERRUPTIBLE( JavaThread* _thread = (JavaThread*)ThreadLocalStorage::thread(),_result = _cmd, _result, _thread, _clear, , , UseVMInterruptibleIO); \
 148 } while((_result == OS_ERR) && (errno == EINTR))
 149 
 150 #else
 151 
 152 // This adds an assertion that it is only called from thread_in_native
 153 // The call overhead is skipped for performance in product mode
 154 #define INTERRUPTIBLE(_cmd, _result, _clear) do { \
 155   _INTERRUPTIBLE(JavaThread* _thread = os::Solaris::setup_interruptible_native(), _result = _cmd, _result, _thread, _clear, , os::Solaris::cleanup_interruptible_native(_thread), UseVMInterruptibleIO ); \
 156 } while((_result == OS_ERR) && (errno == EINTR))
 157 
 158 #endif
 159 
 160 // Used for calls from _thread_in_vm, not from _thread_in_native
 161 #define INTERRUPTIBLE_VM(_cmd, _result, _clear) do { \
 162   _INTERRUPTIBLE(JavaThread* _thread = os::Solaris::setup_interruptible(), _result = _cmd, _result, _thread, _clear, , os::Solaris::cleanup_interruptible(_thread), UseVMInterruptibleIO ); \
 163 } while((_result == OS_ERR) && (errno == EINTR))
 164 
 165 /* Use NORESTART when the system call cannot return EINTR, when something other
 166    than a system call is being invoked, or when the caller must do EINTR
 167    handling. */
 168 
 169 #ifndef ASSERT
 170 
 171 #define INTERRUPTIBLE_NORESTART(_cmd, _result, _clear) \
 172   _INTERRUPTIBLE( JavaThread* _thread = (JavaThread*)ThreadLocalStorage::thread(),_result = _cmd, _result, _thread, _clear, , , UseVMInterruptibleIO)
 173 
 174 #else
 175 
 176 // This adds an assertion that it is only called from thread_in_native
 177 // The call overhead is skipped for performance in product mode
 178 #define INTERRUPTIBLE_NORESTART(_cmd, _result, _clear) \
 179   _INTERRUPTIBLE(JavaThread* _thread = os::Solaris::setup_interruptible_native(), _result = _cmd, _result, _thread, _clear, , os::Solaris::cleanup_interruptible_native(_thread), UseVMInterruptibleIO )
 180 
 181 #endif
 182 
 183 // Don't attend to UseVMInterruptibleIO. Always allow interruption.
 184 // Also assumes that it is called from the _thread_blocked state.
 185 // Used by os_sleep().
 186 
 187 #define INTERRUPTIBLE_NORESTART_VM_ALWAYS(_cmd, _result, _thread, _clear) \
 188   _INTERRUPTIBLE(os::Solaris::setup_interruptible_already_blocked(_thread), _result = _cmd, _result, _thread, _clear, , , true )
 189 
 190 #define INTERRUPTIBLE_RETURN_INT(_cmd, _clear) do { \
 191   int _result; \
 192   do { \
 193     INTERRUPTIBLE(_cmd, _result, _clear); \
 194   } while((_result == OS_ERR) && (errno == EINTR)); \
 195   return _result; \
 196 } while(false)
 197 
 198 #define INTERRUPTIBLE_RETURN_INT_VM(_cmd, _clear) do { \
 199   int _result; \
 200   do { \
 201     INTERRUPTIBLE_VM(_cmd, _result, _clear); \
 202   } while((_result == OS_ERR) && (errno == EINTR)); \
 203   return _result; \
 204 } while(false)
 205 
 206 #define INTERRUPTIBLE_RETURN_INT_NORESTART(_cmd, _clear) do { \
 207   int _result; \
 208   INTERRUPTIBLE_NORESTART(_cmd, _result, _clear); \
 209   return _result; \
 210 } while(false)
 211 
 212 /* Use the RESTARTABLE macros when interruptible io is not needed */
 213 
 214 #define RESTARTABLE(_cmd, _result) do { \
 215   do { \
 216     _result = _cmd; \
 217   } while((_result == OS_ERR) && (errno == EINTR)); \
 218 } while(false)
 219 
 220 #define RESTARTABLE_RETURN_INT(_cmd) do { \
 221   int _result; \
 222   RESTARTABLE(_cmd, _result); \
 223   return _result; \
 224 } while(false)
 225 
 226 inline bool os::numa_has_static_binding()   { return false; }
 227 inline bool os::numa_has_group_homing()     { return true;  }
 228 
 229 inline int    os::socket(int domain, int type, int protocol) {
 230   return ::socket(domain, type, protocol);
 231 }
 232 
 233 inline int    os::listen(int fd, int count) {
 234   if (fd < 0) return OS_ERR;
 235 
 236   return ::listen(fd, count);
 237 }
 238 
 239 inline int os::socket_shutdown(int fd, int howto){
 240   return ::shutdown(fd, howto);
 241 }
 242 
 243 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len){
 244   return ::getsockname(fd, him, len);
 245 }
 246 
 247 inline int os::get_host_name(char* name, int namelen){
 248   return ::gethostname(name, namelen);
 249 }
 250 
 251 inline struct hostent* os::get_host_by_name(char* name) {
 252   return ::gethostbyname(name);
 253 }
 254 
 255 inline int os::get_sock_opt(int fd, int level, int optname,
 256                             char* optval, socklen_t* optlen) {
 257   return ::getsockopt(fd, level, optname, optval, optlen);
 258 }
 259 
 260 inline int os::set_sock_opt(int fd, int level, int optname,
 261                             const char *optval, socklen_t optlen) {
 262   return ::setsockopt(fd, level, optname, optval, optlen);
 263 }
 264 #endif // OS_SOLARIS_VM_OS_SOLARIS_INLINE_HPP