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