83d308c64743311b394254d34fe5e445b1144b76
[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    $Id$
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "codegen.h"                   /* for PATCHER_NOPS */
36
37 #include "mm/memory.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/vm.h"                     /* for vm_abort */
46
47 #include "vm/jit/code.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/patcher-common.h"
50
51 #include "vmcore/options.h"
52
53
54 /* patcher_list_create *********************************************************
55
56    TODO
57
58 *******************************************************************************/
59
60 void patcher_list_create(codeinfo *code)
61 {
62         code->patchers = list_create(OFFSET(patchref_t, linkage));
63 }
64
65
66 /* patcher_list_reset **********************************************************
67
68    TODO
69
70 *******************************************************************************/
71
72 void patcher_list_reset(codeinfo *code)
73 {
74         patchref_t *pr;
75
76         /* free all elements of the list */
77
78         while((pr = list_first(code->patchers)) != NULL) {
79                 list_remove(code->patchers, pr);
80
81                 FREE(pr, patchref_t);
82
83 #if defined(ENABLE_STATISTICS)
84                 if (opt_stat)
85                         size_patchref -= sizeof(patchref_t);
86 #endif
87         }
88 }
89
90 /* patcher_list_free ***********************************************************
91
92    TODO
93
94 *******************************************************************************/
95
96 void patcher_list_free(codeinfo *code)
97 {
98         /* free all elements of the list */
99
100         patcher_list_reset(code);
101
102         /* free the list itself */
103
104         FREE(code->patchers, list_t);
105 }
106
107
108 /* patcher_list_find ***********************************************************
109
110    TODO
111
112    NOTE: Caller should hold the patcher list lock or maintain
113    exclusive access otherwise.
114
115 *******************************************************************************/
116
117 static patchref_t *patcher_list_find(codeinfo *code, u1 *pc)
118 {
119         patchref_t *pr;
120
121         /* walk through all patcher references for the given codeinfo */
122
123         pr = list_first_unsynced(code->patchers);
124         while (pr) {
125
126                 if (pr->mpc == (ptrint) pc)
127                         return pr;
128
129                 pr = list_next_unsynced(code->patchers, pr);
130         }
131
132         return NULL;
133 }
134
135
136 /* patcher_add_patch_ref *******************************************************
137
138    Appends a new patcher reference to the list of patching positions.
139
140 *******************************************************************************/
141
142 void patcher_add_patch_ref(jitdata *jd, functionptr patcher, voidptr ref,
143                            s4 disp)
144 {
145         codegendata *cd;
146     codeinfo    *code;
147     patchref_t  *pr;
148     s4           patchmpc;
149
150         cd       = jd->cd;
151     code     = jd->code;
152     patchmpc = cd->mcodeptr - cd->mcodebase;
153
154 #if !defined(NDEBUG)
155         if (patcher_list_find(code, (u1 *) (intptr_t) patchmpc) != NULL)
156                 vm_abort("patcher_add_patch_ref: different patchers at same position.");
157 #endif
158
159     /* allocate patchref on heap (at least freed together with codeinfo) */
160
161         pr = NEW(patchref_t);
162         list_add_first_unsynced(code->patchers, pr);
163
164 #if defined(ENABLE_STATISTICS)
165         if (opt_stat)
166                 size_patchref += sizeof(patchref_t);
167 #endif
168
169     /* set patcher information (mpc is resolved later) */
170
171     pr->mpc     = patchmpc;
172     pr->disp    = disp;
173     pr->patcher = patcher;
174     pr->ref     = ref;
175         pr->mcode   = 0;
176         pr->done    = false;
177
178     /* Generate NOPs for opt_shownops. */
179
180     if (opt_shownops)
181         PATCHER_NOPS;
182 }
183
184
185 /* patcher_handler *************************************************************
186
187    TODO
188
189 *******************************************************************************/
190
191 /*#define TRACE_PATCHER*/
192
193 #ifdef TRACE_PATCHER
194 /* XXX this indent is not thread safe! */
195 /* XXX if you want it thread safe, place patcher_depth in threadobject! */
196 static int patcher_depth = 0;
197 # define TRACE_PATCHER_INDENT for (i=0; i<patcher_depth; i++) printf("\t")
198 #endif
199
200 java_handle_t *patcher_handler(u1 *pc)
201 {
202         codeinfo      *code;
203         patchref_t    *pr;
204         bool           result;
205         java_handle_t *e;
206 #ifdef TRACE_PATCHER
207         int            i;
208 #endif
209
210         /* define the patcher function */
211
212         bool (*patcher_function)(patchref_t *);
213
214         /* search the codeinfo for the given PC */
215
216         code = code_find_codeinfo_for_pc(pc);
217         assert(code);
218
219         /* enter a monitor on the patcher list */
220
221         LOCK_MONITOR_ENTER(code->patchers);
222
223         /* search the patcher information for the given PC */
224
225         pr = patcher_list_find(code, pc);
226
227         if (pr == NULL)
228                 vm_abort("patcher_handler: Unable to find patcher reference.");
229
230         if (pr->done) {
231                 log_println("patcher_handler: double-patching detected!");
232                 LOCK_MONITOR_EXIT(code->patchers);
233                 return NULL;
234         }
235
236 #ifdef TRACE_PATCHER
237         TRACE_PATCHER_INDENT; printf("patching in "); method_print(code->m); printf("\n");
238         TRACE_PATCHER_INDENT; printf("\texception program counter = %p\n", (void *) pr->mpc);
239         TRACE_PATCHER_INDENT; printf("\tmcodes before = "); for (i=0; i<5; i++) printf("0x%08x ", *((u4 *) pr->mpc + i)); printf("\n");
240         patcher_depth++;
241         assert(patcher_depth > 0);
242 #endif
243
244         /* cast the passed function to a patcher function */
245
246         patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
247
248         /* call the proper patcher function */
249
250         result = (patcher_function)(pr);
251
252 #ifdef TRACE_PATCHER
253         assert(patcher_depth > 0);
254         patcher_depth--;
255         TRACE_PATCHER_INDENT; printf("\tmcodes after  = "); for (i=0; i<5; i++) printf("0x%08x ", *((u4 *) pr->mpc + i)); printf("\n");
256         if (result == false) {
257                 TRACE_PATCHER_INDENT; printf("\tPATCHER EXCEPTION!\n");
258         }
259 #endif
260
261         /* check for return value and exit accordingly */
262
263         if (result == false) {
264                 e = exceptions_get_and_clear_exception();
265
266                 LOCK_MONITOR_EXIT(code->patchers);
267
268                 return e;
269         }
270
271         pr->done = true; /* XXX this is only preliminary to prevent double-patching */
272
273         LOCK_MONITOR_EXIT(code->patchers);
274
275         return NULL;
276 }
277
278
279 /*
280  * These are local overrides for various environment variables in Emacs.
281  * Please do not remove this and leave it at the end of the file, where
282  * Emacs will automagically detect them.
283  * ---------------------------------------------------------------------
284  * Local variables:
285  * mode: c
286  * indent-tabs-mode: t
287  * c-basic-offset: 4
288  * tab-width: 4
289  * End:
290  * vim:noexpandtab:sw=4:ts=4:
291  */
292