* src/vm/jit/i386/codegen.c (codegen_emit_stub_native): Loads function pointer
[cacao.git] / src / vm / jit / patcher-common.c
1 /* src/vm/jit/patcher-common.c - architecture independent code patching stuff
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "codegen.h"                   /* for PATCHER_NOPS */
34
35 #include "mm/memory.h"
36
37 #include "native/native.h"
38
39 #include "threads/lock-common.h"
40
41 #include "toolbox/list.h"
42 #include "toolbox/logging.h"           /* XXX remove me! */
43
44 #include "vm/exceptions.h"
45 #include "vm/initialize.h"
46 #include "vm/resolve.h"
47 #include "vm/vm.h"                     /* for vm_abort */
48
49 #include "vm/jit/code.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/md.h"
52 #include "vm/jit/patcher-common.h"
53
54 #include "vmcore/options.h"
55
56
57 /* patcher_list_create *********************************************************
58
59    TODO
60
61 *******************************************************************************/
62
63 void patcher_list_create(codeinfo *code)
64 {
65         code->patchers = list_create(OFFSET(patchref_t, linkage));
66 }
67
68
69 /* patcher_list_reset **********************************************************
70
71    TODO
72
73 *******************************************************************************/
74
75 void patcher_list_reset(codeinfo *code)
76 {
77         patchref_t *pr;
78
79         /* free all elements of the list */
80
81         while((pr = list_first(code->patchers)) != NULL) {
82                 list_remove(code->patchers, pr);
83
84                 FREE(pr, patchref_t);
85
86 #if defined(ENABLE_STATISTICS)
87                 if (opt_stat)
88                         size_patchref -= sizeof(patchref_t);
89 #endif
90         }
91 }
92
93 /* patcher_list_free ***********************************************************
94
95    TODO
96
97 *******************************************************************************/
98
99 void patcher_list_free(codeinfo *code)
100 {
101         /* free all elements of the list */
102
103         patcher_list_reset(code);
104
105         /* free the list itself */
106
107         FREE(code->patchers, list_t);
108 }
109
110
111 /* patcher_list_find ***********************************************************
112
113    TODO
114
115    NOTE: Caller should hold the patcher list lock or maintain
116    exclusive access otherwise.
117
118 *******************************************************************************/
119
120 static patchref_t *patcher_list_find(codeinfo *code, u1 *pc)
121 {
122         patchref_t *pr;
123
124         /* walk through all patcher references for the given codeinfo */
125
126         pr = list_first_unsynced(code->patchers);
127         while (pr) {
128
129 /*#define TRACE_PATCHER_FIND*/
130 #ifdef TRACE_PATCHER_FIND
131                 log_println("patcher_list_find: %p == %p", pr->mpc, pc);
132 #endif
133
134                 if (pr->mpc == (ptrint) pc)
135                         return pr;
136
137                 pr = list_next_unsynced(code->patchers, pr);
138         }
139
140         return NULL;
141 }
142
143
144 /* patcher_add_patch_ref *******************************************************
145
146    Appends a new patcher reference to the list of patching positions.
147
148 *******************************************************************************/
149
150 void patcher_add_patch_ref(jitdata *jd, functionptr patcher, voidptr ref,
151                            s4 disp)
152 {
153         codegendata *cd;
154     codeinfo    *code;
155     patchref_t  *pr;
156     s4           patchmpc;
157
158         cd       = jd->cd;
159     code     = jd->code;
160     patchmpc = cd->mcodeptr - cd->mcodebase;
161
162 #if !defined(NDEBUG)
163         if (patcher_list_find(code, (u1 *) (intptr_t) patchmpc) != NULL)
164                 vm_abort("patcher_add_patch_ref: different patchers at same position.");
165 #endif
166
167     /* allocate patchref on heap (at least freed together with codeinfo) */
168
169         pr = NEW(patchref_t);
170         list_add_first_unsynced(code->patchers, pr);
171
172 #if defined(ENABLE_STATISTICS)
173         if (opt_stat)
174                 size_patchref += sizeof(patchref_t);
175 #endif
176
177     /* set patcher information (mpc is resolved later) */
178
179     pr->mpc     = patchmpc;
180     pr->disp    = disp;
181     pr->patcher = patcher;
182     pr->ref     = ref;
183         pr->mcode   = 0;
184         pr->done    = false;
185
186     /* Generate NOPs for opt_shownops. */
187
188     if (opt_shownops)
189         PATCHER_NOPS;
190 }
191
192
193 /* patcher_handler *************************************************************
194
195    TODO
196
197 *******************************************************************************/
198
199 /*#define TRACE_PATCHER*/
200
201 #ifdef TRACE_PATCHER
202 /* XXX this indent is not thread safe! */
203 /* XXX if you want it thread safe, place patcher_depth in threadobject! */
204 static int patcher_depth = 0;
205 #define TRACE_PATCHER_INDENT for (i=0; i<patcher_depth; i++) printf("\t")
206
207 typedef struct patcher_function_list_t {
208         functionptr  patcher;
209         char        *name;
210 } patcher_function_list_t;
211
212 static patcher_function_list_t patcher_function_list[] = {
213         { PATCHER_initialize_class,              "initialize_class" },
214         { PATCHER_resolve_class,                 "resolve_class" },
215         { PATCHER_resolve_native_function,       "resolve_native_function" },
216         { PATCHER_invokestatic_special,          "invokestatic_special" },
217         { PATCHER_invokevirtual,                 "invokevirtual" },
218         { PATCHER_invokeinterface,               "invokeinterface" },
219         { NULL, "-UNKNOWN PATCHER FUNCTION-" }
220 };
221 #endif /* TRACE_PATCHER */
222
223 java_handle_t *patcher_handler(u1 *pc)
224 {
225         codeinfo      *code;
226         patchref_t    *pr;
227         bool           result;
228         java_handle_t *e;
229 #ifdef TRACE_PATCHER
230         patcher_function_list_t *l;
231         int                      i;
232 #endif
233
234         /* define the patcher function */
235
236         bool (*patcher_function)(patchref_t *);
237
238         /* search the codeinfo for the given PC */
239
240         code = code_find_codeinfo_for_pc(pc);
241         assert(code);
242
243         /* enter a monitor on the patcher list */
244
245         LOCK_MONITOR_ENTER(code->patchers);
246
247         /* search the patcher information for the given PC */
248
249         pr = patcher_list_find(code, pc);
250
251         if (pr == NULL)
252                 vm_abort("patcher_handler: Unable to find patcher reference.");
253
254         if (pr->done) {
255                 log_println("patcher_handler: double-patching detected!");
256                 LOCK_MONITOR_EXIT(code->patchers);
257                 return NULL;
258         }
259
260 #ifdef TRACE_PATCHER
261         for (l = patcher_function_list; l->patcher != NULL; l++)
262                 if (l->patcher == pr->patcher)
263                         break;
264
265         TRACE_PATCHER_INDENT; printf("patching in "); method_print(code->m); printf("\n");
266         TRACE_PATCHER_INDENT; printf("\texception program counter = %p\n", (void *) pr->mpc);
267         TRACE_PATCHER_INDENT; printf("\tpatcher function = %s\n", l->name);
268         TRACE_PATCHER_INDENT; printf("\tmcodes before = "); for (i=0; i<5; i++) printf("0x%08x ", *((u4 *) pr->mpc + i)); printf("\n");
269         patcher_depth++;
270         assert(patcher_depth > 0);
271 #endif
272
273         /* cast the passed function to a patcher function */
274
275         patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
276
277         /* call the proper patcher function */
278
279         result = (patcher_function)(pr);
280
281 #ifdef TRACE_PATCHER
282         assert(patcher_depth > 0);
283         patcher_depth--;
284         TRACE_PATCHER_INDENT; printf("\tmcodes after  = "); for (i=0; i<5; i++) printf("0x%08x ", *((u4 *) pr->mpc + i)); printf("\n");
285         if (result == false) {
286                 TRACE_PATCHER_INDENT; printf("\tPATCHER EXCEPTION!\n");
287         }
288 #endif
289
290         /* check for return value and exit accordingly */
291
292         if (result == false) {
293                 e = exceptions_get_and_clear_exception();
294
295                 LOCK_MONITOR_EXIT(code->patchers);
296
297                 return e;
298         }
299
300         pr->done = true; /* XXX this is only preliminary to prevent double-patching */
301
302         LOCK_MONITOR_EXIT(code->patchers);
303
304         return NULL;
305 }
306
307
308 /* patcher_initialize_class ****************************************************
309
310    Initalizes a given classinfo pointer.
311    This function does not patch any data.
312
313 *******************************************************************************/
314
315 bool patcher_initialize_class(patchref_t *pr)
316 {
317         classinfo *c;
318
319         /* get stuff from the patcher reference */
320
321         c = (classinfo *) pr->ref;
322
323         /* check if the class is initialized */
324
325         if (!(c->state & CLASS_INITIALIZED))
326                 if (!initialize_class(c))
327                         return false;
328
329         /* patch back original code */
330
331         patcher_patch_code(pr);
332
333         return true;
334 }
335
336
337 /* patcher_resolve_class *******************************************************
338
339    Resolves a given unresolved class reference.
340    This function does not patch any data.
341
342 *******************************************************************************/
343
344 #ifdef ENABLE_VERIFIER
345 bool patcher_resolve_class(patchref_t *pr)
346 {
347         unresolved_class *uc;
348
349         /* get stuff from the patcher reference */
350
351         uc = (unresolved_class *) pr->ref;
352
353         /* resolve the class and check subtype constraints */
354
355         if (!resolve_class_eager_no_access_check(uc))
356                 return false;
357
358         /* patch back original code */
359
360         patcher_patch_code(pr);
361
362         return true;
363 }
364 #endif /* ENABLE_VERIFIER */
365
366
367 /* patcher_resolve_native_function *********************************************
368
369    Resolves the native function for a given methodinfo.
370    This function patches one data segment word.
371
372 *******************************************************************************/
373
374 bool patcher_resolve_native_function(patchref_t *pr)
375 {
376         methodinfo  *m;
377         uint8_t     *datap;
378         functionptr  f;
379
380         /* get stuff from the patcher reference */
381
382         m     = (methodinfo *) pr->ref;
383         datap = (uint8_t *)    pr->datap;
384
385         /* resolve native function */
386
387         if (!(f = native_resolve_function(m)))
388                 return false;
389
390         /* patch native function pointer */
391
392         *((intptr_t *) datap) = (intptr_t) f;
393
394         /* synchronize data cache */
395
396         md_dcacheflush(datap, SIZEOF_VOID_P);
397
398         /* patch back original code */
399
400         patcher_patch_code(pr);
401
402         return true;
403 }
404
405
406 /*
407  * These are local overrides for various environment variables in Emacs.
408  * Please do not remove this and leave it at the end of the file, where
409  * Emacs will automagically detect them.
410  * ---------------------------------------------------------------------
411  * Local variables:
412  * mode: c
413  * indent-tabs-mode: t
414  * c-basic-offset: 4
415  * tab-width: 4
416  * End:
417  * vim:noexpandtab:sw=4:ts=4:
418  */
419