2 * Create trampolines to invoke arbitrary functions.
3 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
5 * Contributions by Malte Hildingson
8 #include "arm-codegen.h"
11 #if defined(_WIN32_WCE) || defined (UNDER_CE)
17 #include "mono/metadata/class.h"
18 #include "mono/metadata/tabledefs.h"
19 #include "mono/interpreter/interp.h"
20 #include "mono/metadata/appdomain.h"
24 # define ARM_DUMP_DISASM 1
27 /* prototypes for private functions (to avoid compiler warnings) */
28 void flush_icache (void);
29 void* alloc_code_buff (int num_instr);
34 * The resulting function takes the form:
35 * void func (void (*callme)(), void *retval, void *this_obj, stackval *arguments);
36 * NOTE: all args passed in ARM registers (A1-A4),
37 * then copied to R4-R7 (see definitions below).
40 #define REG_FUNC_ADDR ARMREG_R4
41 #define REG_RETVAL ARMREG_R5
42 #define REG_THIS ARMREG_R6
43 #define REG_ARGP ARMREG_R7
46 #define ARG_SIZE sizeof(stackval)
54 FlushInstructionCache(GetCurrentProcess(), NULL, 0);
59 asm ("mcr p15, 0, r0, c7, c7, 0");
61 /* TODO: use (movnv pc, rx) method */
67 void* alloc_code_buff (int num_instr)
70 int code_size = num_instr * sizeof(arminstr_t);
72 #if defined(_WIN32) || defined(UNDER_CE)
75 code_buff = malloc(code_size);
76 VirtualProtect(code_buff, code_size, PAGE_EXECUTE_READWRITE, &old_prot);
80 int page_size = sysconf(_SC_PAGESIZE);
83 new_code_size = code_size + page_size - 1;
84 code_buff = malloc(new_code_size);
85 code_buff = (void *) (((int) code_buff + page_size - 1) & ~(page_size - 1));
87 if (mprotect(code_buff, code_size, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
88 g_critical (G_GNUC_PRETTY_FUNCTION
89 ": mprotect error: %s", g_strerror (errno));
98 * Refer to ARM Procedure Call Standard (APCS) for more info.
100 MonoPIFunc mono_arch_create_trampoline (MonoMethodSignature *sig, gboolean string_ctor)
103 MonoPIFunc code_buff;
105 guint32 code_size, stack_size;
107 int i, hasthis, aregs, regc, stack_offs;
109 guchar reg_alloc [ARM_NUM_ARG_REGS];
111 /* pessimistic estimation for prologue/epilogue size */
113 /* push/pop work regs */
121 hasthis = sig->hasthis ? 1 : 0;
123 aregs = ARM_NUM_ARG_REGS - hasthis;
125 for (i = 0, regc = aregs; i < sig->param_count; ++i) {
126 param = sig->params [i];
128 /* keep track of argument sizes */
129 if (i < ARM_NUM_ARG_REGS) reg_alloc [i] = 0;
134 reg_alloc [i] = regc;
138 stack_size += sizeof(gpointer);
141 simple_type = param->type;
143 switch (simple_type) {
144 case MONO_TYPE_BOOLEAN:
156 case MONO_TYPE_SZARRAY:
157 case MONO_TYPE_CLASS:
158 case MONO_TYPE_OBJECT:
159 case MONO_TYPE_STRING:
163 reg_alloc [i] = regc;
174 /* keep track of argument sizes */
176 /* fits into registers, two LDRs */
178 reg_alloc [i] = regc;
180 } else if (regc > 0) {
181 /* first half fits into register, one LDR */
183 reg_alloc [i] = regc;
185 /* the rest on the stack, LDR/STR */
189 /* stack arg, 4 instrs - 2x(LDR/STR) */
194 case MONO_TYPE_VALUETYPE:
195 if (param->data.klass->enumtype) {
196 simple_type = param->data.klass->enum_basetype->type;
200 if (mono_class_value_size(param->data.klass, NULL) != 4) {
201 g_error("can only marshal enums, not generic structures (size: %d)", mono_class_value_size(param->data.klass, NULL));
206 reg_alloc [i] = regc;
220 code_buff = (MonoPIFunc)alloc_code_buff(code_size);
221 p = (arminstr_t*)code_buff;
224 p = arm_emit_lean_prologue(p, stack_size,
225 /* save workset (r4-r7) */
226 (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6) | (1 << ARMREG_R7));
229 /* copy args into workset */
230 /* callme - always present */
231 ARM_MOV_REG_REG(p, ARMREG_R4, ARMREG_A1);
233 if (sig->ret->byref || string_ctor || (sig->ret->type != MONO_TYPE_VOID)) {
234 ARM_MOV_REG_REG(p, ARMREG_R5, ARMREG_A2);
239 if (stack_size == 0) {
240 ARM_MOV_REG_REG(p, ARMREG_A1, ARMREG_A3);
243 ARM_MOV_REG_REG(p, ARMREG_R6, ARMREG_A3);
247 if (sig->param_count != 0) {
248 ARM_MOV_REG_REG(p, ARMREG_R7, ARMREG_A4);
251 stack_offs = stack_size;
253 /* handle arguments */
254 /* in reverse order so we could use r0 (arg1) for memory transfers */
255 for (i = sig->param_count; --i >= 0;) {
256 param = sig->params [i];
258 if (i < aregs && reg_alloc[i] > 0) {
259 ARM_LDR_IMM(p, ARMREG_A1 + i, REG_ARGP, i*ARG_SIZE);
261 stack_offs -= sizeof(armword_t);
262 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i*ARG_SIZE);
263 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs);
266 simple_type = param->type;
268 switch (simple_type) {
269 case MONO_TYPE_BOOLEAN:
281 case MONO_TYPE_SZARRAY:
282 case MONO_TYPE_CLASS:
283 case MONO_TYPE_OBJECT:
284 case MONO_TYPE_STRING:
285 if (i < aregs && reg_alloc [i] > 0) {
286 /* pass in register */
287 ARM_LDR_IMM(p, ARMREG_A1 + hasthis + (aregs - reg_alloc [i]), REG_ARGP, i*ARG_SIZE);
289 stack_offs -= sizeof(armword_t);
290 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i*ARG_SIZE);
291 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs);
297 if (i < aregs && reg_alloc [i] > 0) {
298 if (reg_alloc [i] > 1) {
299 /* pass in registers */
300 ARM_LDR_IMM(p, ARMREG_A1 + hasthis + (aregs - reg_alloc [i]), REG_ARGP, i*ARG_SIZE);
301 ARM_LDR_IMM(p, ARMREG_A1 + hasthis + (aregs - reg_alloc [i]) + 1, REG_ARGP, i*ARG_SIZE + 4);
303 stack_offs -= sizeof(armword_t);
304 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i*ARG_SIZE + 4);
305 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs);
306 ARM_LDR_IMM(p, ARMREG_A1 + hasthis + (aregs - reg_alloc [i]), REG_ARGP, i*ARG_SIZE);
309 /* two words transferred on the stack */
310 stack_offs -= 2*sizeof(armword_t);
311 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i*ARG_SIZE);
312 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs);
313 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i*ARG_SIZE + 4);
314 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs + 4);
317 case MONO_TYPE_VALUETYPE:
318 if (param->data.klass->enumtype) {
319 /* it's an enum value, proceed based on its base type */
320 simple_type = param->data.klass->enum_basetype->type;
323 if (i < aregs && reg_alloc[i] > 0) {
324 int vtreg = ARMREG_A1 + hasthis +
325 hasthis + (aregs - reg_alloc[i]);
326 ARM_LDR_IMM(p, vtreg, REG_ARGP, i * ARG_SIZE);
327 ARM_LDR_IMM(p, vtreg, vtreg, 0);
329 stack_offs -= sizeof(armword_t);
330 ARM_LDR_IMM(p, ARMREG_R0, REG_ARGP, i * ARG_SIZE);
331 ARM_LDR_IMM(p, ARMREG_R0, ARMREG_R0, 0);
332 ARM_STR_IMM(p, ARMREG_R0, ARMREG_SP, stack_offs);
343 if (sig->hasthis && !this_loaded) {
344 /* [this] always passed in A1, regardless of sig->call_convention */
345 ARM_MOV_REG_REG(p, ARMREG_A1, REG_THIS);
349 ARM_MOV_REG_REG(p, ARMREG_LR, ARMREG_PC);
350 ARM_MOV_REG_REG(p, ARMREG_PC, REG_FUNC_ADDR);
353 if (sig->ret->byref || string_ctor) {
354 ARM_STR_IMM(p, ARMREG_R0, REG_RETVAL, 0);
356 simple_type = sig->ret->type;
358 switch (simple_type) {
359 case MONO_TYPE_BOOLEAN:
362 ARM_STRB_IMM(p, ARMREG_R0, REG_RETVAL, 0);
367 ARM_STRH_IMM(p, ARMREG_R0, REG_RETVAL, 0);
370 * A 32-bit integer and integer-equivalent return value
372 * Single-precision floating-point values are returned in R0.
379 case MONO_TYPE_OBJECT:
380 case MONO_TYPE_CLASS:
381 case MONO_TYPE_ARRAY:
382 case MONO_TYPE_SZARRAY:
383 case MONO_TYPE_STRING:
384 ARM_STR_IMM(p, ARMREG_R0, REG_RETVAL, 0);
387 * A 64-bit integer is returned in R0 and R1.
388 * Double-precision floating-point values are returned in R0 and R1.
393 ARM_STR_IMM(p, ARMREG_R0, REG_RETVAL, 0);
394 ARM_STR_IMM(p, ARMREG_R1, REG_RETVAL, 4);
396 case MONO_TYPE_VALUETYPE:
397 if (sig->ret->data.klass->enumtype) {
398 simple_type = sig->ret->data.klass->enum_basetype->type;
409 p = arm_emit_std_epilogue(p, stack_size,
411 (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6) | (1 << ARMREG_R7));
415 #ifdef ARM_DUMP_DISASM
416 _armdis_decode((arminstr_t*)code_buff, ((guint8*)p) - ((guint8*)code_buff));
424 #define MINV_OFFS(member) G_STRUCT_OFFSET(MonoInvocation, member)
429 * Returns a pointer to a native function that can be used to
430 * call the specified method.
431 * The function created will receive the arguments according
432 * to the call convention specified in the method.
433 * This function works by creating a MonoInvocation structure,
434 * filling the fields in and calling ves_exec_method on it.
435 * Still need to figure out how to handle the exception stuff
436 * across the managed/unmanaged boundary.
438 void* mono_arch_create_method_pointer (MonoMethod* method)
440 MonoMethodSignature* sig;
441 guchar* p, * p_method, * p_stackval_from_data, * p_exec;
443 int i, stack_size, arg_pos, arg_add, stackval_pos, offs;
444 int areg, reg_args, shift, pos;
447 code_buff = alloc_code_buff(128);
448 p = (guchar*)code_buff;
450 sig = method->signature;
454 /* embed magic number followed by method pointer */
465 *(void**)p = stackval_from_data;
466 p_stackval_from_data = p;
468 *(void**)p = ves_exec_method;
472 stack_size = sizeof(MonoInvocation) + ARG_SIZE*(sig->param_count + 1) + ARM_NUM_ARG_REGS*2*sizeof(armword_t);
475 p = (guchar*)arm_emit_lean_prologue((arminstr_t*)p, stack_size,
481 /* R7 - ptr to stack args */
482 ARM_MOV_REG_REG(p, ARMREG_R7, ARMREG_IP);
485 * Initialize MonoInvocation fields, first the ones known now.
487 ARM_MOV_REG_IMM8(p, ARMREG_R4, 0);
488 ARM_STR_IMM(p, ARMREG_R4, ARMREG_SP, MINV_OFFS(ex));
489 ARM_STR_IMM(p, ARMREG_R4, ARMREG_SP, MINV_OFFS(ex_handler));
490 ARM_STR_IMM(p, ARMREG_R4, ARMREG_SP, MINV_OFFS(parent));
492 /* Set the method pointer. */
493 ARM_LDR_IMM(p, ARMREG_R4, ARMREG_PC, -(int)(p - p_method + sizeof(arminstr_t)*2));
494 ARM_STR_IMM(p, ARMREG_R4, ARMREG_SP, MINV_OFFS(method));
498 ARM_STR_IMM(p, ARMREG_A1, ARMREG_SP, MINV_OFFS(obj));
500 /* else set minv.obj to NULL */
501 ARM_STR_IMM(p, ARMREG_R4, ARMREG_SP, MINV_OFFS(obj));
504 /* copy args from registers to stack */
505 areg = ARMREG_A1 + sig->hasthis;
506 arg_pos = -(int)(ARM_NUM_ARG_REGS - sig->hasthis) * 2 * sizeof(armword_t);
508 for (i = 0; i < sig->param_count; ++i) {
509 if (areg >= ARM_NUM_ARG_REGS) break;
510 ARM_STR_IMM(p, areg, ARMREG_R7, arg_pos);
512 if (!sig->params[i]->byref) {
513 switch (sig->params[i]->type) {
517 if (areg >= ARM_NUM_ARG_REGS) {
518 /* load second half of 64-bit arg */
519 ARM_LDR_IMM(p, ARMREG_R4, ARMREG_R7, 0);
520 ARM_STR_IMM(p, ARMREG_R4, ARMREG_R7, arg_pos + sizeof(armword_t));
521 arg_add = sizeof(armword_t);
523 /* second half is already the register */
524 ARM_STR_IMM(p, areg, ARMREG_R7, arg_pos + sizeof(armword_t));
528 case MONO_TYPE_VALUETYPE:
534 arg_pos += 2 * sizeof(armword_t);
536 /* number of args passed in registers */
542 * Calc and save stack args ptr,
543 * args follow MonoInvocation struct on the stack.
545 ARM_ADD_REG_IMM8(p, ARMREG_R1, ARMREG_SP, sizeof(MonoInvocation));
546 ARM_STR_IMM(p, ARMREG_R1, ARMREG_SP, MINV_OFFS(stack_args));
548 /* convert method args to stackvals */
549 arg_pos = -(int)(ARM_NUM_ARG_REGS - sig->hasthis) * 2 * sizeof(armword_t);
550 stackval_pos = sizeof(MonoInvocation);
551 for (i = 0; i < sig->param_count; ++i) {
553 ARM_SUB_REG_IMM8(p, ARMREG_A3, ARMREG_R7, -arg_pos);
554 arg_pos += 2 * sizeof(armword_t);
556 if (arg_pos < 0) arg_pos = 0;
557 pos = arg_pos + arg_add;
559 ARM_ADD_REG_IMM8(p, ARMREG_A3, ARMREG_R7, pos);
561 if (is_arm_const((armword_t)pos)) {
562 shift = calc_arm_mov_const_shift((armword_t)pos);
563 ARM_ADD_REG_IMM(p, ARMREG_A3, ARMREG_R7, pos >> ((32 - shift) & 31), shift >> 1);
565 p = (guchar*)arm_mov_reg_imm32((arminstr_t*)p, ARMREG_R6, (armword_t)pos);
566 ARM_ADD_REG_REG(p, ARMREG_A2, ARMREG_R7, ARMREG_R6);
569 arg_pos += sizeof(armword_t);
570 if (!sig->params[i]->byref) {
571 switch (sig->params[i]->type) {
575 arg_pos += sizeof(armword_t);
577 case MONO_TYPE_VALUETYPE:
586 if (stackval_pos <= 0xFF) {
587 ARM_ADD_REG_IMM8(p, ARMREG_A2, ARMREG_SP, stackval_pos);
589 if (is_arm_const((armword_t)stackval_pos)) {
590 shift = calc_arm_mov_const_shift((armword_t)stackval_pos);
591 ARM_ADD_REG_IMM(p, ARMREG_A2, ARMREG_SP, stackval_pos >> ((32 - shift) & 31), shift >> 1);
593 p = (guchar*)arm_mov_reg_imm32((arminstr_t*)p, ARMREG_R6, (armword_t)stackval_pos);
594 ARM_ADD_REG_REG(p, ARMREG_A2, ARMREG_SP, ARMREG_R6);
599 p = (guchar*)arm_mov_reg_imm32((arminstr_t*)p, ARMREG_A1, (armword_t)sig->params [i]);
601 stackval_pos += ARG_SIZE;
603 offs = -(p + 2*sizeof(arminstr_t) - p_stackval_from_data);
604 /* load function address */
605 ARM_LDR_IMM(p, ARMREG_R4, ARMREG_PC, offs);
606 /* call stackval_from_data */
607 ARM_MOV_REG_REG(p, ARMREG_LR, ARMREG_PC);
608 ARM_MOV_REG_REG(p, ARMREG_PC, ARMREG_R4);
611 /* store retval ptr */
612 p = (guchar*)arm_mov_reg_imm32((arminstr_t*)p, ARMREG_R5, (armword_t)stackval_pos);
613 ARM_ADD_REG_REG(p, ARMREG_R5, ARMREG_SP, ARMREG_R4);
614 ARM_STR_IMM(p, ARMREG_R5, ARMREG_SP, MINV_OFFS(retval));
619 /* A1 = MonoInvocation ptr */
620 ARM_MOV_REG_REG(p, ARMREG_A1, ARMREG_SP);
621 offs = -(p + 2*sizeof(arminstr_t) - p_exec);
622 /* load function address */
623 ARM_LDR_IMM(p, ARMREG_R4, ARMREG_PC, offs);
625 ARM_MOV_REG_REG(p, ARMREG_LR, ARMREG_PC);
626 ARM_MOV_REG_REG(p, ARMREG_PC, ARMREG_R4);
630 * Move retval into reg.
632 if (sig->ret->byref) {
633 ARM_LDR_IMM(p, ARMREG_R0, ARMREG_R5, 0);
635 switch (sig->ret->type) {
636 case MONO_TYPE_BOOLEAN:
639 ARM_LDRB_IMM(p, ARMREG_R0, ARMREG_R5, 0);
644 ARM_LDRH_IMM(p, ARMREG_R0, ARMREG_R5, 0);
651 case MONO_TYPE_OBJECT:
652 case MONO_TYPE_CLASS:
653 case MONO_TYPE_ARRAY:
654 case MONO_TYPE_SZARRAY:
655 ARM_LDR_IMM(p, ARMREG_R0, ARMREG_R5, 0);
660 ARM_LDR_IMM(p, ARMREG_R0, ARMREG_R5, 0);
661 ARM_LDR_IMM(p, ARMREG_R1, ARMREG_R5, 4);
670 p = (guchar*)arm_emit_std_epilogue((arminstr_t*)p, stack_size,
678 #ifdef ARM_DUMP_DISASM
679 _armdis_decode((arminstr_t*)code_buff, ((guint8*)p) - ((guint8*)code_buff));
682 ji = g_new0(MonoJitInfo, 1);
684 ji->code_size = ((guint8 *) p) - ((guint8 *) code_buff);
685 ji->code_start = (gpointer) code_buff;
687 mono_jit_info_table_add(mono_get_root_domain (), ji);
694 * mono_create_method_pointer () will insert a pointer to the MonoMethod
695 * so that the interp can easily get at the data: this function will retrieve
696 * the method from the code stream.
698 MonoMethod* mono_method_pointer_get (void* code)
700 unsigned char* c = code;
701 /* check out magic number that follows unconditional branch */
705 c[7] == 'o') return ((MonoMethod**)code)[2];