1 /* src/vm/jit/patcher-common.cpp - architecture independent code patching stuff
3 Copyright (C) 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "codegen.h" /* for PATCHER_NOPS */
39 #include "mm/memory.hpp"
41 #include "native/native.hpp"
43 #include "toolbox/list.hpp"
44 #include "toolbox/logging.hpp" /* XXX remove me! */
46 #include "vm/breakpoint.hpp"
47 #include "vm/exceptions.hpp"
48 #include "vm/hook.hpp"
49 #include "vm/initialize.hpp"
50 #include "vm/options.h"
52 #include "vm/resolve.hpp"
54 #include "vm/jit/code.hpp"
55 #include "vm/jit/disass.h"
56 #include "vm/jit/jit.hpp"
57 #include "vm/jit/patcher-common.hpp"
60 /* patcher_function_list *******************************************************
62 This is a list which maps patcher function pointers to the according
63 names of the patcher functions. It is only usefull for debugging
66 *******************************************************************************/
69 typedef struct patcher_function_list_t {
72 } patcher_function_list_t;
74 static patcher_function_list_t patcher_function_list[] = {
75 { PATCHER_initialize_class, "initialize_class" },
76 #ifdef ENABLE_VERIFIER
77 { PATCHER_resolve_class, "resolve_class" },
78 #endif /* ENABLE_VERIFIER */
79 { PATCHER_resolve_native_function, "resolve_native_function" },
80 { PATCHER_invokestatic_special, "invokestatic_special" },
81 { PATCHER_invokevirtual, "invokevirtual" },
82 { PATCHER_invokeinterface, "invokeinterface" },
83 { PATCHER_breakpoint, "breakpoint" },
84 { NULL, "-UNKNOWN PATCHER FUNCTION-" }
89 /* patcher_list_create *********************************************************
91 Creates an empty patcher list for the given codeinfo.
93 *******************************************************************************/
95 void patcher_list_create(codeinfo *code)
97 code->patchers = new LockedList<patchref_t>();
101 /* patcher_list_reset **********************************************************
103 Resets the patcher list inside a codeinfo. This is usefull when
104 resetting a codeinfo for recompiling.
106 *******************************************************************************/
108 void patcher_list_reset(codeinfo *code)
110 #if defined(ENABLE_STATISTICS)
112 size_patchref -= sizeof(patchref_t) * code->patchers->size();
115 // Free all elements of the list.
116 code->patchers->clear();
119 /* patcher_list_free ***********************************************************
121 Frees the patcher list and all its entries for the given codeinfo.
123 *******************************************************************************/
125 void patcher_list_free(codeinfo *code)
127 // Free all elements of the list.
128 patcher_list_reset(code);
130 // Free the list itself.
131 delete code->patchers;
136 * Find an entry inside the patcher list for the given codeinfo by
137 * specifying the program counter of the patcher position.
139 * NOTE: Caller should hold the patcher list lock or maintain
140 * exclusive access otherwise.
142 * @param pc Program counter to find.
144 * @return Pointer to patcher.
147 struct foo : public std::binary_function<patchref_t, void*, bool> {
148 bool operator() (const patchref_t& pr, const void* pc) const
150 return (pr.mpc == (uintptr_t) pc);
154 static patchref_t* patcher_list_find(codeinfo* code, void* pc)
156 // Search for a patcher with the given PC.
157 List<patchref_t>::iterator it = std::find_if(code->patchers->begin(), code->patchers->end(), std::bind2nd(foo(), pc));
159 if (it == code->patchers->end())
167 * Show the content of the whole patcher reference list for
168 * debugging purposes.
170 * @param code The codeinfo containing the patcher list.
173 void patcher_list_show(codeinfo *code)
175 for (List<patchref_t>::iterator it = code->patchers->begin(); it != code->patchers->end(); it++) {
176 patchref_t& pr = *it;
178 // Lookup name in patcher function list.
179 patcher_function_list_t* l;
180 for (l = patcher_function_list; l->patcher != NULL; l++)
181 if (l->patcher == pr.patcher)
184 // Display information about patcher.
185 printf("\tpatcher pc:"PRINTF_FORMAT_INTPTR_T, pr.mpc);
186 printf(" datap:"PRINTF_FORMAT_INTPTR_T, pr.datap);
187 printf(" ref:"PRINTF_FORMAT_INTPTR_T, (intptr_t) pr.ref);
188 #if PATCHER_CALL_SIZE == 4
189 printf(" mcode:%08x", (uint32_t) pr.mcode);
190 #elif PATCHER_CALL_SIZE == 2
191 printf(" mcode:%04x", (uint16_t) pr.mcode);
193 # error Unknown PATCHER_CALL_SIZE
195 printf(" type:%s\n", l->name);
197 // Display machine code of patched position.
198 #if 0 && defined(ENABLE_DISASSEMBLER)
199 printf("\t\tcurrent -> ");
200 disassinstr((uint8_t*) pr.mpc);
201 printf("\t\tapplied -> ");
202 disassinstr((uint8_t*) &(pr.mcode));
209 /* patcher_add_patch_ref *******************************************************
211 Appends a new patcher reference to the list of patching positions.
213 Returns a pointer to the newly created patchref_t.
215 *******************************************************************************/
217 patchref_t *patcher_add_patch_ref(jitdata *jd, functionptr patcher, void* ref, s4 disp)
219 codegendata *cd = jd->cd;
220 codeinfo *code = jd->code;
222 #if defined(ALIGN_PATCHER_TRAP)
223 emit_patcher_alignment(cd);
226 int32_t patchmpc = cd->mcodeptr - cd->mcodebase;
229 if (patcher_list_find(code, (void*) (intptr_t) patchmpc) != NULL)
230 os::abort("patcher_add_patch_ref: different patchers at same position.");
233 #if defined(USES_PATCHABLE_MEMORY_BARRIER)
237 // Set patcher information (mpc is resolved later).
244 pr.patcher = patcher;
249 // Store patcher in the list (NOTE: structure is copied).
250 code->patchers->push_back(pr);
252 #if defined(ENABLE_STATISTICS)
254 size_patchref += sizeof(patchref_t);
257 #if defined(ENABLE_JIT) && (defined(__I386__) || defined(__M68K__) || defined(__SPARC_64__) || defined(__X86_64__))
259 /* XXX We can remove that when we don't use UD2 anymore on i386
262 /* On some architectures the patcher stub call instruction might
263 be longer than the actual instruction generated. On this
264 architectures we store the last patcher call position and after
265 the basic block code generation is completed, we check the
266 range and maybe generate some nop's. */
267 /* The nops are generated in codegen_emit in each codegen */
269 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
272 return &code->patchers->back();
277 * Resolve all patchers in the current JIT run.
279 * @param jd JIT data-structure
281 void patcher_resolve(jitdata* jd)
283 // Get required compiler data.
284 codeinfo* code = jd->code;
286 for (List<patchref_t>::iterator it = code->patchers->begin(); it != code->patchers->end(); it++) {
287 patchref_t& pr = *it;
289 pr.mpc += (intptr_t) code->entrypoint;
290 pr.datap = (intptr_t) (pr.disp + code->entrypoint);
296 * Check if the patcher is already patched. This is done by comparing
297 * the machine instruction.
299 * @param pr Patcher structure.
301 * @return true if patched, false otherwise.
303 bool patcher_is_patched(patchref_t* pr)
305 // Validate the instruction at the patching position is the same
306 // instruction as the patcher structure contains.
307 uint32_t mcode = *((uint32_t*) pr->mpc);
309 #if PATCHER_CALL_SIZE == 4
310 if (mcode != pr->mcode) {
311 #elif PATCHER_CALL_SIZE == 2
312 if ((uint16_t) mcode != (uint16_t) pr->mcode) {
314 #error Unknown PATCHER_CALL_SIZE
327 bool patcher_is_patched_at(void* pc)
329 codeinfo* code = code_find_codeinfo_for_pc(pc);
331 // Get the patcher for the given PC.
332 patchref_t* pr = patcher_list_find(code, pc);
335 // The given PC is not a patcher position.
339 // Validate the instruction.
340 return patcher_is_patched(pr);
344 /* patcher_handler *************************************************************
346 Handles the request to patch JIT code at the given patching
347 position. This function is normally called by the signal
350 NOTE: The patcher list lock is used to maintain exclusive
351 access of the patched position (in fact of the whole code).
352 After patching has suceeded, the patcher reference should be
353 removed from the patcher list to avoid double patching.
355 *******************************************************************************/
358 /* XXX this indent is not thread safe! */
359 /* XXX if you want it thread safe, place patcher_depth in threadobject! */
360 static int patcher_depth = 0;
361 #define TRACE_PATCHER_INDENT for (i=0; i<patcher_depth; i++) printf("\t")
362 #endif /* !defined(NDEBUG) */
364 bool patcher_handler(u1 *pc)
370 patcher_function_list_t *l;
374 /* define the patcher function */
376 bool (*patcher_function)(patchref_t *);
378 /* search the codeinfo for the given PC */
380 code = code_find_codeinfo_for_pc(pc);
383 // Enter a mutex on the patcher list.
384 code->patchers->lock();
386 /* search the patcher information for the given PC */
388 pr = patcher_list_find(code, pc);
391 os::abort("patcher_handler: Unable to find patcher reference.");
395 if (opt_DebugPatcher) {
396 log_println("patcher_handler: double-patching detected!");
399 code->patchers->unlock();
404 if (opt_DebugPatcher) {
405 for (l = patcher_function_list; l->patcher != NULL; l++)
406 if (l->patcher == pr->patcher)
409 TRACE_PATCHER_INDENT; printf("patching in "); method_print(code->m); printf(" at %p\n", (void *) pr->mpc);
410 TRACE_PATCHER_INDENT; printf("\tpatcher function = %s <%p>\n", l->name, (void *) (intptr_t) pr->patcher);
412 TRACE_PATCHER_INDENT;
413 printf("\tmachine code before = ");
415 # if defined(ENABLE_DISASSEMBLER)
416 disassinstr((u1*) (void*) pr->mpc);
418 printf("%x at %p (disassembler disabled)\n", *((uint32_t*) pr->mpc), (void*) pr->mpc);
422 assert(patcher_depth > 0);
426 /* cast the passed function to a patcher function */
428 patcher_function = (bool (*)(patchref_t *)) (ptrint) pr->patcher;
430 /* call the proper patcher function */
432 result = (patcher_function)(pr);
435 if (opt_DebugPatcher) {
436 assert(patcher_depth > 0);
439 TRACE_PATCHER_INDENT;
440 printf("\tmachine code after = ");
442 # if defined(ENABLE_DISASSEMBLER)
443 disassinstr((u1*) (void*) pr->mpc);
445 printf("%x at %p (disassembler disabled)\n", *((uint32_t*) pr->mpc), (void*) pr->mpc);
448 if (result == false) {
449 TRACE_PATCHER_INDENT; printf("\tPATCHER EXCEPTION!\n");
454 // Check return value and mangle the pending exception.
456 resolve_handle_pending_exception(true);
458 // XXX This is only preliminary to prevent double-patching.
462 code->patchers->unlock();
468 /* patcher_initialize_class ****************************************************
470 Initalizes a given classinfo pointer.
471 This function does not patch any data.
473 *******************************************************************************/
475 bool patcher_initialize_class(patchref_t *pr)
479 /* get stuff from the patcher reference */
481 c = (classinfo *) pr->ref;
483 /* check if the class is initialized */
485 if (!(c->state & CLASS_INITIALIZED))
486 if (!initialize_class(c))
489 /* patch back original code */
491 patcher_patch_code(pr);
497 /* patcher_resolve_class *******************************************************
499 Resolves a given unresolved class reference.
500 This function does not patch any data.
502 *******************************************************************************/
504 #ifdef ENABLE_VERIFIER
505 bool patcher_resolve_class(patchref_t *pr)
507 unresolved_class *uc;
509 /* get stuff from the patcher reference */
511 uc = (unresolved_class *) pr->ref;
513 /* resolve the class and check subtype constraints */
515 if (!resolve_class_eager_no_access_check(uc))
518 /* patch back original code */
520 patcher_patch_code(pr);
524 #endif /* ENABLE_VERIFIER */
527 /* patcher_resolve_native_function *********************************************
529 Resolves the native function for a given methodinfo.
530 This function patches one data segment word.
532 *******************************************************************************/
534 bool patcher_resolve_native_function(patchref_t *pr)
539 /* get stuff from the patcher reference */
541 m = (methodinfo *) pr->ref;
542 datap = (uint8_t *) pr->datap;
544 /* resolve native function */
546 NativeMethods& nm = VM::get_current()->get_nativemethods();
547 void* f = nm.resolve_method(m);
552 /* patch native function pointer */
554 *((intptr_t*) datap) = (intptr_t) f;
556 /* synchronize data cache */
558 md_dcacheflush(datap, SIZEOF_VOID_P);
560 /* patch back original code */
562 patcher_patch_code(pr);
569 * Deals with breakpoint instructions (ICMD_BREAKPOINT) compiled
570 * into a JIT method. This patcher might never patch back the
571 * original machine code because breakpoints are kept active.
573 bool patcher_breakpoint(patchref_t *pr)
575 // Get stuff from the patcher reference.
576 Breakpoint* breakp = (Breakpoint*) pr->ref;
578 // Hook point when a breakpoint was triggered.
579 Hook::breakpoint(breakp);
581 // In case the breakpoint wants to be kept active, we simply
582 // fail to "patch" at this point.
583 if (!breakp->is_oneshot)
586 // Patch back original code.
587 patcher_patch_code(pr);
594 * These are local overrides for various environment variables in Emacs.
595 * Please do not remove this and leave it at the end of the file, where
596 * Emacs will automagically detect them.
597 * ---------------------------------------------------------------------
600 * indent-tabs-mode: t
604 * vim:noexpandtab:sw=4:ts=4: