1 /* src/vm/jit/alpha/patcher.c - Alpha code patching functions
3 Copyright (C) 1996-2005, 2006, 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
25 $Id: patcher.c 7486 2007-03-08 13:50:07Z twisti $
36 #include "mm/memory.h"
38 #include "native/native.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/initialize.h"
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/patcher.h"
46 #include "vm/jit/md.h"
47 #include "vm/jit/methodheader.h"
48 #include "vm/jit/stacktrace.h"
50 #include "vmcore/class.h"
51 #include "vmcore/field.h"
52 #include "vmcore/options.h"
53 #include "vmcore/references.h"
54 #include "vm/resolve.h"
57 /* patcher_wrapper *************************************************************
59 Wrapper for all patchers. It also creates the stackframe info
62 If the return value of the patcher function is false, it gets the
63 exception object, clears the exception pointer and returns the
66 *******************************************************************************/
68 java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
78 /* define the patcher function */
80 bool (*patcher_function)(u1 *);
84 /* get stuff from the stack */
86 xpc = (u1 *) *((ptrint *) (sp + 5 * 8));
87 o = (java_objectheader *) *((ptrint *) (sp + 4 * 8));
88 f = (functionptr) *((ptrint *) (sp + 0 * 8));
90 /* calculate and set the new return address */
94 *((ptrint *) (sp + 5 * 8)) = (ptrint) xpc;
96 /* store PV into the patcher function position */
98 *((ptrint *) (sp + 0 * 8)) = (ptrint) pv;
100 /* cast the passed function to a patcher function */
102 patcher_function = (bool (*)(u1 *)) (ptrint) f;
104 /* enter a monitor on the patching position */
106 PATCHER_MONITORENTER;
108 /* create the stackframeinfo */
110 stacktrace_create_extern_stackframeinfo(&sfi, pv, sp + 6 * 8, ra, xpc);
112 /* call the proper patcher function */
114 result = (patcher_function)(sp);
116 /* remove the stackframeinfo */
118 stacktrace_remove_stackframeinfo(&sfi);
120 /* check for an error, get the exception and return it */
122 if (result == false) {
123 e = exceptions_get_and_clear_exception();
130 /* patch back original code */
132 mcode = *((u4 *) (sp + 3 * 8));
134 *((u4 *) xpc) = mcode;
136 /* synchronize instruction cache */
138 md_icacheflush(NULL, 0);
140 PATCHER_MARK_PATCHED_MONITOREXIT;
145 /* patcher_initialize_class ****************************************************
147 Initalizes a given classinfo pointer. This function does not patch
150 *******************************************************************************/
152 bool patcher_initialize_class(u1 *sp)
156 /* get stuff from the stack */
158 c = (classinfo *) *((ptrint *) (sp + 2 * 8));
160 /* check if the class is initialized */
162 if (!(c->state & CLASS_INITIALIZED))
163 if (!initialize_class(c))
169 /* patcher_resolve_class *****************************************************
171 Initalizes a given classinfo pointer. This function does not patch
174 *******************************************************************************/
176 #ifdef ENABLE_VERIFIER
177 bool patcher_resolve_class(u1 *sp)
179 unresolved_class *uc;
181 /* get stuff from the stack */
183 uc = (unresolved_class *) *((ptrint *) (sp + 2 * 8));
185 /* resolve the class and check subtype constraints */
187 if (!resolve_class_eager_no_access_check(uc))
192 #endif /* ENABLE_VERIFIER */
195 /* patcher_resolve_classref_to_classinfo ***************************************
199 <patched call postition>
200 a61bff80 ldq a0,-128(pv)
204 <patched call position>
205 a63bff80 ldq a1,-128(pv)
207 a77bff78 ldq pv,-136(pv)
212 <patched call position>
213 a63bfe60 ldq a1,-416(pv)
214 a77bfe58 ldq pv,-424(pv)
217 *******************************************************************************/
219 bool patcher_resolve_classref_to_classinfo(u1 *sp)
221 constant_classref *cr;
226 /* get stuff from the stack */
228 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
229 disp = *((s4 *) (sp + 1 * 8));
230 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
232 /* get the classinfo */
234 if (!(c = resolve_classref_eager(cr)))
237 /* patch the classinfo pointer */
239 *((ptrint *) (pv + disp)) = (ptrint) c;
245 /* patcher_resolve_classref_to_vftbl *******************************************
250 <patched call position>
251 a7940000 ldq at,0(a4)
252 a7bbff28 ldq gp,-216(pv)
254 *******************************************************************************/
256 bool patcher_resolve_classref_to_vftbl(u1 *sp)
258 constant_classref *cr;
263 /* get stuff from the stack */
265 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
266 disp = *((s4 *) (sp + 1 * 8));
267 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
269 /* get the fieldinfo */
271 if (!(c = resolve_classref_eager(cr)))
274 /* patch super class' vftbl */
276 *((ptrint *) (pv + disp)) = (ptrint) c->vftbl;
282 /* patcher_resolve_classref_to_flags *******************************************
284 CHECKCAST/INSTANCEOF:
286 <patched call position>
288 *******************************************************************************/
290 bool patcher_resolve_classref_to_flags(u1 *sp)
292 constant_classref *cr;
297 /* get stuff from the stack */
299 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
300 disp = *((s4 *) (sp + 1 * 8));
301 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
303 /* get the fieldinfo */
305 if (!(c = resolve_classref_eager(cr)))
308 /* patch class flags */
310 *((s4 *) (pv + disp)) = (s4) c->flags;
316 /* patcher_resolve_native_function *********************************************
320 *******************************************************************************/
322 #if !defined(WITH_STATIC_CLASSPATH)
323 bool patcher_resolve_native_function(u1 *sp)
330 /* get stuff from the stack */
332 m = (methodinfo *) *((ptrint *) (sp + 2 * 8));
333 disp = *((s4 *) (sp + 1 * 8));
334 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
336 /* resolve native function */
338 if (!(f = native_resolve_function(m)))
341 /* patch native function pointer */
343 *((ptrint *) (pv + disp)) = (ptrint) f;
347 #endif /* !defined(WITH_STATIC_CLASSPATH) */
350 /* patcher_get_putstatic *******************************************************
354 <patched call position>
355 a73bff98 ldq t11,-104(pv)
356 a2590000 ldl a2,0(t11)
358 *******************************************************************************/
360 bool patcher_get_putstatic(u1 *sp)
362 unresolved_field *uf;
367 /* get stuff from the stack */
369 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
370 disp = *((s4 *) (sp + 1 * 8));
371 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
373 /* get the fieldinfo */
375 if (!(fi = resolve_field_eager(uf)))
378 /* check if the field's class is initialized */
380 if (!(fi->class->state & CLASS_INITIALIZED))
381 if (!initialize_class(fi->class))
384 /* patch the field value's address */
386 *((ptrint *) (pv + disp)) = (ptrint) &(fi->value);
392 /* patcher_get_putfield ********************************************************
396 <patched call position>
397 a2af0020 ldl a5,32(s6)
399 *******************************************************************************/
401 bool patcher_get_putfield(u1 *sp)
404 unresolved_field *uf;
407 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
408 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
410 /* get the fieldinfo */
412 if (!(fi = resolve_field_eager(uf)))
415 /* if we show disassembly, we have to skip the nop */
418 /* patch the field's offset into the instruction */
420 *((u4 *) (ra + 4)) |= (s2) (fi->offset & 0x0000ffff);
423 /* otherwise store the patched instruction on the stack */
425 *((u4 *) (sp + 3 * 8)) |= (s2) (fi->offset & 0x0000ffff);
432 /* patcher_invokestatic_special ************************************************
436 <patched call position>
437 a77bffa8 ldq pv,-88(pv)
440 ******************************************************************************/
442 bool patcher_invokestatic_special(u1 *sp)
444 unresolved_method *um;
449 /* get stuff from the stack */
451 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
452 disp = *((s4 *) (sp + 1 * 8));
453 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
455 /* get the fieldinfo */
457 if (!(m = resolve_method_eager(um)))
460 /* patch stubroutine */
462 *((ptrint *) (pv + disp)) = (ptrint) m->stubroutine;
468 /* patcher_invokevirtual *******************************************************
472 <patched call position>
473 a7900000 ldq at,0(a0)
474 a77c0100 ldq pv,256(at)
477 *******************************************************************************/
479 bool patcher_invokevirtual(u1 *sp)
482 unresolved_method *um;
485 /* get stuff from the stack */
487 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
488 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
490 /* get the fieldinfo */
492 if (!(m = resolve_method_eager(um)))
495 /* if we show disassembly, we have to skip the nop */
500 /* patch vftbl index */
502 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, table[0]) +
503 sizeof(methodptr) * m->vftblindex) & 0x0000ffff);
509 /* patcher_invokeinterface *****************************************************
513 <patched call position>
514 a7900000 ldq at,0(a0)
515 a79cffa0 ldq at,-96(at)
516 a77c0018 ldq pv,24(at)
519 *******************************************************************************/
521 bool patcher_invokeinterface(u1 *sp)
524 unresolved_method *um;
527 /* get stuff from the stack */
529 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
530 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
532 /* get the fieldinfo */
534 if (!(m = resolve_method_eager(um)))
537 /* if we show disassembly, we have to skip the nop */
542 /* patch interfacetable index */
544 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
545 sizeof(methodptr*) * m->class->index) & 0x0000ffff);
547 /* patch method offset */
549 *((s4 *) (ra + 4 + 4)) |=
550 (s4) ((sizeof(methodptr) * (m - m->class->methods)) & 0x0000ffff);
556 /* patcher_checkcast_interface *************************************************
560 <patched call position>
561 a78e0000 ldq at,0(s5)
562 a3bc001c ldl gp,28(at)
563 23bdfffd lda gp,-3(gp)
564 efa0002e ble gp,0x00000200002bf6b0
565 a7bcffe8 ldq gp,-24(at)
567 *******************************************************************************/
569 bool patcher_checkcast_interface(u1 *sp)
572 constant_classref *cr;
575 /* get stuff from the stack */
577 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
578 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
580 /* get the fieldinfo */
582 if (!(c = resolve_classref_eager(cr)))
585 /* if we show disassembly, we have to skip the nop */
590 /* patch super class index */
592 *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
594 *((s4 *) (ra + 5 * 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
595 c->index * sizeof(methodptr*)) & 0x0000ffff);
601 /* patcher_instanceof_interface ************************************************
605 <patched call position>
606 a78e0000 ldq at,0(s5)
607 a3bc001c ldl gp,28(at)
608 23bdfffd lda gp,-3(gp)
609 efa0002e ble gp,0x00000200002bf6b0
610 a7bcffe8 ldq gp,-24(at)
612 *******************************************************************************/
614 bool patcher_instanceof_interface(u1 *sp)
617 constant_classref *cr;
620 /* get stuff from the stack */
622 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
623 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
625 /* get the fieldinfo */
627 if (!(c = resolve_classref_eager(cr)))
630 /* if we show disassembly, we have to skip the nop */
635 /* patch super class index */
637 *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
639 *((s4 *) (ra + 4 * 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
640 c->index * sizeof(methodptr*)) & 0x0000ffff);
647 * These are local overrides for various environment variables in Emacs.
648 * Please do not remove this and leave it at the end of the file, where
649 * Emacs will automagically detect them.
650 * ---------------------------------------------------------------------
653 * indent-tabs-mode: t
657 * vim:noexpandtab:sw=4:ts=4: