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