From patchwork Tue Sep 17 12:43:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russ Dill X-Patchwork-Id: 2902451 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A3B719F1C0 for ; Tue, 17 Sep 2013 12:47:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 10B842038E for ; Tue, 17 Sep 2013 12:47:49 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 83EAF20377 for ; Tue, 17 Sep 2013 12:47:44 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VLufS-00016L-LO; Tue, 17 Sep 2013 12:46:13 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VLuep-0004Fm-Ft; Tue, 17 Sep 2013 12:45:31 +0000 Received: from mail-pb0-x231.google.com ([2607:f8b0:400e:c01::231]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VLudh-00045q-NI for linux-arm-kernel@lists.infradead.org; Tue, 17 Sep 2013 12:44:36 +0000 Received: by mail-pb0-f49.google.com with SMTP id xb4so5477385pbc.22 for ; Tue, 17 Sep 2013 05:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=bxr1jEV4aHGYpgWHn+wwZQHhKkNHOr0xmGMG7ChUV/0=; b=aFwmw4dLXWBxWvcIkKGnaDDOYFtn7wFMA4cm/QHFBXf6HhK7QUyCnIXgfNewLhktnl gmcqxa/owUEw47TbM1fSskCjuQ8Z/ZxmMsfL1RVSFjVjZWvz3SfNa/SbSTrwAz5srOnf W2vDgcLxL49NhTcblJsGPWvFz6BGrRes3TE6pccJXK/eHf1C2DTSQCgrrPper6nWgdwt Q940FtD3yjQy1NBPQOqM5tupWFvoBWRhzdmbdDgRy5mnN5JUF2gJ3ZXHwSgsdiIXB/eZ yllBR9FSunsgIbQhpdJxFIu1HXH3DmZu/8Sr13YFD/AeWZKjHV8s0C3IOuzpfHEsQGoQ OB5Q== X-Received: by 10.66.154.1 with SMTP id vk1mr36757814pab.85.1379421840260; Tue, 17 Sep 2013 05:44:00 -0700 (PDT) Received: from localhost (pool-71-189-49-9.lsanca.fios.verizon.net. [71.189.49.9]) by mx.google.com with ESMTPSA id ct4sm38012622pbb.41.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 17 Sep 2013 05:43:59 -0700 (PDT) From: Russ Dill To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-omap@vger.kernel.org Subject: [RFC PATCH 04/11] asm-generic: fncpy: Add function copying macros Date: Tue, 17 Sep 2013 05:43:30 -0700 Message-Id: <1379421817-15759-5-git-send-email-Russ.Dill@ti.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1379421817-15759-1-git-send-email-Russ.Dill@ti.com> References: <1379421817-15759-1-git-send-email-Russ.Dill@ti.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130917_084422_059261_9FF5C49A X-CRM114-Status: GOOD ( 20.22 ) X-Spam-Score: -1.9 (-) Cc: mans@mansr.com, Russell King - ARM Linux , Ard Biesheuvel , linux-kbuild@vger.kernel.org, Shawn Guo , Dave Martin X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Under certain arches (ARM) function pointers cannot be used naively. Specifically, for thumb functions, their 0 bit is set, but they are contained on a word aligned address. Add a fncpy macro to perform function copies correctly along with two helpers, fnptr_to_address, and fnptr_translate. Signed-off-by: Russ Dill --- arch/alpha/include/asm/fncpy.h | 1 + arch/arc/include/asm/fncpy.h | 1 + arch/arm/include/asm/fncpy.h | 76 +++++++------------------- arch/arm/plat-omap/sram.c | 2 +- arch/arm64/include/asm/fncpy.h | 1 + arch/avr32/include/asm/fncpy.h | 1 + arch/blackfin/include/asm/fncpy.h | 1 + arch/c6x/include/asm/fncpy.h | 1 + arch/cris/include/asm/fncpy.h | 1 + arch/frv/include/asm/fncpy.h | 1 + arch/h8300/include/asm/fncpy.h | 1 + arch/hexagon/include/asm/fncpy.h | 1 + arch/ia64/include/asm/fncpy.h | 1 + arch/m32r/include/asm/fncpy.h | 1 + arch/m68k/include/asm/fncpy.h | 1 + arch/metag/include/asm/fncpy.h | 1 + arch/microblaze/include/asm/fncpy.h | 1 + arch/mips/include/asm/fncpy.h | 1 + arch/mn10300/include/asm/fncpy.h | 1 + arch/openrisc/include/asm/fncpy.h | 1 + arch/parisc/include/asm/fncpy.h | 1 + arch/powerpc/include/asm/fncpy.h | 1 + arch/s390/include/asm/fncpy.h | 1 + arch/score/include/asm/fncpy.h | 1 + arch/sh/include/asm/fncpy.h | 1 + arch/sparc/include/asm/fncpy.h | 1 + arch/tile/include/asm/fncpy.h | 1 + arch/um/include/asm/fncpy.h | 1 + arch/unicore32/include/asm/fncpy.h | 1 + arch/x86/include/asm/fncpy.h | 1 + arch/xtensa/include/asm/fncpy.h | 1 + include/asm-generic/fncpy.h | 104 ++++++++++++++++++++++++++++++++++++ 32 files changed, 154 insertions(+), 57 deletions(-) create mode 100644 arch/alpha/include/asm/fncpy.h create mode 100644 arch/arc/include/asm/fncpy.h create mode 100644 arch/arm64/include/asm/fncpy.h create mode 100644 arch/avr32/include/asm/fncpy.h create mode 100644 arch/blackfin/include/asm/fncpy.h create mode 100644 arch/c6x/include/asm/fncpy.h create mode 100644 arch/cris/include/asm/fncpy.h create mode 100644 arch/frv/include/asm/fncpy.h create mode 100644 arch/h8300/include/asm/fncpy.h create mode 100644 arch/hexagon/include/asm/fncpy.h create mode 100644 arch/ia64/include/asm/fncpy.h create mode 100644 arch/m32r/include/asm/fncpy.h create mode 100644 arch/m68k/include/asm/fncpy.h create mode 100644 arch/metag/include/asm/fncpy.h create mode 100644 arch/microblaze/include/asm/fncpy.h create mode 100644 arch/mips/include/asm/fncpy.h create mode 100644 arch/mn10300/include/asm/fncpy.h create mode 100644 arch/openrisc/include/asm/fncpy.h create mode 100644 arch/parisc/include/asm/fncpy.h create mode 100644 arch/powerpc/include/asm/fncpy.h create mode 100644 arch/s390/include/asm/fncpy.h create mode 100644 arch/score/include/asm/fncpy.h create mode 100644 arch/sh/include/asm/fncpy.h create mode 100644 arch/sparc/include/asm/fncpy.h create mode 100644 arch/tile/include/asm/fncpy.h create mode 100644 arch/um/include/asm/fncpy.h create mode 100644 arch/unicore32/include/asm/fncpy.h create mode 100644 arch/x86/include/asm/fncpy.h create mode 100644 arch/xtensa/include/asm/fncpy.h create mode 100644 include/asm-generic/fncpy.h diff --git a/arch/alpha/include/asm/fncpy.h b/arch/alpha/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/alpha/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/arc/include/asm/fncpy.h b/arch/arc/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/arc/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/arm/include/asm/fncpy.h b/arch/arm/include/asm/fncpy.h index de53547..f165f20 100644 --- a/arch/arm/include/asm/fncpy.h +++ b/arch/arm/include/asm/fncpy.h @@ -17,16 +17,12 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#ifndef __ASM_FNCPY_H +#define __ASM_FNCPY_H + +#include + /* - * These macros are intended for use when there is a need to copy a low-level - * function body into special memory. - * - * For example, when reconfiguring the SDRAM controller, the code doing the - * reconfiguration may need to run from SRAM. - * - * NOTE: that the copied function body must be entirely self-contained and - * position-independent in order for this to work properly. - * * NOTE: in order for embedded literals and data to get referenced correctly, * the alignment of functions must be preserved when copying. To ensure this, * the source and destination addresses for fncpy() must be aligned to a @@ -34,61 +30,29 @@ * You will typically need a ".align 3" directive in the assembler where the * function to be copied is defined, and ensure that your allocator for the * destination buffer returns 8-byte-aligned pointers. - * - * Typical usage example: - * - * extern int f(args); - * extern uint32_t size_of_f; - * int (*copied_f)(args); - * void *sram_buffer; - * - * copied_f = fncpy(sram_buffer, &f, size_of_f); - * - * ... later, call the function: ... - * - * copied_f(args); - * - * The size of the function to be copied can't be determined from C: - * this must be determined by other means, such as adding assmbler directives - * in the file where f is defined. - */ +*/ +#define ARCH_FNCPY_ALIGN 3 -#ifndef __ASM_FNCPY_H -#define __ASM_FNCPY_H - -#include -#include - -#include -#include - -/* - * Minimum alignment requirement for the source and destination addresses - * for function copying. - */ -#define FNCPY_ALIGN 8 - -#define fncpy(dest_buf, funcp, size) ({ \ +/* Clear the Thumb bit */ +#define fnptr_to_addr(funcp) ({ \ uintptr_t __funcp_address; \ - typeof(funcp) __result; \ \ asm("" : "=r" (__funcp_address) : "0" (funcp)); \ + __funcp_address & ~1; \ +}) + +/* Put the Thumb bit back */ +#define fnptr_translate(orig_funcp, new_addr) ({ \ + uintptr_t __funcp_address; \ + typeof(orig_funcp) __result; \ \ - /* \ - * Ensure alignment of source and destination addresses, \ - * disregarding the function's Thumb bit: \ - */ \ - BUG_ON((uintptr_t)(dest_buf) & (FNCPY_ALIGN - 1) || \ - (__funcp_address & ~(uintptr_t)1 & (FNCPY_ALIGN - 1))); \ - \ - memcpy(dest_buf, (void const *)(__funcp_address & ~1), size); \ - flush_icache_range((unsigned long)(dest_buf), \ - (unsigned long)(dest_buf) + (size)); \ - \ + asm("" : "=r" (__funcp_address) : "0" (orig_funcp)); \ asm("" : "=r" (__result) \ - : "0" ((uintptr_t)(dest_buf) | (__funcp_address & 1))); \ + : "0" ((uintptr_t)(new_addr) | (__funcp_address & 1))); \ \ __result; \ }) +#include + #endif /* !__ASM_FNCPY_H */ diff --git a/arch/arm/plat-omap/sram.c b/arch/arm/plat-omap/sram.c index a5bc92d..90ccd74 100644 --- a/arch/arm/plat-omap/sram.c +++ b/arch/arm/plat-omap/sram.c @@ -54,7 +54,7 @@ void *omap_sram_push_address(unsigned long size) } new_ceil -= size; - new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN); + new_ceil = ROUND_DOWN(new_ceil, 1 << ARCH_FNCPY_ALIGN); omap_sram_ceil = IOMEM(new_ceil); return (void *)omap_sram_ceil; diff --git a/arch/arm64/include/asm/fncpy.h b/arch/arm64/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/arm64/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/avr32/include/asm/fncpy.h b/arch/avr32/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/avr32/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/blackfin/include/asm/fncpy.h b/arch/blackfin/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/blackfin/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/c6x/include/asm/fncpy.h b/arch/c6x/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/c6x/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/cris/include/asm/fncpy.h b/arch/cris/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/cris/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/frv/include/asm/fncpy.h b/arch/frv/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/frv/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/h8300/include/asm/fncpy.h b/arch/h8300/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/h8300/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/hexagon/include/asm/fncpy.h b/arch/hexagon/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/hexagon/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/ia64/include/asm/fncpy.h b/arch/ia64/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/ia64/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/m32r/include/asm/fncpy.h b/arch/m32r/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/m32r/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/m68k/include/asm/fncpy.h b/arch/m68k/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/m68k/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/metag/include/asm/fncpy.h b/arch/metag/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/metag/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/microblaze/include/asm/fncpy.h b/arch/microblaze/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/microblaze/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/mips/include/asm/fncpy.h b/arch/mips/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/mips/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/mn10300/include/asm/fncpy.h b/arch/mn10300/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/mn10300/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/openrisc/include/asm/fncpy.h b/arch/openrisc/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/openrisc/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/parisc/include/asm/fncpy.h b/arch/parisc/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/parisc/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/powerpc/include/asm/fncpy.h b/arch/powerpc/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/powerpc/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/s390/include/asm/fncpy.h b/arch/s390/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/s390/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/score/include/asm/fncpy.h b/arch/score/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/score/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/sh/include/asm/fncpy.h b/arch/sh/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/sh/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/sparc/include/asm/fncpy.h b/arch/sparc/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/sparc/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/tile/include/asm/fncpy.h b/arch/tile/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/tile/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/um/include/asm/fncpy.h b/arch/um/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/um/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/unicore32/include/asm/fncpy.h b/arch/unicore32/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/unicore32/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/x86/include/asm/fncpy.h b/arch/x86/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/x86/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/arch/xtensa/include/asm/fncpy.h b/arch/xtensa/include/asm/fncpy.h new file mode 100644 index 0000000..ee4741c --- /dev/null +++ b/arch/xtensa/include/asm/fncpy.h @@ -0,0 +1 @@ +#include diff --git a/include/asm-generic/fncpy.h b/include/asm-generic/fncpy.h new file mode 100644 index 0000000..1a25282 --- /dev/null +++ b/include/asm-generic/fncpy.h @@ -0,0 +1,104 @@ +/* + * include/asm-generic/fncpy.h - helper macros for function body copying + * + * Copyright (C) 2011 Linaro Limited + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * These macros are intended for use when there is a need to copy a low-level + * function body into special memory. + * + * For example, when reconfiguring the SDRAM controller, the code doing the + * reconfiguration may need to run from SRAM. + * + * NOTE: that the copied function body must be entirely self-contained and + * position-independent in order for this to work properly. + * + * Typical usage example: + * + * extern int f(args); + * extern uint32_t size_of_f; + * int (*copied_f)(args); + * void *sram_buffer; + * + * copied_f = fncpy(sram_buffer, &f, size_of_f); + * + * ... later, call the function: ... + * + * copied_f(args); + * + * The size of the function to be copied can't be determined from C: + * this must be determined by other means, such as adding assmbler directives + * in the file where f is defined. + */ + +#ifndef __ASM_GENERIC_FNCPY_H +#define __ASM_GENERIC_FNCPY_H + +#include +#include + +#include +#include + +/* + * Minimum alignment requirement for the source and destination addresses + * for function copying. + */ +#ifndef ARCH_FNCPY_ALIGN +#define ARCH_FNCPY_ALIGN 0 +#endif + +#define ARCH_FNCPY_MASK ((1 << (ARCH_FNCPY_ALIGN)) - 1) + +#ifndef fnptr_to_addr +#define fnptr_to_addr(funcp) ({ \ + (uintptr_t) (funcp); \ +}) +#endif + +#ifndef fnptr_translate +#define fnptr_translate(orig_funcp, new_addr) ({ \ + (typeof(orig_funcp)) (new_addr); \ +}) +#endif + +/* Ensure alignment of source and destination addresses */ +#ifndef fn_dest_invalid +#define fn_dest_invalid(funcp, dest_buf) ({ \ + uintptr_t __funcp_address; \ + \ + __funcp_address = fnptr_to_addr(funcp); \ + \ + ((uintptr_t)(dest_buf) & ARCH_FNCPY_MASK) || \ + (__funcp_address & ARCH_FNCPY_MASK); \ +}) +#endif + +#ifndef fncpy +#define fncpy(dest_buf, funcp, size) ({ \ + BUG_ON(fn_dest_invalid(funcp, dest_buf)); \ + \ + memcpy(dest_buf, (void const *)(funcp), size); \ + flush_icache_range((unsigned long)(dest_buf), \ + (unsigned long)(dest_buf) + (size)); \ + \ + fnptr_translate(funcp, dest_buf); \ +}) +#endif + +#endif /* !__ASM_GENERIC_FNCPY_H */ +