1 /*
   2  * Copyright (c) 1998, 2005, 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 /*
  26  * HotSpot integration note:
  27  *
  28  * This is a consolidation of these two files:
  29  *      src/share/hpi/export/hpi.h      1.15  99/06/18  JDK1.3 beta build I
  30  *      src/share/hpi/export/dll.h      1.3   98/09/15  JDK1.3 beta build I
  31  * from the classic VM.
  32  *
  33  * bool_t is a type in the classic VM, and we define it here,
  34  * but in the future this should be a jboolean.
  35  *
  36  * The files are included verbatim expect for local includes removed from hpi.h.
  37  */
  38 
  39 #ifndef _JAVASOFT_HPI_H_
  40 #define _JAVASOFT_HPI_H_
  41 
  42 #ifdef __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 /* A classic VMism that should become a jboolean.  Fix in 1.2.1? */
  47 typedef enum { HPI_FALSE = 0, HPI_TRUE = 1 } bool_t;
  48 
  49 /*
  50  * DLL.H: A common interface for helper DLLs loaded by the VM.
  51  * Each library exports the main entry point "DLL_Initialize". Through
  52  * that function the programmer can obtain a function pointer which has
  53  * type "GetInterfaceFunc." Through the function pointer the programmer
  54  * can obtain other interfaces supported in the DLL.
  55  */
  56 typedef jint (JNICALL * GetInterfaceFunc)
  57        (void **intfP, const char *name, jint ver);
  58 
  59 jint JNICALL DLL_Initialize(GetInterfaceFunc *, void *args);
  60 
  61 
  62 /*
  63  * Host Porting Interface. This defines the "porting layer" for
  64  * POSIX.1 compliant operating systems.
  65  */
  66 
  67 /*
  68  * memory allocations
  69  */
  70 typedef struct {
  71     /*
  72      * Malloc must return a unique pointer if size == 0.
  73      */
  74   void *  (*Malloc)(size_t size);
  75   void *  (*Realloc)(void *ptr, size_t new_size);
  76     /*
  77      * Free must allow ptr == NULL to be a no-op.
  78      */
  79   void    (*Free)(void *ptr);
  80     /*
  81      * Calloc must return a unique pointer for if
  82      * n_item == 0 || item_size == 0.
  83      */
  84   void *  (*Calloc)(size_t n_item, size_t item_size);
  85   char *  (*Strdup)(const char *str);
  86 
  87   void *  (*MapMem)(size_t req_size, size_t *maped_size);
  88   void *  (*UnmapMem)(void *req_addr, size_t req_size, size_t *unmap_size);
  89   /*
  90    * CommitMem should round the ptr down to the nearest page and
  91    * round the size up to the nearest page so that the committed
  92    * region is at least as large as the requested region.
  93    */
  94   void *  (*CommitMem)(void *ptr, size_t size, size_t *actual);
  95   /*
  96    * sysDecommitMem should round the ptr up to the nearest page and
  97    * round the size down to the nearest page so that the decommitted
  98    * region is no greater than the requested region.
  99    */
 100   void *  (*DecommitMem)(void *ptr, size_t size, size_t *actual);
 101 
 102 #define HPI_PAGE_ALIGNMENT          (64 * 1024)
 103 
 104   void *  (*AllocBlock)(size_t size, void **headP);
 105   void    (*FreeBlock)(void *head);
 106 } HPI_MemoryInterface;
 107 
 108 /*
 109  * dynamic linking libraries
 110  */
 111 typedef struct {
 112   void   (*BuildLibName)(char *buf, int buf_len, char *path, const char *name);
 113   int    (*BuildFunName)(char *name, int name_len, int arg_size, int en_idx);
 114 
 115   void * (*LoadLibrary)(const char *name, char *err_buf, int err_buflen);
 116   void   (*UnloadLibrary)(void *lib);
 117   void * (*FindLibraryEntry)(void *lib, const char *name);
 118 } HPI_LibraryInterface;
 119 
 120 typedef void (*signal_handler_t)(int sig, void *siginfo, void *context);
 121 
 122 #define HPI_SIG_DFL (signal_handler_t)0
 123 #define HPI_SIG_ERR (signal_handler_t)-1
 124 #define HPI_SIG_IGN (signal_handler_t)1
 125 
 126 typedef struct {
 127   char *name; /* name such as green/native threads. */
 128   int  isMP;
 129 } HPI_SysInfo;
 130 
 131 typedef struct {
 132   HPI_SysInfo *    (*GetSysInfo)(void);
 133   long             (*GetMilliTicks)(void);
 134   jlong            (*TimeMillis)(void);
 135 
 136   signal_handler_t (*Signal)(int sig, signal_handler_t handler);
 137   void             (*Raise)(int sig);
 138   void             (*SignalNotify)(int sig);
 139   int              (*SignalWait)(void);
 140 
 141   int              (*Shutdown)(void);
 142 
 143   int              (*SetLoggingLevel)(int level);
 144   bool_t           (*SetMonitoringOn)(bool_t on);
 145   int              (*GetLastErrorString)(char *buf, int len);
 146 } HPI_SystemInterface;
 147 
 148 /*
 149  * threads and monitors
 150  */
 151 typedef struct  sys_thread sys_thread_t;
 152 typedef struct  sys_mon sys_mon_t;
 153 
 154 #define HPI_OK          0
 155 #define HPI_ERR        -1
 156 #define HPI_INTRPT     -2    /* Operation was interrupted */
 157 #define HPI_TIMEOUT    -3    /* A timer ran out */
 158 #define HPI_NOMEM      -5    /* Ran out of memory */
 159 #define HPI_NORESOURCE -6    /* Ran out of some system resource */
 160 
 161 /* There are three basic states: RUNNABLE, MONITOR_WAIT, and CONDVAR_WAIT.
 162  * When the thread is suspended in any of these states, the
 163  * HPI_THREAD_SUSPENDED bit will be set
 164  */
 165 enum {
 166     HPI_THREAD_RUNNABLE = 1,
 167     HPI_THREAD_MONITOR_WAIT,
 168     HPI_THREAD_CONDVAR_WAIT
 169 };
 170 
 171 #define HPI_MINIMUM_PRIORITY        1
 172 #define HPI_MAXIMUM_PRIORITY        10
 173 #define HPI_NORMAL_PRIORITY         5
 174 
 175 #define HPI_THREAD_SUSPENDED        0x8000
 176 #define HPI_THREAD_INTERRUPTED      0x4000
 177 
 178 typedef struct {
 179     sys_thread_t *owner;
 180     int          entry_count;
 181     sys_thread_t **monitor_waiters;
 182     sys_thread_t **condvar_waiters;
 183     int          sz_monitor_waiters;
 184     int          sz_condvar_waiters;
 185     int          n_monitor_waiters;
 186     int          n_condvar_waiters;
 187 } sys_mon_info;
 188 
 189 typedef struct {
 190   int            (*ThreadBootstrap)(sys_thread_t **tidP,
 191                                     sys_mon_t **qlockP,
 192                                     int nReservedBytes);
 193   int            (*ThreadCreate)(sys_thread_t **tidP,
 194                                  long stk_size,
 195                                  void (*func)(void *),
 196                                  void *arg);
 197   sys_thread_t * (*ThreadSelf)(void);
 198   void           (*ThreadYield)(void);
 199   int            (*ThreadSuspend)(sys_thread_t *tid);
 200   int            (*ThreadResume)(sys_thread_t *tid);
 201   int            (*ThreadSetPriority)(sys_thread_t *tid, int prio);
 202   int            (*ThreadGetPriority)(sys_thread_t *tid, int *prio);
 203   void *         (*ThreadStackPointer)(sys_thread_t *tid);
 204   void *         (*ThreadStackTop)(sys_thread_t *tid);
 205   long *         (*ThreadRegs)(sys_thread_t *tid, int *regs);
 206   int            (*ThreadSingle)(void);
 207   void           (*ThreadMulti)(void);
 208   int            (*ThreadEnumerateOver)(int (*func)(sys_thread_t *, void *),
 209                                         void *arg);
 210   int            (*ThreadCheckStack)(void);
 211   void           (*ThreadPostException)(sys_thread_t *tid, void *arg);
 212   void           (*ThreadInterrupt)(sys_thread_t *tid);
 213   int            (*ThreadIsInterrupted)(sys_thread_t *tid, int clear);
 214   int            (*ThreadAlloc)(sys_thread_t **tidP);
 215   int            (*ThreadFree)(void);
 216   jlong          (*ThreadCPUTime)(void);
 217   int            (*ThreadGetStatus)(sys_thread_t *tid, sys_mon_t **monitor);
 218   void *         (*ThreadInterruptEvent)(void);
 219   void *         (*ThreadNativeID)(sys_thread_t *tid);
 220 
 221   /* These three functions are used by the CPU time profiler.
 222    * sysThreadIsRunning determines whether the thread is running (not just
 223    * runnable). It is only safe to call this function after calling
 224    * sysThreadProfSuspend.
 225    */
 226   bool_t         (*ThreadIsRunning)(sys_thread_t *tid);
 227   void           (*ThreadProfSuspend)(sys_thread_t *tid);
 228   void           (*ThreadProfResume)(sys_thread_t *tid);
 229 
 230   int            (*AdjustTimeSlice)(int ms);
 231 
 232   size_t         (*MonitorSizeof)(void);
 233   int            (*MonitorInit)(sys_mon_t *mid);
 234   int            (*MonitorDestroy)(sys_mon_t *mid);
 235   int            (*MonitorEnter)(sys_thread_t *self, sys_mon_t *mid);
 236   bool_t         (*MonitorEntered)(sys_thread_t *self, sys_mon_t *mid);
 237   int            (*MonitorExit)(sys_thread_t *self, sys_mon_t *mid);
 238   int            (*MonitorNotify)(sys_thread_t *self, sys_mon_t *mid);
 239   int            (*MonitorNotifyAll)(sys_thread_t *self, sys_mon_t *mid);
 240   int            (*MonitorWait)(sys_thread_t *self, sys_mon_t *mid, jlong ms);
 241   bool_t         (*MonitorInUse)(sys_mon_t *mid);
 242   sys_thread_t * (*MonitorOwner)(sys_mon_t *mid);
 243   int            (*MonitorGetInfo)(sys_mon_t *mid, sys_mon_info *info);
 244 
 245 } HPI_ThreadInterface;
 246 
 247 /*
 248  * files
 249  */
 250 
 251 #define HPI_FILETYPE_REGULAR    (0)
 252 #define HPI_FILETYPE_DIRECTORY  (1)
 253 #define HPI_FILETYPE_OTHER      (2)
 254 
 255 typedef struct {
 256   char *         (*NativePath)(char *path);
 257   int            (*FileType)(const char *path);
 258   int            (*Open)(const char *name, int openMode, int filePerm);
 259   int            (*Close)(int fd);
 260   jlong          (*Seek)(int fd, jlong offset, int whence);
 261   int            (*SetLength)(int fd, jlong length);
 262   int            (*Sync)(int fd);
 263   int            (*Available)(int fd, jlong *bytes);
 264   size_t         (*Read)(int fd, void *buf, unsigned int nBytes);
 265   size_t         (*Write)(int fd, const void *buf, unsigned int nBytes);
 266   int            (*FileSizeFD)(int fd, jlong *size);
 267 } HPI_FileInterface;
 268 
 269 /*
 270  * sockets
 271  */
 272 struct sockaddr;
 273 struct hostent;
 274 
 275 typedef struct {
 276   int              (*Close)(int fd);
 277   long             (*Available)(int fd, jint *pbytes);
 278   int              (*Connect)(int fd, struct sockaddr *him, int len);
 279   int              (*Accept)(int fd, struct sockaddr *him, int *len);
 280   int              (*SendTo)(int fd, char *buf, int len, int flags,
 281                              struct sockaddr *to, int tolen);
 282   int              (*RecvFrom)(int fd, char *buf, int nbytes, int flags,
 283                                struct sockaddr *from, int *fromlen);
 284   int              (*Listen)(int fd, long count);
 285   int              (*Recv)(int fd, char *buf, int nBytes, int flags);
 286   int              (*Send)(int fd, char *buf, int nBytes, int flags);
 287   int              (*Timeout)(int fd, long timeout);
 288   struct hostent * (*GetHostByName)(char *hostname);
 289   int              (*Socket)(int domain, int type, int protocol);
 290   int              (*SocketShutdown)(int fd, int howto);
 291   int              (*Bind)(int fd, struct sockaddr *him, int len);
 292   int              (*GetSocketName)(int fd, struct sockaddr *him, int *len);
 293   int              (*GetHostName)(char *hostname, int namelen);
 294   struct hostent * (*GetHostByAddr)(const char *hostname, int len, int type);
 295   int              (*SocketGetOption)(int fd, int level, int optname, char *optval, int *optlen);
 296   int              (*SocketSetOption)(int fd, int level, int optname, const char *optval, int optlen);
 297   struct protoent * (*GetProtoByName)(char* name);
 298 } HPI_SocketInterface;
 299 
 300 /*
 301  * callbacks.
 302  */
 303 typedef struct vm_calls {
 304     int    (*jio_fprintf)(FILE *fp, const char *fmt, ...);
 305     void   (*panic)(const char *fmt, ...);
 306     void   (*monitorRegister)(sys_mon_t *mid, char *info_str);
 307 
 308     void   (*monitorContendedEnter)(sys_thread_t *self, sys_mon_t *mid);
 309     void   (*monitorContendedEntered)(sys_thread_t *self, sys_mon_t *mid);
 310     void   (*monitorContendedExit)(sys_thread_t *self, sys_mon_t *mid);
 311 } vm_calls_t;
 312 
 313 #ifdef __cplusplus
 314 }
 315 #endif
 316 
 317 #endif /* !_JAVASOFT_HPI_H_ */