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