1 /*
   2  * Copyright (c) 1998, 2010, 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 SHARE_VM_RUNTIME_HPI_HPP
  26 #define SHARE_VM_RUNTIME_HPI_HPP
  27 
  28 #include "prims/hpi_imported.h"
  29 #include "runtime/os.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "utilities/top.hpp"
  32 
  33 //
  34 // C++ wrapper to HPI.
  35 //
  36 
  37 class hpi : AllStatic {
  38 
  39 private:
  40   static GetInterfaceFunc       _get_interface;
  41   static HPI_FileInterface*     _file;
  42   static HPI_SocketInterface*   _socket;
  43   static HPI_LibraryInterface*  _library;
  44   static HPI_SystemInterface*   _system;
  45 
  46 private:
  47   static void initialize_get_interface(vm_calls_t *callbacks);
  48 
  49 public:
  50   // Load and initialize everything except sockets.
  51   static jint initialize();
  52 
  53   // Socket library needs to be lazy intialized because eagerly
  54   // loading Winsock is known to cause "connect to your ISP"
  55   // dialog to show up.  Or so goes the legend.
  56   static jint initialize_socket_library();
  57 
  58   // HPI_FileInterface
  59   static inline char*  native_path(char *path);
  60   static inline int    file_type(const char *path);
  61   static inline int    open(const char *name, int mode, int perm);
  62   static inline int    close(int fd);
  63   static inline jlong  lseek(int fd, jlong off, int whence);
  64   static inline int    ftruncate(int fd, jlong length);
  65   static inline int    fsync(int fd);
  66   static inline int    available(int fd, jlong *bytes);
  67   static inline size_t read(int fd, void *buf, unsigned int nBytes);
  68   static inline size_t write(int fd, const void *buf, unsigned int nBytes);
  69   static inline int    fsize(int fd, jlong *size);
  70 
  71   // HPI_SocketInterface
  72   static inline int    socket(int domain, int type, int protocol);
  73   static inline int    socket_close(int fd);
  74   static inline int    socket_shutdown(int fd, int howto);
  75   static inline int    recv(int fd, char *buf, int nBytes, int flags);
  76   static inline int    send(int fd, char *buf, int nBytes, int flags);
  77   // Variant of send that doesn't support interruptible I/O
  78   static inline int    raw_send(int fd, char *buf, int nBytes, int flags);
  79   static inline int    timeout(int fd, long timeout);
  80   static inline int    listen(int fd, int count);
  81   static inline int    connect(int fd, struct sockaddr *him, int len);
  82   static inline int    bind(int fd, struct sockaddr *him, int len);
  83   static inline int    accept(int fd, struct sockaddr *him, int *len);
  84   static inline int    recvfrom(int fd, char *buf, int nbytes, int flags,
  85                                 struct sockaddr *from, int *fromlen);
  86   static inline int    get_sock_name(int fd, struct sockaddr *him, int *len);
  87   static inline int    sendto(int fd, char *buf, int len, int flags,
  88                               struct sockaddr *to, int tolen);
  89   static inline int    socket_available(int fd, jint *pbytes);
  90 
  91   static inline int    get_sock_opt(int fd, int level, int optname,
  92                               char *optval, int* optlen);
  93   static inline int    set_sock_opt(int fd, int level, int optname,
  94                               const char *optval, int optlen);
  95   static inline int    get_host_name(char* name, int namelen);
  96   static inline struct hostent*  get_host_by_addr(const char* name, int len, int type);
  97   static inline struct hostent*  get_host_by_name(char* name);
  98   static inline struct protoent* get_proto_by_name(char* name);
  99 
 100   // HPI_LibraryInterface
 101   static inline void   dll_build_name(char *buf, int buf_len, const char* path,
 102                                       const char *name);
 103   static inline void*  dll_load(const char *name, char *ebuf, int ebuflen);
 104   static inline void   dll_unload(void *lib);
 105   static inline void*  dll_lookup(void *lib, const char *name);
 106 
 107   // HPI_SystemInterface
 108   static inline int    lasterror(char *buf, int len);
 109 };
 110 
 111 //
 112 // Macros that provide inline bodies for the functions.
 113 //
 114 
 115 #define HPIDECL(name, names, intf, func, ret_type, ret_fmt, arg_type, arg_print, arg)   \
 116   inline ret_type hpi::name arg_type {        \
 117     if (TraceHPI) {                           \
 118       tty->print("hpi::" names "(");          \
 119       tty->print arg_print ;                  \
 120       tty->print(") = ");                     \
 121     }                                         \
 122     ret_type result = (*intf->func) arg ;     \
 123     if (TraceHPI) {                           \
 124       tty->print_cr(ret_fmt, result);         \
 125     }                                         \
 126     return result;                            \
 127   }
 128 
 129 // Macro to facilitate moving HPI functionality into the vm.
 130 // See bug 6348631.  The only difference between this macro and
 131 // HPIDECL is that we call a vm method rather than use the HPI
 132 // transfer vector.  Ultimately, we'll replace HPIDECL with
 133 // VM_HPIDECL for all hpi methods.
 134 #define VM_HPIDECL(name, names, func, ret_type, ret_fmt, arg_type,arg_print, arg)   \
 135   inline ret_type hpi::name arg_type {        \
 136     if (TraceHPI) {                           \
 137       tty->print("hpi::" names "(");          \
 138       tty->print arg_print ;                  \
 139       tty->print(") = ");                     \
 140     }                                         \
 141     ret_type result = func arg ;              \
 142     if (TraceHPI) {                           \
 143       tty->print_cr(ret_fmt, result);         \
 144     }                                         \
 145     return result;                            \
 146   }
 147 
 148 #define VM_HPIDECL_VOID(name, names, func, arg_type, arg_print, arg)   \
 149   inline void  hpi::name arg_type {           \
 150     if (TraceHPI) {                           \
 151       tty->print("hpi::" names "(");          \
 152       tty->print arg_print;                   \
 153       tty->print(") = ");                     \
 154     }                                         \
 155     func arg;                                 \
 156   }
 157 
 158 #define HPIDECL_VOID(name, names, intf, func, arg_type, arg_print, arg) \
 159   inline void hpi::name arg_type {            \
 160     if (TraceHPI) {                           \
 161       tty->print("hpi::" names "(");          \
 162       tty->print arg_print ;                  \
 163       tty->print_cr(") = void");              \
 164     }                                         \
 165     (*intf->func) arg ;                       \
 166   }
 167 
 168 
 169 // The macro calls below realize into
 170 //          inline char * hpi::native_path(...) {  inlined_body; }
 171 // etc.
 172 
 173 // HPI_FileInterface
 174 
 175 HPIDECL(native_path, "native_path", _file, NativePath, char *, "%s",
 176         (char *path),
 177         ("path = %s", path),
 178         (path));
 179 
 180 HPIDECL(file_type, "file_type", _file, FileType, int, "%d",
 181         (const char *path),
 182         ("path = %s", path),
 183         (path));
 184 
 185 HPIDECL(open, "open", _file, Open, int, "%d",
 186         (const char *name, int mode, int perm),
 187         ("name = %s, mode = %d, perm = %d", name, mode, perm),
 188         (name, mode, perm));
 189 
 190 HPIDECL(lseek, "seek", _file, Seek, jlong, "(a jlong)",
 191         (int fd, jlong off, int whence),
 192         ("fd = %d, off = (a jlong), whence = %d", fd, /* off, */ whence),
 193         (fd, off, whence));
 194 
 195 HPIDECL(ftruncate, "ftruncate", _file, SetLength, int, "%d",
 196         (int fd, jlong length),
 197         ("fd = %d, length = (a jlong)", fd /*, length */),
 198         (fd, length));
 199 
 200 HPIDECL(fsync, "fsync", _file, Sync, int, "%d",
 201         (int fd),
 202         ("fd = %d", fd),
 203         (fd));
 204 
 205 HPIDECL(available, "available", _file, Available, int, "%d",
 206         (int fd, jlong *bytes),
 207         ("fd = %d, bytes = %p", fd, bytes),
 208         (fd, bytes));
 209 
 210 HPIDECL(fsize, "fsize", _file, FileSizeFD, int, "%d",
 211         (int fd, jlong *size),
 212         ("fd = %d, size = %p", fd, size),
 213         (fd, size));
 214 
 215 // HPI_LibraryInterface
 216 VM_HPIDECL_VOID(dll_build_name, "dll_build_name", os::dll_build_name,
 217                (char *buf, int buf_len, const char *path, const char *name),
 218                ("buf = %p, buflen = %d, path = %s, name = %s",
 219                 buf, buf_len, path, name),
 220                (buf, buf_len, path, name));
 221 
 222 VM_HPIDECL(dll_load, "dll_load", os::dll_load,
 223         void *, "(void *)%p",
 224         (const char *name, char *ebuf, int ebuflen),
 225         ("name = %s, ebuf = %p, ebuflen = %d", name, ebuf, ebuflen),
 226         (name, ebuf, ebuflen));
 227 
 228 HPIDECL_VOID(dll_unload, "dll_unload", _library, UnloadLibrary,
 229              (void *lib),
 230              ("lib = %p", lib),
 231              (lib));
 232 
 233 HPIDECL(dll_lookup, "dll_lookup", _library, FindLibraryEntry, void *, "%p",
 234         (void *lib, const char *name),
 235         ("lib = %p, name = %s", lib, name),
 236         (lib, name));
 237 
 238 // HPI_SystemInterface
 239 HPIDECL(lasterror, "lasterror", _system, GetLastErrorString, int, "%d",
 240         (char *buf, int len),
 241         ("buf = %p, len = %d", buf, len),
 242         (buf, len));
 243 
 244 #endif // SHARE_VM_RUNTIME_HPI_HPP