public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
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 4/4] arm: Support for Static Trampolines
Date: Tue, 24 Nov 2020 13:32:06 -0600	[thread overview]
Message-ID: <20201124193206.10289-5-madvenka@linux.microsoft.com> (raw)
In-Reply-To: <20201124193206.10289-1-madvenka@linux.microsoft.com>

From: "Madhavan T. Venkataraman" <madvenka@linux.microsoft.com>

	- 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 <madvenka@linux.microsoft.com>
---
 src/arm/ffi.c      | 16 ++++++++++++++++
 src/arm/internal.h | 10 ++++++++++
 src/arm/sysv.S     | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/src/arm/ffi.c b/src/arm/ffi.c
index 0058390..6529803 100644
--- a/src/arm/ffi.c
+++ b/src/arm/ffi.c
@@ -612,6 +612,9 @@ ffi_prep_closure_loc (ffi_closure * closure,
   config[1] = closure_func;
 #else
 
+  if (ffi_closure_tramp_set_parms (closure, closure_func))
+    goto out;
+
 #ifndef _M_ARM
   memcpy(closure->tramp, ffi_arm_trampoline, 8);
 #else
@@ -633,6 +636,7 @@ ffi_prep_closure_loc (ffi_closure * closure,
 #else
   *(void (**)(void))(closure->tramp + 8) = closure_func;
 #endif
+out:
 #endif
 
   closure->cif = cif;
@@ -873,4 +877,16 @@ layout_vfp_args (ffi_cif * cif)
     }
 }
 
+#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 = ARM_TRAMP_SIZE;
+  *map_size = ARM_TRAMP_MAP_SIZE;
+  return &trampoline_code_table;
+}
+#endif
+
 #endif /* __arm__ or _M_ARM */
diff --git a/src/arm/internal.h b/src/arm/internal.h
index 6cf0b2a..fa8ab0b 100644
--- a/src/arm/internal.h
+++ b/src/arm/internal.h
@@ -5,3 +5,13 @@
 #define ARM_TYPE_INT	4
 #define ARM_TYPE_VOID	5
 #define ARM_TYPE_STRUCT	6
+
+#if defined(FFI_EXEC_STATIC_TRAMP)
+/*
+ * For the trampoline table mapping, a mapping size of 4K (base page size)
+ * is chosen.
+ */
+#define ARM_TRAMP_MAP_SHIFT	12
+#define ARM_TRAMP_MAP_SIZE	(1 << ARM_TRAMP_MAP_SHIFT)
+#define ARM_TRAMP_SIZE		20
+#endif
diff --git a/src/arm/sysv.S b/src/arm/sysv.S
index 74bc53f..f0a435f 100644
--- a/src/arm/sysv.S
+++ b/src/arm/sysv.S
@@ -227,6 +227,10 @@ ARM_FUNC_END(ffi_go_closure_SYSV)
 ARM_FUNC_START(ffi_closure_SYSV)
 	UNWIND(.fnstart)
 	cfi_startproc
+#if defined(FFI_EXEC_STATIC_TRAMP)
+	ldr	ip, [sp, #4]
+	add	sp, sp, 8
+#endif
 	stmdb	sp!, {r0-r3}			@ save argument regs
 	cfi_adjust_cfa_offset(16)
 
@@ -274,6 +278,10 @@ ARM_FUNC_END(ffi_go_closure_VFP)
 ARM_FUNC_START(ffi_closure_VFP)
 	UNWIND(.fnstart)
 	cfi_startproc
+#if defined(FFI_EXEC_STATIC_TRAMP)
+	ldr	ip, [sp, #4]
+	add	sp, sp, 8
+#endif
 	stmdb	sp!, {r0-r3}			@ save argument regs
 	cfi_adjust_cfa_offset(16)
 
@@ -354,6 +362,35 @@ E(ARM_TYPE_STRUCT)
 	cfi_endproc
 ARM_FUNC_END(ffi_closure_ret)
 
+#if defined(FFI_EXEC_STATIC_TRAMP)
+/*
+ * The trampoline uses register ip (r12). It saves the original value of ip
+ * 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 ip
+ * - load the data address in a register
+ * - restore the stack pointer to what it was when the trampoline was invoked.
+ */
+	.align	ARM_TRAMP_MAP_SHIFT
+ARM_FUNC_START(trampoline_code_table)
+	.rept	ARM_TRAMP_MAP_SIZE / ARM_TRAMP_SIZE
+	sub	sp, sp, #8		/* Make space on the stack */
+	str	ip, [sp]		/* Save ip on stack */
+	ldr	ip, [pc, #4080]		/* Copy data into ip */
+	str	ip, [sp, #4]		/* Save data on stack */
+	ldr	pc, [pc, #4076]		/* Copy code into PC */
+	.endr
+ARM_FUNC_END(trampoline_code_table)
+	.align	ARM_TRAMP_MAP_SHIFT
+#endif /* FFI_EXEC_STATIC_TRAMP */
+
 #if FFI_EXEC_TRAMPOLINE_TABLE
 
 #ifdef __MACH__
-- 
2.25.1


      parent reply	other threads:[~2020-11-24 19:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9bd94fd78a3c8f638b8a0d2269258da99d58e70f>
2020-11-24 19:32 ` [RFC PATCH v1 0/4] Libffi " madvenka
2020-11-24 19:32   ` [RFC PATCH v1 1/4] " madvenka
2020-11-24 19:49     ` Anthony Green
2020-11-24 20:02       ` Madhavan T. Venkataraman
2020-12-02 16:49       ` Madhavan T. Venkataraman
2020-12-02 18:14         ` Anthony Green
2020-12-02 21:33           ` Madhavan T. Venkataraman
2020-12-03 18:45             ` Madhavan T. Venkataraman
2020-12-05  2:38               ` [RFC PATCH v1 1/4] Static Trampolines - Quick question Madhavan T. Venkataraman
2020-11-24 19:32   ` [RFC PATCH v1 2/4] x86: Support for Static Trampolines madvenka
2020-11-24 19:32   ` [RFC PATCH v1 3/4] aarch64: " madvenka
2020-11-24 19:32   ` madvenka [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124193206.10289-5-madvenka@linux.microsoft.com \
    --to=madvenka@linux.microsoft.com \
    --cc=dj@redhat.com \
    --cc=fw@deneb.enyo.de \
    --cc=libffi-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).