1 /* src/vm/jit/patcher-common.c - architecture independent code patching stuff
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
8 This file is part of CACAO.
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.
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.
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
34 #include "codegen.h" /* for PATCHER_NOPS */
36 #include "mm/memory.h"
38 #include "toolbox/list.h"
39 #include "toolbox/logging.h" /* XXX remove me! */
41 #include "vm/exceptions.h"
42 #include "vm/vm.h" /* for vm_abort */
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"
49 #include "vmcore/options.h"
52 /* patcher_list_create *********************************************************
56 *******************************************************************************/
58 void patcher_list_create(codeinfo *code)
60 code->patchers = list_create(OFFSET(patchref_t, linkage));
64 /* patcher_list_free ***********************************************************
68 *******************************************************************************/
70 void patcher_list_free(codeinfo *code)
74 /* free all elements of the list */
76 while((pr = list_first(code->patchers)) != NULL) {
77 list_remove(code->patchers, pr);
82 /* free the list itself */
84 FREE(code->patchers, list_t);
88 /* patcher_list_find ***********************************************************
92 NOTE: Caller should hold the patcher list lock or maintain
93 exclusive access otherwise.
95 *******************************************************************************/
97 static patchref_t *patcher_list_find(codeinfo *code, u1 *pc)
101 /* walk through all patcher references for the given codeinfo */
103 pr = list_first_unsynced(code->patchers);
106 if (pr->mpc == (ptrint) pc)
109 pr = list_next_unsynced(code->patchers, pr);
116 /* patcher_add_patch_ref *******************************************************
118 Appends a new patcher reference to the list of patching positions.
120 *******************************************************************************/
122 void patcher_add_patch_ref(jitdata *jd, functionptr patcher, voidptr ref,
132 patchmpc = cd->mcodeptr - cd->mcodebase;
134 /* allocate patchref on heap (at least freed together with codeinfo) */
136 pr = NEW(patchref_t);
137 list_add_first_unsynced(code->patchers, pr);
139 /* set patcher information (mpc is resolved later) */
143 pr->patcher = patcher;
148 /* Generate NOPs for opt_shownops. */
155 /* patcher_handler *************************************************************
159 *******************************************************************************/
161 java_objectheader *patcher_handler(u1 *pc)
166 java_objectheader *e;
168 /* define the patcher function */
170 bool (*patcher_function)(patchref_t *);
172 /* search the codeinfo for the given PC */
174 code = code_find_codeinfo_for_pc(pc);
177 /* enter a monitor on the patcher list */
179 LOCK_MONITOR_ENTER(code->patchers);
181 /* search the patcher information for the given PC */
183 pr = patcher_list_find(code, pc);
186 vm_abort("patcher_handler: Unable to find patcher reference.");
189 log_println("patcher_handler: double-patching detected!");
190 LOCK_MONITOR_ENTER(code->patchers);
194 /* cast the passed function to a patcher function */
196 patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
198 /* call the proper patcher function */
200 result = (patcher_function)(pr);
202 /* check for return value and exit accordingly */
204 if (result == false) {
205 e = exceptions_get_and_clear_exception();
207 LOCK_MONITOR_EXIT(code->patchers);
212 pr->done = true; /* XXX this is only preliminary to prevent double-patching */
214 LOCK_MONITOR_EXIT(code->patchers);
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 * ---------------------------------------------------------------------
227 * indent-tabs-mode: t
231 * vim:noexpandtab:sw=4:ts=4: