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