* src/vm/jit/patcher-common.h: Added.
[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
34 #include "codegen.h"                   /* for PATCHER_NOPS */
35
36 #include "mm/memory.h"
37
38 #include "toolbox/list.h"
39 #include "toolbox/logging.h"           /* XXX remove me! */
40
41 #include "vm/exceptions.h"
42 #include "vm/vm.h"                     /* for vm_abort */
43
44 #include "vm/jit/code.h"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/patcher.h"
47 #include "vm/jit/patcher-common.h"
48
49 #include "vmcore/options.h"
50
51
52 /* patcher_list_create *********************************************************
53
54    TODO
55
56 *******************************************************************************/
57
58 void patcher_list_create(codeinfo *code)
59 {
60         code->patchers = list_create(OFFSET(patchref_t, linkage));
61 }
62
63
64 /* patcher_list_free ***********************************************************
65
66    TODO
67
68 *******************************************************************************/
69
70 void patcher_list_free(codeinfo *code)
71 {
72         patchref_t *pr;
73
74         /* free all elements of the list */
75
76         while((pr = list_first(code->patchers)) != NULL) {
77                 list_remove(code->patchers, pr);
78
79                 FREE(pr, patchref_t);
80         }
81
82         /* free the list itself */
83
84         FREE(code->patchers, list_t);
85 }
86
87
88 /* patcher_list_find ***********************************************************
89
90    TODO
91
92    NOTE: Caller should hold the patcher list lock or maintain
93    exclusive access otherwise.
94
95 *******************************************************************************/
96
97 static patchref_t *patcher_list_find(codeinfo *code, u1 *pc)
98 {
99         patchref_t *pr;
100
101         /* walk through all patcher references for the given codeinfo */
102
103         pr = list_first_unsynced(code->patchers);
104         while (pr) {
105
106                 if (pr->mpc == (ptrint) pc)
107                         return pr;
108
109                 pr = list_next_unsynced(code->patchers, pr);
110         }
111
112         return NULL;
113 }
114
115
116 /* patcher_add_patch_ref *******************************************************
117
118    Appends a new patcher reference to the list of patching positions.
119
120 *******************************************************************************/
121
122 void patcher_add_patch_ref(jitdata *jd, functionptr patcher, voidptr ref,
123                            s4 disp)
124 {
125         codegendata *cd;
126     codeinfo    *code;
127     patchref_t  *pr;
128     s4           patchmpc;
129
130         cd       = jd->cd;
131     code     = jd->code;
132     patchmpc = cd->mcodeptr - cd->mcodebase;
133
134     /* allocate patchref on heap (at least freed together with codeinfo) */
135
136         pr = NEW(patchref_t);
137         list_add_first_unsynced(code->patchers, pr);
138
139     /* set patcher information (mpc is resolved later) */
140
141     pr->mpc     = patchmpc;
142     pr->disp    = disp;
143     pr->patcher = patcher;
144     pr->ref     = ref;
145         pr->mcode   = 0;
146         pr->done    = false;
147
148     /* Generate NOPs for opt_shownops. */
149
150     if (opt_shownops)
151         PATCHER_NOPS;
152 }
153
154
155 /* patcher_handler *************************************************************
156
157    TODO
158
159 *******************************************************************************/
160
161 java_objectheader *patcher_handler(u1 *pc)
162 {
163         codeinfo          *code;
164         patchref_t        *pr;
165         bool               result;
166         java_objectheader *e;
167
168         /* define the patcher function */
169
170         bool (*patcher_function)(patchref_t *);
171
172         /* search the codeinfo for the given PC */
173
174         code = code_find_codeinfo_for_pc(pc);
175         assert(code);
176
177         /* enter a monitor on the patcher list */
178
179         LOCK_MONITOR_ENTER(code->patchers);
180
181         /* search the patcher information for the given PC */
182
183         pr = patcher_list_find(code, pc);
184
185         if (pr == NULL)
186                 vm_abort("patcher_handler: Unable to find patcher reference.");
187
188         if (pr->done) {
189                 log_println("patcher_handler: double-patching detected!");
190                 LOCK_MONITOR_ENTER(code->patchers);
191                 return NULL;
192         }
193
194         /* cast the passed function to a patcher function */
195
196         patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
197
198         /* call the proper patcher function */
199
200         result = (patcher_function)(pr);
201
202         /* check for return value and exit accordingly */
203
204         if (result == false) {
205                 e = exceptions_get_and_clear_exception();
206
207                 LOCK_MONITOR_EXIT(code->patchers);
208
209                 return e;
210         }
211
212         pr->done = true; /* XXX this is only preliminary to prevent double-patching */
213
214         LOCK_MONITOR_EXIT(code->patchers);
215
216         return NULL;
217 }
218
219
220 /*
221  * These are local overrides for various environment variables in Emacs.
222  * Please do not remove this and leave it at the end of the file, where
223  * Emacs will automagically detect them.
224  * ---------------------------------------------------------------------
225  * Local variables:
226  * mode: c
227  * indent-tabs-mode: t
228  * c-basic-offset: 4
229  * tab-width: 4
230  * End:
231  * vim:noexpandtab:sw=4:ts=4:
232  */
233