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
35 #include "codegen.h" /* for PATCHER_NOPS */
37 #include "mm/memory.h"
39 #include "threads/lock-common.h"
41 #include "toolbox/list.h"
42 #include "toolbox/logging.h" /* XXX remove me! */
44 #include "vm/exceptions.h"
45 #include "vm/vm.h" /* for vm_abort */
47 #include "vm/jit/code.h"
48 #include "vm/jit/jit.h"
49 #include "vm/jit/patcher-common.h"
51 #include "vmcore/options.h"
54 /* patcher_list_create *********************************************************
58 *******************************************************************************/
60 void patcher_list_create(codeinfo *code)
62 code->patchers = list_create(OFFSET(patchref_t, linkage));
66 /* patcher_list_reset **********************************************************
70 *******************************************************************************/
72 void patcher_list_reset(codeinfo *code)
76 /* free all elements of the list */
78 while((pr = list_first(code->patchers)) != NULL) {
79 list_remove(code->patchers, pr);
83 #if defined(ENABLE_STATISTICS)
85 size_patchref -= sizeof(patchref_t);
90 /* patcher_list_free ***********************************************************
94 *******************************************************************************/
96 void patcher_list_free(codeinfo *code)
98 /* free all elements of the list */
100 patcher_list_reset(code);
102 /* free the list itself */
104 FREE(code->patchers, list_t);
108 /* patcher_list_find ***********************************************************
112 NOTE: Caller should hold the patcher list lock or maintain
113 exclusive access otherwise.
115 *******************************************************************************/
117 static patchref_t *patcher_list_find(codeinfo *code, u1 *pc)
121 /* walk through all patcher references for the given codeinfo */
123 pr = list_first_unsynced(code->patchers);
126 if (pr->mpc == (ptrint) pc)
129 pr = list_next_unsynced(code->patchers, pr);
136 /* patcher_add_patch_ref *******************************************************
138 Appends a new patcher reference to the list of patching positions.
140 *******************************************************************************/
142 void patcher_add_patch_ref(jitdata *jd, functionptr patcher, voidptr ref,
152 patchmpc = cd->mcodeptr - cd->mcodebase;
155 if (patcher_list_find(code, (u1 *) (intptr_t) patchmpc) != NULL)
156 vm_abort("patcher_add_patch_ref: different patchers at same position.");
159 /* allocate patchref on heap (at least freed together with codeinfo) */
161 pr = NEW(patchref_t);
162 list_add_first_unsynced(code->patchers, pr);
164 #if defined(ENABLE_STATISTICS)
166 size_patchref += sizeof(patchref_t);
169 /* set patcher information (mpc is resolved later) */
173 pr->patcher = patcher;
178 /* Generate NOPs for opt_shownops. */
185 /* patcher_handler *************************************************************
189 *******************************************************************************/
191 java_objectheader *patcher_handler(u1 *pc)
196 java_objectheader *e;
198 /* define the patcher function */
200 bool (*patcher_function)(patchref_t *);
202 /* search the codeinfo for the given PC */
204 code = code_find_codeinfo_for_pc(pc);
207 /* enter a monitor on the patcher list */
209 LOCK_MONITOR_ENTER(code->patchers);
211 /* search the patcher information for the given PC */
213 pr = patcher_list_find(code, pc);
216 vm_abort("patcher_handler: Unable to find patcher reference.");
219 log_println("patcher_handler: double-patching detected!");
220 LOCK_MONITOR_EXIT(code->patchers);
224 /* cast the passed function to a patcher function */
226 patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
228 /* call the proper patcher function */
230 result = (patcher_function)(pr);
232 /* check for return value and exit accordingly */
234 if (result == false) {
235 e = exceptions_get_and_clear_exception();
237 LOCK_MONITOR_EXIT(code->patchers);
242 pr->done = true; /* XXX this is only preliminary to prevent double-patching */
244 LOCK_MONITOR_EXIT(code->patchers);
251 * These are local overrides for various environment variables in Emacs.
252 * Please do not remove this and leave it at the end of the file, where
253 * Emacs will automagically detect them.
254 * ---------------------------------------------------------------------
257 * indent-tabs-mode: t
261 * vim:noexpandtab:sw=4:ts=4: