CACAO
hpi.cpp
Go to the documentation of this file.
1 /* src/native/vm/openjdk/hpi.cpp - HotSpot HPI interface functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #include "config.h"
26 
27 #ifndef WITH_JAVA_RUNTIME_LIBRARY_OPENJDK_7
28 // Include this one early.
30 
31 #include "native/native.hpp"
32 
33 #include "toolbox/buffer.hpp"
34 #include "toolbox/logging.hpp"
35 
36 #include "vm/options.hpp"
37 #include "vm/os.hpp"
38 #include "vm/properties.hpp"
39 #include "vm/utf8.hpp"
40 #include "vm/vm.hpp"
41 
42 
43 /* VM callback functions ******************************************************/
44 
45 static vm_calls_t callbacks = {
46  /* TODO What should we use here? */
47 /* jio_fprintf, */
48 /* unimplemented_panic, */
49 /* unimplemented_monitorRegister, */
50  NULL,
51  NULL,
52  NULL,
53 
54  NULL, /* unused */
55  NULL, /* unused */
56  NULL /* unused */
57 };
58 
59 
60 /**
61  * Initialize the Host Porting Interface (HPI).
62  */
64 {
65 }
66 
67 void HPI::initialize() // REMOVEME
68 {
69  TRACESUBSYSTEMINITIALIZATION("hpi_init");
70 
71  // Load libhpi.so
72  VM* vm = VM::get_current();
73  Properties& properties = vm->get_properties();
74  const char* boot_library_path = properties.get("sun.boot.library.path");
75 
76  // Use Buffer to assemble library path.
77  Buffer<> buf;
78 
79  buf.write(boot_library_path);
80  buf.write("/native_threads/libhpi.so");
81 
82  Utf8String u = buf.utf8_str();
83 
84  if (opt_TraceHPI)
85  log_println("HPI::initialize: Loading HPI %s ", buf.c_str());
86 
87  NativeLibrary nl(u);
88  void* handle = nl.open();
89 
90  if (handle == NULL)
91  if (opt_TraceHPI)
92  os::abort("HPI::initialize: HPI open failed");
93 
94  // Resolve the DLL_Initialize function from the library.
95  void* dll_initialize = os::dlsym(handle, "DLL_Initialize");
96 
97  jint (JNICALL *DLL_Initialize)(GetInterfaceFunc*, void*);
98  DLL_Initialize = (jint (JNICALL *)(GetInterfaceFunc*, void*)) (uintptr_t) dll_initialize;
99 
100  if (opt_TraceHPI && DLL_Initialize == NULL)
101  log_println("hpi_init: HPI dlsym of DLL_Initialize failed: %s", os::dlerror());
102 
103  if (DLL_Initialize == NULL || (*DLL_Initialize)(&_get_interface, &callbacks) < 0) {
104  if (opt_TraceHPI)
105  vm_abort("hpi_init: HPI DLL_Initialize failed");
106  }
107 
109  nls.add(nl);
110 
111  if (opt_TraceHPI)
112  log_println("HPI::initialize: HPI loaded successfully");
113 
114  // Resolve the interfaces.
115  /* NOTE: The intptr_t-case is only to prevent the a compiler
116  warning with -O2: warning: dereferencing type-punned pointer
117  will break strict-aliasing rules */
118 
119  int result;
120 
121  result = (*_get_interface)((void**) (uintptr_t) &_file, "File", 1);
122 
123  if (result != 0)
124  os::abort("hpi_init: Can't find HPI_FileInterface");
125 
126  result = (*_get_interface)((void**) (uintptr_t) &_library, "Library", 1);
127 
128  if (result != 0)
129  os::abort("hpi_init: Can't find HPI_LibraryInterface");
130 
131  result = (*_get_interface)((void**) (uintptr_t) &_system, "System", 1);
132 
133  if (result != 0)
134  os::abort("hpi_init: Can't find HPI_SystemInterface");
135 }
136 
137 
138 /**
139  * Initialize the Host Porting Interface (HPI) socket library.
140  */
142 {
143  // Resolve the socket library interface.
144  int result = (*_get_interface)((void**) (uintptr_t) &_socket, "Socket", 1);
145 
146  if (result != 0) {
147  if (opt_TraceHPI)
148  log_println("HPI::initialize_socket_library: Can't find HPI_SocketInterface");
149 
150  return JNI_ERR;
151  }
152 
153  return JNI_OK;
154 }
155 
156 
157 // Legacy C interface.
158 extern "C" {
159  void HPI_initialize() { VM::get_current()->get_hpi().initialize(); }
160 }
161 
162 #endif /*not defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK_7)*/
163 /*
164  * These are local overrides for various environment variables in Emacs.
165  * Please do not remove this and leave it at the end of the file, where
166  * Emacs will automagically detect them.
167  * ---------------------------------------------------------------------
168  * Local variables:
169  * mode: c++
170  * indent-tabs-mode: t
171  * c-basic-offset: 4
172  * tab-width: 4
173  * End:
174  * vim:noexpandtab:sw=4:ts=4:
175  */
HPI_SystemInterface * _system
Definition: hpi.hpp:59
Utf8String utf8_str()
get utf-8 string contents of buffer as utf8-string
Definition: buffer.hpp:506
HPI()
Initialize the Host Porting Interface (HPI).
Definition: hpi.cpp:63
const char * c_str()
get contents of buffer as zero-terminated c-style-string This strings lifetime is tied to it&#39;s buffer...
Definition: buffer.hpp:489
Commandline properties.
Definition: properties.hpp:45
void log_println(const char *text,...)
Definition: logging.cpp:193
#define TRACESUBSYSTEMINITIALIZATION(text)
Definition: options.hpp:258
void vm_abort(const char *text,...)
Definition: vm.cpp:2586
HPI_LibraryInterface * _library
Definition: hpi.hpp:58
static void * dlsym(void *handle, const char *symbol)
Definition: os.hpp:319
void initialize()
Definition: hpi.cpp:67
NativeLibraries & get_nativelibraries()
Definition: vm.hpp:127
static vm_calls_t callbacks
Definition: hpi.cpp:45
HPI_FileInterface * _file
Definition: hpi.hpp:56
static char * dlerror(void)
Definition: os.hpp:299
Properties & get_properties()
Definition: vm.hpp:113
GetInterfaceFunc _get_interface
Definition: hpi.hpp:55
Represents a native library.
Definition: native.hpp:60
static void abort()
Definition: os.hpp:196
void add(NativeLibrary &library)
Add the given native library to the native libraries table.
Definition: native.cpp:557
int opt_TraceHPI
Definition: options.cpp:209
Buffer & write(char)
Definition: buffer.hpp:280
Table containing all loaded native libraries.
Definition: native.hpp:85
void HPI_initialize()
Definition: hpi.cpp:159
HPI_SocketInterface * _socket
Definition: hpi.hpp:57
Nl nl
Definition: OStream.cpp:56
int initialize_socket_library()
Initialize the Host Porting Interface (HPI) socket library.
Definition: hpi.cpp:141
const char * get(const char *key)
Get a property entry from the internal property map.
Definition: properties.cpp:590
Represent an instance of a VM.
Definition: vm.hpp:56
static VM * get_current()
Definition: vm.hpp:99