1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef OS_AIX_VM_OS_AIX_INLINE_HPP
  27 #define OS_AIX_VM_OS_AIX_INLINE_HPP
  28 
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/orderAccess.inline.hpp"
  31 #include "runtime/os.hpp"
  32 
  33 // System includes
  34 
  35 #include <unistd.h>
  36 #include <sys/socket.h>
  37 #include <sys/poll.h>
  38 #include <sys/ioctl.h>
  39 #include <netdb.h>
  40 
  41 // Defined in the system headers included above.
  42 #undef rem_size
  43 
  44 inline void* os::thread_local_storage_at(int index) {
  45   return pthread_getspecific((pthread_key_t)index);
  46 }
  47 
  48 inline const char* os::file_separator() {
  49   return "/";
  50 }
  51 
  52 inline const char* os::line_separator() {
  53   return "\n";
  54 }
  55 
  56 inline const char* os::path_separator() {
  57   return ":";
  58 }
  59 
  60 // File names are case-sensitive on windows only
  61 inline int os::file_name_strcmp(const char* s1, const char* s2) {
  62   return strcmp(s1, s2);
  63 }
  64 
  65 inline bool os::obsolete_option(const JavaVMOption *option) {
  66   return false;
  67 }
  68 
  69 inline bool os::uses_stack_guard_pages() {
  70   return true;
  71 }
  72 
  73 inline bool os::allocate_stack_guard_pages() {
  74   assert(uses_stack_guard_pages(), "sanity check");
  75   return true;
  76 }
  77 
  78 
  79 // On Aix, reservations are made on a page by page basis, nothing to do.
  80 inline void os::pd_split_reserved_memory(char *base, size_t size,
  81                                          size_t split, bool realloc) {
  82 }
  83 
  84 
  85 // Bang the shadow pages if they need to be touched to be mapped.
  86 inline void os::bang_stack_shadow_pages() {
  87 }
  88 
  89 inline void os::dll_unload(void *lib) {
  90   ::dlclose(lib);
  91 }
  92 
  93 inline const int os::default_file_open_flags() { return 0;}
  94 
  95 inline DIR* os::opendir(const char* dirname)
  96 {
  97   assert(dirname != NULL, "just checking");
  98   return ::opendir(dirname);
  99 }
 100 
 101 inline int os::readdir_buf_size(const char *path)
 102 {
 103   // according to aix sys/limits, NAME_MAX must be retrieved at runtime. */
 104   const long my_NAME_MAX = pathconf(path, _PC_NAME_MAX);
 105   return my_NAME_MAX + sizeof(dirent) + 1;
 106 }
 107 
 108 inline jlong os::lseek(int fd, jlong offset, int whence) {
 109   return (jlong) ::lseek64(fd, offset, whence);
 110 }
 111 
 112 inline int os::fsync(int fd) {
 113   return ::fsync(fd);
 114 }
 115 
 116 inline char* os::native_path(char *path) {
 117   return path;
 118 }
 119 
 120 inline int os::ftruncate(int fd, jlong length) {
 121   return ::ftruncate64(fd, length);
 122 }
 123 
 124 inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf)
 125 {
 126   dirent* p;
 127   int status;
 128   assert(dirp != NULL, "just checking");
 129 
 130   // NOTE: Linux readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX
 131   // version. Here is the doc for this function:
 132   // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html
 133 
 134   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
 135     errno = status;
 136     return NULL;
 137   } else
 138     return p;
 139 }
 140 
 141 inline int os::closedir(DIR *dirp) {
 142   assert(dirp != NULL, "argument is NULL");
 143   return ::closedir(dirp);
 144 }
 145 
 146 // macros for restartable system calls
 147 
 148 #define RESTARTABLE(_cmd, _result) do { \
 149     _result = _cmd; \
 150   } while(((int)_result == OS_ERR) && (errno == EINTR))
 151 
 152 #define RESTARTABLE_RETURN_INT(_cmd) do { \
 153   int _result; \
 154   RESTARTABLE(_cmd, _result); \
 155   return _result; \
 156 } while(false)
 157 
 158 // We don't have NUMA support on Aix, but we need this for compilation.
 159 inline bool os::numa_has_static_binding()   { ShouldNotReachHere(); return true; }
 160 inline bool os::numa_has_group_homing()     { ShouldNotReachHere(); return false;  }
 161 
 162 inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
 163   size_t res;
 164   RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
 165   return res;
 166 }
 167 
 168 inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
 169   size_t res;
 170   RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
 171   return res;
 172 }
 173 
 174 inline int os::close(int fd) {
 175   return ::close(fd);
 176 }
 177 
 178 inline int os::socket_close(int fd) {
 179   return ::close(fd);
 180 }
 181 
 182 inline int os::socket(int domain, int type, int protocol) {
 183   return ::socket(domain, type, protocol);
 184 }
 185 
 186 inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
 187   RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
 188 }
 189 
 190 inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
 191   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
 192 }
 193 
 194 inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
 195   return os::send(fd, buf, nBytes, flags);
 196 }
 197 
 198 inline int os::timeout(int fd, long timeout) {
 199   julong prevtime,newtime;
 200   struct timeval t;
 201 
 202   gettimeofday(&t, NULL);
 203   prevtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
 204 
 205   for(;;) {
 206     struct pollfd pfd;
 207 
 208     pfd.fd = fd;
 209     pfd.events = POLLIN | POLLERR;
 210 
 211     int res = ::poll(&pfd, 1, timeout);
 212 
 213     if (res == OS_ERR && errno == EINTR) {
 214 
 215       // On Linux any value < 0 means "forever"
 216 
 217       if(timeout >= 0) {
 218         gettimeofday(&t, NULL);
 219         newtime = ((julong)t.tv_sec * 1000) + t.tv_usec / 1000;
 220         timeout -= newtime - prevtime;
 221         if(timeout <= 0)
 222           return OS_OK;
 223         prevtime = newtime;
 224       }
 225     } else
 226       return res;
 227   }
 228 }
 229 
 230 inline int os::listen(int fd, int count) {
 231   return ::listen(fd, count);
 232 }
 233 
 234 inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
 235   RESTARTABLE_RETURN_INT(::connect(fd, him, len));
 236 }
 237 
 238 inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
 239   // Linux doc says this can't return EINTR, unlike accept() on Solaris.
 240   // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
 241   return (int)::accept(fd, him, len);
 242 }
 243 
 244 inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
 245                         sockaddr* from, socklen_t* fromlen) {
 246   RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
 247 }
 248 
 249 inline int os::sendto(int fd, char* buf, size_t len, uint flags,
 250                       struct sockaddr* to, socklen_t tolen) {
 251   RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
 252 }
 253 
 254 inline int os::socket_shutdown(int fd, int howto) {
 255   return ::shutdown(fd, howto);
 256 }
 257 
 258 inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
 259   return ::bind(fd, him, len);
 260 }
 261 
 262 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
 263   return ::getsockname(fd, him, len);
 264 }
 265 
 266 inline int os::get_host_name(char* name, int namelen) {
 267   return ::gethostname(name, namelen);
 268 }
 269 
 270 inline struct hostent* os::get_host_by_name(char* name) {
 271   return ::gethostbyname(name);
 272 }
 273 
 274 inline int os::get_sock_opt(int fd, int level, int optname,
 275                             char* optval, socklen_t* optlen) {
 276   return ::getsockopt(fd, level, optname, optval, optlen);
 277 }
 278 
 279 inline int os::set_sock_opt(int fd, int level, int optname,
 280                             const char* optval, socklen_t optlen) {
 281   return ::setsockopt(fd, level, optname, optval, optlen);
 282 }
 283 
 284 inline bool os::supports_monotonic_clock() {
 285   // mread_real_time() is monotonic on AIX (see os::javaTimeNanos() comments)
 286   return true;
 287 }
 288 
 289 #endif // OS_AIX_VM_OS_AIX_INLINE_HPP