From a9d9b6deb361f8ca6dc2159ff069ad27ec1b82e2 Mon Sep 17 00:00:00 2001 From: Alhad Deshpande Date: Thu, 8 Sep 2022 10:55:38 +0000 Subject: [PATCH 1/3] [ppc64le] Fixed thunk address 8 byte alignment issue --- src/mono/mono/mini/mini-ppc.c | 17 ++++++++++++++--- src/mono/mono/mini/mini-ppc.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/mini/mini-ppc.c b/src/mono/mono/mini/mini-ppc.c index d1b8fc9790783..bc97b497af93a 100644 --- a/src/mono/mono/mini/mini-ppc.c +++ b/src/mono/mono/mini/mini-ppc.c @@ -2732,6 +2732,9 @@ handle_thunk (MonoCompile *cfg, guchar *code, const guchar *target) if (!cfg->arch.thunks) { cfg->arch.thunks = cfg->thunks; cfg->arch.thunks_size = cfg->thunk_area; +#ifdef THUNK_ADDR_ALIGNMENT + cfg->arch.thunks = ALIGN_TO(cfg->arch.thunks, THUNK_ADDR_ALIGNMENT); +#endif } thunks = cfg->arch.thunks; thunks_size = cfg->arch.thunks_size; @@ -3907,11 +3910,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb) if (cfg->compile_aot && ins->sreg1 == ppc_r12) { /* The trampolines clobber this */ ppc_mr (code, ppc_r29, ins->sreg1); - ppc_ldptr (code, ppc_r0, ins->inst_offset, ppc_r29); + ppc_ldptr (code, ppc_r12, ins->inst_offset, ppc_r29); } else { - ppc_ldptr (code, ppc_r0, ins->inst_offset, ins->sreg1); + ppc_ldptr (code, ppc_r12, ins->inst_offset, ins->sreg1); } - ppc_mtlr (code, ppc_r0); + ppc_mtlr (code, ppc_r12); ppc_blrl (code); /* FIXME: this should be handled somewhere else in the new jit */ code = emit_move_return_value (cfg, ins, code); @@ -5556,6 +5559,14 @@ mono_arch_emit_exceptions (MonoCompile *cfg) } set_code_cursor (cfg, code); + +#ifdef THUNK_ADDR_ALIGNMENT + /* We need to align thunks_offset to 8 byte boundary, hence allocating first 8 bytes + for padding purpose */ + if (cfg->thunk_area != 0) { + cfg->thunk_area += THUNK_ADDR_ALIGNMENT; + } +#endif } #endif diff --git a/src/mono/mono/mini/mini-ppc.h b/src/mono/mono/mini/mini-ppc.h index 0b962aac233d2..e872c4b99c538 100644 --- a/src/mono/mono/mini/mini-ppc.h +++ b/src/mono/mono/mini/mini-ppc.h @@ -35,6 +35,7 @@ #ifdef TARGET_POWERPC64 #if !defined(PPC_USES_FUNCTION_DESCRIPTOR) #define THUNK_SIZE 8 +#define THUNK_ADDR_ALIGNMENT 8 #define GET_MEMORY_SLOT_THUNK_ADDRESS(c) \ ((guint64)(((c)) [0] & 0x0000ffff) << 48) \ + ((guint64)(((c)) [1] & 0x0000ffff) << 32) \ From 3531291e6c796fbebb747a84a0342b480ae81a6f Mon Sep 17 00:00:00 2001 From: Alhad Deshpande Date: Wed, 19 Oct 2022 06:29:54 +0000 Subject: [PATCH 2/3] Fixed FSharp crash issue --- src/mono/mono/mini/mini-ppc.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/mono/mono/mini/mini-ppc.c b/src/mono/mono/mini/mini-ppc.c index a2d8c5a714e3b..db669f8b5da0f 100644 --- a/src/mono/mono/mini/mini-ppc.c +++ b/src/mono/mono/mini/mini-ppc.c @@ -3782,23 +3782,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb) ppc_addis (code, ppc_r12, cfg->frame_reg, ppc_ha(cfg->stack_usage)); ppc_addi (code, ppc_r12, ppc_r12, cfg->stack_usage); } - if (!cfg->method->save_lmf) { - pos = 0; - for (i = 31; i >= 13; --i) { - if (cfg->used_int_regs & (1 << i)) { - pos += sizeof (target_mgreg_t); - ppc_ldptr (code, i, -pos, ppc_r12); - } - } - } else { - /* FIXME restore from MonoLMF: though this can't happen yet */ - } /* Copy arguments on the stack to our argument area */ if (call->stack_usage) { code = emit_memcpy (code, call->stack_usage, ppc_r12, PPC_STACK_PARAM_OFFSET, ppc_sp, PPC_STACK_PARAM_OFFSET); /* r12 was clobbered */ - g_assert (cfg->frame_reg == ppc_sp); if (ppc_is_imm16 (cfg->stack_usage)) { ppc_addi (code, ppc_r12, cfg->frame_reg, cfg->stack_usage); } else { @@ -3809,6 +3797,18 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb) } } + if (!cfg->method->save_lmf) { + pos = 0; + for (i = 31; i >= 13; --i) { + if (cfg->used_int_regs & (1 << i)) { + pos += sizeof (target_mgreg_t); + ppc_ldptr (code, i, -pos, ppc_r12); + } + } + } else { + /* FIXME restore from MonoLMF: though this can't happen yet */ + } + ppc_mr (code, ppc_sp, ppc_r12); mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method); cfg->thunk_area += THUNK_SIZE; From 96f7c4dfe62f03ebe82b8228dd2ee9dde93cc4e1 Mon Sep 17 00:00:00 2001 From: Alhad Deshpande Date: Fri, 21 Oct 2022 09:11:24 +0000 Subject: [PATCH 3/3] [ppc64le] Implementation of mono_arch_get_delegate_virtual_invoke_impl method for ppc64le architecture --- src/mono/mono/mini/mini-ppc.c | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/mini/mini-ppc.c b/src/mono/mono/mini/mini-ppc.c index db669f8b5da0f..73a26c02b18aa 100644 --- a/src/mono/mono/mini/mini-ppc.c +++ b/src/mono/mono/mini/mini-ppc.c @@ -470,10 +470,54 @@ mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_targe return start; } +/** + * + * @brief Architecture-specific delegation virtual trampoline processing + * + * @param[in] @sig - Method signature + * @param[in] @method - Method + * @param[in] @offset - Offset into vtable + * @param[in] @load_imt_reg - Whether to load the LMT register + * @returns Trampoline + * + * Return a pointer to a delegation virtual trampoline + */ + gpointer mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg) { - return NULL; + guint8 *code, *start; + int size = 32; + + start = code = (guint8 *) mono_global_codeman_reserve (size); + + /* + * Replace the "this" argument with the target + */ + ppc_mr (code, ppc_r12, ppc_r3); + ppc_ldptr (code, ppc_r3, MONO_STRUCT_OFFSET(MonoDelegate, target), ppc_r12); + + /* + * Load the IMT register, if needed + */ + if (load_imt_reg) { + ppc_ldptr (code, MONO_ARCH_IMT_REG, MONO_STRUCT_OFFSET(MonoDelegate, method), ppc_r12); + } + + /* + * Load the vTable + */ + ppc_ldptr (code, ppc_r12, MONO_STRUCT_OFFSET(MonoObject, vtable), ppc_r3); + if (!ppc_is_imm16(offset)) + ppc_addis (code, ppc_r12, ppc_r12, ppc_ha(offset)); + ppc_ldptr (code, ppc_r12, offset, ppc_r12); + ppc_mtctr (code, ppc_r12); + ppc_bcctr (code, PPC_BR_ALWAYS, 0); + + mono_arch_flush_icache (start, code - start); + MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL)); + + return(start); } gpointer