1bb2d52d0b07fa31c963a9c2106146f4bb318192
[cacao.git] / src / native / vm / openjdk / hpi.cpp
1 /* src/native/vm/openjdk/hpi.cpp - HotSpot HPI interface functions
2
3    Copyright (C) 2008
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
26 #include "config.h"
27
28 // Include this one early.
29 #include "native/vm/openjdk/hpi.hpp"
30
31 #include "mm/memory.hpp"
32
33 #include "native/native.hpp"
34
35 #include "toolbox/sequence.hpp"
36
37 #include "vm/options.h"
38 #include "vm/os.hpp"
39 #include "vm/properties.hpp"
40 #include "vm/utf8.h"
41 #include "vm/vm.hpp"
42
43
44 /* VM callback functions ******************************************************/
45
46 static vm_calls_t callbacks = {
47         /* TODO What should we use here? */
48 /*   jio_fprintf, */
49 /*   unimplemented_panic, */
50 /*   unimplemented_monitorRegister, */
51         NULL,
52         NULL,
53         NULL,
54         
55         NULL, /* unused */
56         NULL, /* unused */
57         NULL  /* unused */
58 };
59
60
61 /**
62  * Initialize the Host Porting Interface (HPI).
63  */
64 HPI::HPI()
65 {
66 }
67
68 void HPI::initialize() // REMOVEME
69 {
70         TRACESUBSYSTEMINITIALIZATION("hpi_init");
71
72         // Load libhpi.so
73         VM* vm = VM::get_current();
74         Properties& properties = vm->get_properties();
75         const char* boot_library_path = properties.get("sun.boot.library.path");
76
77         // Use sequence builder to assemble library path.
78         SequenceBuilder sb;
79
80         sb.cat(boot_library_path);
81         sb.cat("/native_threads/libhpi.so");
82
83         // XXX This should actually be sb.export_symbol()
84         utf* u = utf_new_char(sb.c_str());
85
86     if (opt_TraceHPI)
87                 log_println("HPI::initialize: Loading HPI %s ", sb.c_str());
88
89         NativeLibrary nl(u);
90         void* handle = nl.open();
91
92         if (handle == NULL)
93                 if (opt_TraceHPI)
94                         os::abort("HPI::initialize: HPI open failed");
95
96         // Resolve the DLL_Initialize function from the library.
97         void* dll_initialize = os::dlsym(handle, "DLL_Initialize");
98
99     jint (JNICALL *DLL_Initialize)(GetInterfaceFunc*, void*);
100     DLL_Initialize = (jint (JNICALL *)(GetInterfaceFunc*, void*)) (uintptr_t) dll_initialize;
101
102     if (opt_TraceHPI && DLL_Initialize == NULL)
103                 log_println("hpi_init: HPI dlsym of DLL_Initialize failed: %s", os::dlerror());
104
105     if (DLL_Initialize == NULL || (*DLL_Initialize)(&_get_interface, &callbacks) < 0) {
106         if (opt_TraceHPI)
107                         vm_abort("hpi_init: HPI DLL_Initialize failed");
108     }
109
110         NativeLibraries& nls = vm->get_nativelibraries();
111         nls.add(nl);
112
113     if (opt_TraceHPI)
114                 log_println("HPI::initialize: HPI loaded successfully");
115
116         // Resolve the interfaces.
117         /* NOTE: The intptr_t-case is only to prevent the a compiler
118            warning with -O2: warning: dereferencing type-punned pointer
119            will break strict-aliasing rules */
120
121         int result;
122
123         result = (*_get_interface)((void**) (uintptr_t) &_file, "File", 1);
124
125         if (result != 0)
126                 os::abort("hpi_init: Can't find HPI_FileInterface");
127
128         result = (*_get_interface)((void**) (uintptr_t) &_library, "Library", 1);
129
130         if (result != 0)
131                 os::abort("hpi_init: Can't find HPI_LibraryInterface");
132
133         result = (*_get_interface)((void**) (uintptr_t) &_system, "System", 1);
134
135         if (result != 0)
136                 os::abort("hpi_init: Can't find HPI_SystemInterface");
137 }
138
139
140 /**
141  * Initialize the Host Porting Interface (HPI) socket library.
142  */
143 int HPI::initialize_socket_library()
144 {
145         // Resolve the socket library interface.
146         int result = (*_get_interface)((void**) (uintptr_t) &_socket, "Socket", 1);
147
148         if (result != 0) {
149                 if (opt_TraceHPI)
150                         log_println("HPI::initialize_socket_library: Can't find HPI_SocketInterface");
151
152                 return JNI_ERR;
153         }
154
155         return JNI_OK;
156 }
157
158
159 // Legacy C interface.
160 extern "C" {
161         void HPI_initialize() { VM::get_current()->get_hpi().initialize(); }
162 }
163
164
165 /*
166  * These are local overrides for various environment variables in Emacs.
167  * Please do not remove this and leave it at the end of the file, where
168  * Emacs will automagically detect them.
169  * ---------------------------------------------------------------------
170  * Local variables:
171  * mode: c++
172  * indent-tabs-mode: t
173  * c-basic-offset: 4
174  * tab-width: 4
175  * End:
176  * vim:noexpandtab:sw=4:ts=4:
177  */