1 /*
   2  * Copyright (c) 1999, 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_LINUX_VM_OS_LINUX_INLINE_HPP
  26 #define OS_LINUX_VM_OS_LINUX_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_linux_x86
  33 # include "orderAccess_linux_x86.inline.hpp"
  34 #endif
  35 #ifdef TARGET_OS_ARCH_linux_sparc
  36 # include "orderAccess_linux_sparc.inline.hpp"
  37 #endif
  38 #ifdef TARGET_OS_ARCH_linux_zero
  39 # include "orderAccess_linux_zero.inline.hpp"
  40 #endif
  41 #ifdef TARGET_OS_ARCH_linux_arm
  42 # include "orderAccess_linux_arm.inline.hpp"
  43 #endif
  44 #ifdef TARGET_OS_ARCH_linux_ppc
  45 # include "orderAccess_linux_ppc.inline.hpp"
  46 #endif
  47 
  48 // System includes
  49 
  50 #include <unistd.h>
  51 #include <sys/socket.h>
  52 #include <sys/poll.h>
  53 #include <netdb.h>
  54 
  55 inline void* os::thread_local_storage_at(int index) {
  56   return pthread_getspecific((pthread_key_t)index);
  57 }
  58 
  59 inline const char* os::file_separator() {
  60   return "/";
  61 }
  62 
  63 inline const char* os::line_separator() {
  64   return "\n";
  65 }
  66 
  67 inline const char* os::path_separator() {
  68   return ":";
  69 }
  70 
  71 // File names are case-sensitive on windows only
  72 inline int os::file_name_strcmp(const char* s1, const char* s2) {
  73   return strcmp(s1, s2);
  74 }
  75 
  76 inline bool os::obsolete_option(const JavaVMOption *option) {
  77   return false;
  78 }
  79 
  80 inline bool os::uses_stack_guard_pages() {
  81   return true;
  82 }
  83 
  84 inline bool os::allocate_stack_guard_pages() {
  85   assert(uses_stack_guard_pages(), "sanity check");
  86   return true;
  87 }
  88 
  89 
  90 // On Linux, reservations are made on a page by page basis, nothing to do.
  91 inline void os::pd_split_reserved_memory(char *base, size_t size,
  92                                       size_t split, bool realloc) {
  93 }
  94 
  95 
  96 // Bang the shadow pages if they need to be touched to be mapped.
  97 inline void os::bang_stack_shadow_pages() {
  98 }
  99 
 100 inline void os::dll_unload(void *lib) {
 101   ::dlclose(lib);
 102 }
 103 
 104 inline const int os::default_file_open_flags() { return 0;}
 105 
 106 inline DIR* os::opendir(const char* dirname)
 107 {
 108   assert(dirname != NULL, "just checking");
 109   return ::opendir(dirname);
 110 }
 111 
 112 inline int os::readdir_buf_size(const char *path)
 113 {
 114   return NAME_MAX + sizeof(dirent) + 1;
 115 }
 116 
 117 inline jlong os::lseek(int fd, jlong offset, int whence) {
 118   return (jlong) ::lseek64(fd, offset, whence);
 119 }
 120 
 121 inline int os::fsync(int fd) {
 122   return ::fsync(fd);
 123 }
 124 
 125 inline char* os::native_path(char *path) {
 126   return path;
 127 }
 128 
 129 inline int os::ftruncate(int fd, jlong length) {
 130   return ::ftruncate64(fd, length);
 131 }
 132 
 133 inline struct dirent* os::readdir(DIR* dirp, dirent *dbuf)
 134 {
 135   dirent* p;
 136   int status;
 137   assert(dirp != NULL, "just checking");
 138 
 139   // NOTE: Linux readdir_r (on RH 6.2 and 7.2 at least) is NOT like the POSIX
 140   // version. Here is the doc for this function:
 141   // http://www.gnu.org/manual/glibc-2.2.3/html_node/libc_262.html
 142 
 143   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
 144     errno = status;
 145     return NULL;
 146   } else
 147     return p;
 148 }
 149 
 150 inline int os::closedir(DIR *dirp) {
 151   assert(dirp != NULL, "argument is NULL");
 152   return ::closedir(dirp);
 153 }
 154 
 155 // macros for restartable system calls
 156 
 157 #define RESTARTABLE(_cmd, _result) do { \
 158     _result = _cmd; \
 159   } while(((int)_result == OS_ERR) && (errno == EINTR))
 160 
 161 #define RESTARTABLE_RETURN_INT(_cmd) do { \
 162   int _result; \
 163   RESTARTABLE(_cmd, _result); \
 164   return _result; \
 165 } while(false)
 166 
 167 inline bool os::numa_has_static_binding()   { return true; }
 168 inline bool os::numa_has_group_homing()     { return false;  }
 169 
 170 inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
 171   size_t res;
 172   RESTARTABLE( (size_t) ::read(fd, buf, (size_t) nBytes), res);
 173   return res;
 174 }
 175 
 176 inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
 177   size_t res;
 178   RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
 179   return res;
 180 }
 181 
 182 inline int os::close(int fd) {
 183   return ::close(fd);
 184 }
 185 
 186 inline int os::socket_close(int fd) {
 187   return ::close(fd);
 188 }
 189 
 190 inline int os::socket(int domain, int type, int protocol) {
 191   return ::socket(domain, type, protocol);
 192 }
 193 
 194 inline int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
 195   RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
 196 }
 197 
 198 inline int os::send(int fd, char* buf, size_t nBytes, uint flags) {
 199   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
 200 }
 201 
 202 inline int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
 203   return os::send(fd, buf, nBytes, flags);
 204 }
 205 
 206 inline int os::timeout(int fd, long timeout) {
 207   julong prevtime,newtime;
 208   struct timeval t;
 209 
 210   gettimeofday(&t, NULL);
 211   prevtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 212 
 213   for(;;) {
 214     struct pollfd pfd;
 215 
 216     pfd.fd = fd;
 217     pfd.events = POLLIN | POLLERR;
 218 
 219     int res = ::poll(&pfd, 1, timeout);
 220 
 221     if (res == OS_ERR && errno == EINTR) {
 222 
 223       // On Linux any value < 0 means "forever"
 224 
 225       if(timeout >= 0) {
 226         gettimeofday(&t, NULL);
 227         newtime = ((julong)t.tv_sec * 1000)  +  t.tv_usec / 1000;
 228         timeout -= newtime - prevtime;
 229         if(timeout <= 0)
 230           return OS_OK;
 231         prevtime = newtime;
 232       }
 233     } else
 234       return res;
 235   }
 236 }
 237 
 238 inline int os::listen(int fd, int count) {
 239   return ::listen(fd, count);
 240 }
 241 
 242 inline int os::connect(int fd, struct sockaddr* him, socklen_t len) {
 243   RESTARTABLE_RETURN_INT(::connect(fd, him, len));
 244 }
 245 
 246 inline int os::accept(int fd, struct sockaddr* him, socklen_t* len) {
 247   // Linux doc says this can't return EINTR, unlike accept() on Solaris.
 248   // But see attachListener_linux.cpp, LinuxAttachListener::dequeue().
 249   return (int)::accept(fd, him, len);
 250 }
 251 
 252 inline int os::recvfrom(int fd, char* buf, size_t nBytes, uint flags,
 253                         sockaddr* from, socklen_t* fromlen) {
 254   RESTARTABLE_RETURN_INT((int)::recvfrom(fd, buf, nBytes, flags, from, fromlen));
 255 }
 256 
 257 inline int os::sendto(int fd, char* buf, size_t len, uint flags,
 258                       struct sockaddr* to, socklen_t tolen) {
 259   RESTARTABLE_RETURN_INT((int)::sendto(fd, buf, len, flags, to, tolen));
 260 }
 261 
 262 inline int os::socket_shutdown(int fd, int howto) {
 263   return ::shutdown(fd, howto);
 264 }
 265 
 266 inline int os::bind(int fd, struct sockaddr* him, socklen_t len) {
 267   return ::bind(fd, him, len);
 268 }
 269 
 270 inline int os::get_sock_name(int fd, struct sockaddr* him, socklen_t* len) {
 271   return ::getsockname(fd, him, len);
 272 }
 273 
 274 inline int os::get_host_name(char* name, int namelen) {
 275   return ::gethostname(name, namelen);
 276 }
 277 
 278 inline struct hostent* os::get_host_by_name(char* name) {
 279   return ::gethostbyname(name);
 280 }
 281 
 282 inline int os::get_sock_opt(int fd, int level, int optname,
 283                             char* optval, socklen_t* optlen) {
 284   return ::getsockopt(fd, level, optname, optval, optlen);
 285 }
 286 
 287 inline int os::set_sock_opt(int fd, int level, int optname,
 288                             const char* optval, socklen_t optlen) {
 289   return ::setsockopt(fd, level, optname, optval, optlen);
 290 }
 291 #endif // OS_LINUX_VM_OS_LINUX_INLINE_HPP