From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by sourceware.org (Postfix) with ESMTP id 45850386F401 for ; Tue, 24 Nov 2020 19:32:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 45850386F401 Received: from microsoft-linux.home (unknown [47.187.219.45]) by linux.microsoft.com (Postfix) with ESMTPSA id 7FB7E20B7187; Tue, 24 Nov 2020 11:32:15 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7FB7E20B7187 From: madvenka@linux.microsoft.com To: libffi-discuss@sourceware.org Cc: fw@deneb.enyo.de, dj@redhat.com, madvenka@linux.microsoft.com Subject: [RFC PATCH v1 3/4] aarch64: Support for Static Trampolines Date: Tue, 24 Nov 2020 13:32:05 -0600 Message-Id: <20201124193206.10289-4-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201124193206.10289-1-madvenka@linux.microsoft.com> References: <9bd94fd78a3c8f638b8a0d2269258da99d58e70f> <20201124193206.10289-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-25.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, ENV_AND_HDR_SPF_MATCH, GIT_PATCH_0, SPF_HELO_PASS, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libffi-discuss@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libffi-discuss mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Nov 2020 19:32:17 -0000 From: "Madhavan T. Venkataraman" - Define the arch-specific initialization function ffi_tramp_arch () that returns trampoline size information to common code. - Define the trampoline code and data mapping sizes. - Introduce a tiny amount of code at the beginning of each ABI handler to retrieve the information saved by the trampoline on stack. - Define the trampoline code table statically. - Call ffi_closure_tramp_init () to initialize static trampoline parameters from ffi_prep_closure_loc (). Signed-off-by: Madhavan T. Venkataraman --- src/aarch64/ffi.c | 16 +++++++++++++++ src/aarch64/internal.h | 10 +++++++++ src/aarch64/sysv.S | 46 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/aarch64/ffi.c b/src/aarch64/ffi.c index ef09f4d..64d36b6 100644 --- a/src/aarch64/ffi.c +++ b/src/aarch64/ffi.c @@ -817,6 +817,9 @@ ffi_prep_closure_loc (ffi_closure *closure, }; char *tramp = closure->tramp; + if (ffi_closure_tramp_set_parms (closure, start)) + goto out; + memcpy (tramp, trampoline, sizeof(trampoline)); *(UINT64 *)(tramp + 16) = (uintptr_t)start; @@ -832,6 +835,7 @@ ffi_prep_closure_loc (ffi_closure *closure, unsigned char *tramp_code = ffi_data_to_code_pointer (tramp); #endif ffi_clear_cache (tramp_code, tramp_code + FFI_TRAMPOLINE_SIZE); +out: #endif closure->cif = cif; @@ -1022,4 +1026,16 @@ ffi_closure_SYSV_inner (ffi_cif *cif, return flags; } +#if defined(FFI_EXEC_STATIC_TRAMP) +void * +ffi_tramp_arch (size_t *tramp_size, size_t *map_size) +{ + extern void *trampoline_code_table; + + *tramp_size = AARCH64_TRAMP_SIZE; + *map_size = AARCH64_TRAMP_MAP_SIZE; + return &trampoline_code_table; +} +#endif + #endif /* (__aarch64__) || defined(__arm64__)|| defined (_M_ARM64)*/ diff --git a/src/aarch64/internal.h b/src/aarch64/internal.h index 3d4d035..de55755 100644 --- a/src/aarch64/internal.h +++ b/src/aarch64/internal.h @@ -66,3 +66,13 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define N_X_ARG_REG 8 #define N_V_ARG_REG 8 #define CALL_CONTEXT_SIZE (N_V_ARG_REG * 16 + N_X_ARG_REG * 8) + +#if defined(FFI_EXEC_STATIC_TRAMP) +/* + * For the trampoline code table mapping, a mapping size of 16K is chosen to + * cover the base page sizes of 4K and 16K. + */ +#define AARCH64_TRAMP_MAP_SHIFT 14 +#define AARCH64_TRAMP_MAP_SIZE (1 << AARCH64_TRAMP_MAP_SHIFT) +#define AARCH64_TRAMP_SIZE 32 +#endif diff --git a/src/aarch64/sysv.S b/src/aarch64/sysv.S index b720a92..fece3f9 100644 --- a/src/aarch64/sysv.S +++ b/src/aarch64/sysv.S @@ -232,6 +232,10 @@ CNAME(ffi_call_SYSV): .align 4 CNAME(ffi_closure_SYSV_V): cfi_startproc +#if defined(FFI_EXEC_STATIC_TRAMP) + ldr x17, [sp, #8] + add sp, sp, #16 +#endif stp x29, x30, [sp, #-ffi_closure_SYSV_FS]! cfi_adjust_cfa_offset (ffi_closure_SYSV_FS) cfi_rel_offset (x29, 0) @@ -255,6 +259,10 @@ CNAME(ffi_closure_SYSV_V): .align 4 cfi_startproc CNAME(ffi_closure_SYSV): +#if defined(FFI_EXEC_STATIC_TRAMP) + ldr x17, [sp, #8] + add sp, sp, #16 +#endif stp x29, x30, [sp, #-ffi_closure_SYSV_FS]! cfi_adjust_cfa_offset (ffi_closure_SYSV_FS) cfi_rel_offset (x29, 0) @@ -367,6 +375,44 @@ CNAME(ffi_closure_SYSV): .size CNAME(ffi_closure_SYSV), . - CNAME(ffi_closure_SYSV) #endif +#if defined(FFI_EXEC_STATIC_TRAMP) +/* + * The trampoline uses register x17. It saves the original value of x17 on + * the stack. + * + * The trampoline has two parameters - target code to jump to and data for + * the target code. The trampoline extracts the parameters from its parameter + * block (see tramp_table_map()). The trampoline saves the data address on + * the stack. Finally, it jumps to the target code. + * + * The target code can choose to: + * + * - restore the value of x17 + * - load the data address in a register + * - restore the stack pointer to what it was when the trampoline was invoked. + */ + .align AARCH64_TRAMP_MAP_SHIFT +CNAME(trampoline_code_table): + .rept AARCH64_TRAMP_MAP_SIZE / AARCH64_TRAMP_SIZE + sub sp, sp, #16 /* Make space on the stack */ + str x17, [sp] /* Save x17 on stack */ + adr x17, #16376 /* Get data address */ + ldr x17, [x17] /* Copy data into x17 */ + str x17, [sp, #8] /* Save data on stack */ + adr x17, #16372 /* Get code address */ + ldr x17, [x17] /* Load code address into x17 */ + br x17 /* Jump to code */ + .endr + + .globl CNAME(trampoline_code_table) + FFI_HIDDEN(CNAME(trampoline_code_table)) +#ifdef __ELF__ + .type CNAME(trampoline_code_table), #function + .size CNAME(trampoline_code_table), . - CNAME(trampoline_code_table) +#endif + .align AARCH64_TRAMP_MAP_SHIFT +#endif /* FFI_EXEC_STATIC_TRAMP */ + #if FFI_EXEC_TRAMPOLINE_TABLE #ifdef __MACH__ -- 2.25.1