Re: [PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-12 Thread Cyril Chemparathy

On 08/11/12 22:22, Nicolas Pitre wrote:

On Fri, 10 Aug 2012, Cyril Chemparathy wrote:


The original phys_to_virt/virt_to_phys patching implementation relied on early
patching prior to MMU initialization.  On PAE systems running out of >4G
address space, this would have entailed an additional round of patching after
switching over to the high address space.

The approach implemented here conceptually extends the original PHYS_OFFSET
patching implementation with the introduction of "early" patch stubs.  Early
patch code is required to be functional out of the box, even before the patch
is applied.  This is implemented by inserting functional (but inefficient)
load code into the .runtime.patch.code init section.  Having functional code
out of the box then allows us to defer the init time patch application until
later in the init sequence.

In addition to fitting better with our need for physical address-space
switch-over, this implementation should be somewhat more extensible by virtue
of its more readable (and hackable) C implementation.  This should prove
useful for other similar init time specialization needs, especially in light
of our multi-platform kernel initiative.

This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
(Cortex-A8) device.

Note: the obtuse use of stringified symbols in patch_stub() and
early_patch_stub() is intentional.  Theoretically this should have been
accomplished with formal operands passed into the asm block, but this requires
the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
However, the 'c' modifier has been found to ICE certain versions of GCC, and
therefore we resort to stringified symbols here.

Signed-off-by: Cyril Chemparathy 


Reviewed-by: Nicolas Pitre 



Thanks.

I've been looking at the compiler emitted code, and had to make a couple 
of changes to keep things streamlined...


[...]

+#define early_patch_imm8(insn, to, from, sym, offset)  \
+   early_patch_stub(PATCH_IMM8,\
+/* code */ \
+"ldr  %0, =" __stringify(sym + offset) "\n"\
+"ldr  %0, [%0]\n"\
+insn " %0, %1, %0\n",\
+/* patch_data */   \
+".long " __stringify(sym + offset) "\n"\
+insn " %0, %1, %2\n",\
+: "=" (to) \
+: "r" (from), "I" (__IMM8), "m" (sym)\
+: "cc")


First, the "m" operand modifier for "sym" forces GCC to emit code to 
load the address of the symbol into a register.  I've replaced this with 
"i" (&(sym) to make that go away.  With this, the emitted code doesn't 
contain any such unexpected nonsense.


Second, marking the "to" operand as early clobber makes the compiler 
generate horrid register moves around the assembly block, even when it 
has registers to spare.  Simply adding a temporary variable does a much 
much better job, especially since this temporary register is used only 
in the patched-out "early" code.


Thanks
-- Cyril.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-12 Thread Cyril Chemparathy

On 08/11/12 22:22, Nicolas Pitre wrote:

On Fri, 10 Aug 2012, Cyril Chemparathy wrote:


The original phys_to_virt/virt_to_phys patching implementation relied on early
patching prior to MMU initialization.  On PAE systems running out of 4G
address space, this would have entailed an additional round of patching after
switching over to the high address space.

The approach implemented here conceptually extends the original PHYS_OFFSET
patching implementation with the introduction of early patch stubs.  Early
patch code is required to be functional out of the box, even before the patch
is applied.  This is implemented by inserting functional (but inefficient)
load code into the .runtime.patch.code init section.  Having functional code
out of the box then allows us to defer the init time patch application until
later in the init sequence.

In addition to fitting better with our need for physical address-space
switch-over, this implementation should be somewhat more extensible by virtue
of its more readable (and hackable) C implementation.  This should prove
useful for other similar init time specialization needs, especially in light
of our multi-platform kernel initiative.

This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
(Cortex-A8) device.

Note: the obtuse use of stringified symbols in patch_stub() and
early_patch_stub() is intentional.  Theoretically this should have been
accomplished with formal operands passed into the asm block, but this requires
the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
However, the 'c' modifier has been found to ICE certain versions of GCC, and
therefore we resort to stringified symbols here.

Signed-off-by: Cyril Chemparathy cy...@ti.com


Reviewed-by: Nicolas Pitre n...@linaro.org



Thanks.

I've been looking at the compiler emitted code, and had to make a couple 
of changes to keep things streamlined...


[...]

+#define early_patch_imm8(insn, to, from, sym, offset)  \
+   early_patch_stub(PATCH_IMM8,\
+/* code */ \
+ldr  %0, = __stringify(sym + offset) \n\
+ldr  %0, [%0]\n\
+insn  %0, %1, %0\n,\
+/* patch_data */   \
+.long  __stringify(sym + offset) \n\
+insn  %0, %1, %2\n,\
+: =r (to) \
+: r (from), I (__IMM8), m (sym)\
+: cc)


First, the m operand modifier for sym forces GCC to emit code to 
load the address of the symbol into a register.  I've replaced this with 
i ((sym) to make that go away.  With this, the emitted code doesn't 
contain any such unexpected nonsense.


Second, marking the to operand as early clobber makes the compiler 
generate horrid register moves around the assembly block, even when it 
has registers to spare.  Simply adding a temporary variable does a much 
much better job, especially since this temporary register is used only 
in the patched-out early code.


Thanks
-- Cyril.
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-11 Thread Nicolas Pitre
On Fri, 10 Aug 2012, Cyril Chemparathy wrote:

> The original phys_to_virt/virt_to_phys patching implementation relied on early
> patching prior to MMU initialization.  On PAE systems running out of >4G
> address space, this would have entailed an additional round of patching after
> switching over to the high address space.
> 
> The approach implemented here conceptually extends the original PHYS_OFFSET
> patching implementation with the introduction of "early" patch stubs.  Early
> patch code is required to be functional out of the box, even before the patch
> is applied.  This is implemented by inserting functional (but inefficient)
> load code into the .runtime.patch.code init section.  Having functional code
> out of the box then allows us to defer the init time patch application until
> later in the init sequence.
> 
> In addition to fitting better with our need for physical address-space
> switch-over, this implementation should be somewhat more extensible by virtue
> of its more readable (and hackable) C implementation.  This should prove
> useful for other similar init time specialization needs, especially in light
> of our multi-platform kernel initiative.
> 
> This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
> (Cortex-A8) device.
> 
> Note: the obtuse use of stringified symbols in patch_stub() and
> early_patch_stub() is intentional.  Theoretically this should have been
> accomplished with formal operands passed into the asm block, but this requires
> the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
> However, the 'c' modifier has been found to ICE certain versions of GCC, and
> therefore we resort to stringified symbols here.
> 
> Signed-off-by: Cyril Chemparathy 

Reviewed-by: Nicolas Pitre 


> ---
>  arch/arm/Kconfig |3 +
>  arch/arm/include/asm/module.h|7 ++
>  arch/arm/include/asm/runtime-patch.h |  175 +++
>  arch/arm/kernel/Makefile |1 +
>  arch/arm/kernel/module.c |4 +
>  arch/arm/kernel/runtime-patch.c  |  189 
> ++
>  arch/arm/kernel/setup.c  |3 +
>  arch/arm/kernel/vmlinux.lds.S|   10 ++
>  8 files changed, 392 insertions(+)
>  create mode 100644 arch/arm/include/asm/runtime-patch.h
>  create mode 100644 arch/arm/kernel/runtime-patch.c
> 
> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
> index e91c7cd..d0a04ad 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -61,6 +61,9 @@ config ARM
>  config ARM_HAS_SG_CHAIN
>   bool
>  
> +config ARM_RUNTIME_PATCH
> + bool
> +
>  config NEED_SG_DMA_LENGTH
>   bool
>  
> diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h
> index 6c6809f..2090486 100644
> --- a/arch/arm/include/asm/module.h
> +++ b/arch/arm/include/asm/module.h
> @@ -43,9 +43,16 @@ struct mod_arch_specific {
>  #define MODULE_ARCH_VERMAGIC_ARMTHUMB ""
>  #endif
>  
> +#ifdef CONFIG_ARM_RUNTIME_PATCH
> +#define MODULE_ARCH_VERMAGIC_RT_PATCH "rt-patch "
> +#else
> +#define MODULE_ARCH_VERMAGIC_RT_PATCH ""
> +#endif
> +
>  #define MODULE_ARCH_VERMAGIC \
>   MODULE_ARCH_VERMAGIC_ARMVSN \
>   MODULE_ARCH_VERMAGIC_ARMTHUMB \
> + MODULE_ARCH_VERMAGIC_RT_PATCH \
>   MODULE_ARCH_VERMAGIC_P2V
>  
>  #endif /* _ASM_ARM_MODULE_H */
> diff --git a/arch/arm/include/asm/runtime-patch.h 
> b/arch/arm/include/asm/runtime-patch.h
> new file mode 100644
> index 000..6c6e8a2
> --- /dev/null
> +++ b/arch/arm/include/asm/runtime-patch.h
> @@ -0,0 +1,175 @@
> +/*
> + * arch/arm/include/asm/runtime-patch.h
> + * Note: this file should not be included by non-asm/.h files
> + *
> + * Copyright 2012 Texas Instruments, Inc.
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope 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, see .
> + */
> +#ifndef __ASM_ARM_RUNTIME_PATCH_H
> +#define __ASM_ARM_RUNTIME_PATCH_H
> +
> +#include 
> +
> +#ifndef __ASSEMBLY__
> +
> +#ifdef CONFIG_ARM_RUNTIME_PATCH
> +
> +struct patch_info {
> + void*insn;
> + u16  type;
> + u8   insn_size;
> + u8   data_size;
> + u32  data[0];
> +};
> +
> +#define patch_next(p)((void *)(p) + sizeof(*(p)) + 
> (p)->data_size)
> +
> +#define PATCH_TYPE_MASK  0x00ff
> +#define PATCH_IMM8   0x0001
> +
> +#define PATCH_EARLY  0x8000
> +
> +#define patch_stub(type, code, patch_data, 

Re: [PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-11 Thread Nicolas Pitre
On Fri, 10 Aug 2012, Cyril Chemparathy wrote:

 The original phys_to_virt/virt_to_phys patching implementation relied on early
 patching prior to MMU initialization.  On PAE systems running out of 4G
 address space, this would have entailed an additional round of patching after
 switching over to the high address space.
 
 The approach implemented here conceptually extends the original PHYS_OFFSET
 patching implementation with the introduction of early patch stubs.  Early
 patch code is required to be functional out of the box, even before the patch
 is applied.  This is implemented by inserting functional (but inefficient)
 load code into the .runtime.patch.code init section.  Having functional code
 out of the box then allows us to defer the init time patch application until
 later in the init sequence.
 
 In addition to fitting better with our need for physical address-space
 switch-over, this implementation should be somewhat more extensible by virtue
 of its more readable (and hackable) C implementation.  This should prove
 useful for other similar init time specialization needs, especially in light
 of our multi-platform kernel initiative.
 
 This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
 (Cortex-A8) device.
 
 Note: the obtuse use of stringified symbols in patch_stub() and
 early_patch_stub() is intentional.  Theoretically this should have been
 accomplished with formal operands passed into the asm block, but this requires
 the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
 However, the 'c' modifier has been found to ICE certain versions of GCC, and
 therefore we resort to stringified symbols here.
 
 Signed-off-by: Cyril Chemparathy cy...@ti.com

Reviewed-by: Nicolas Pitre n...@linaro.org


 ---
  arch/arm/Kconfig |3 +
  arch/arm/include/asm/module.h|7 ++
  arch/arm/include/asm/runtime-patch.h |  175 +++
  arch/arm/kernel/Makefile |1 +
  arch/arm/kernel/module.c |4 +
  arch/arm/kernel/runtime-patch.c  |  189 
 ++
  arch/arm/kernel/setup.c  |3 +
  arch/arm/kernel/vmlinux.lds.S|   10 ++
  8 files changed, 392 insertions(+)
  create mode 100644 arch/arm/include/asm/runtime-patch.h
  create mode 100644 arch/arm/kernel/runtime-patch.c
 
 diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
 index e91c7cd..d0a04ad 100644
 --- a/arch/arm/Kconfig
 +++ b/arch/arm/Kconfig
 @@ -61,6 +61,9 @@ config ARM
  config ARM_HAS_SG_CHAIN
   bool
  
 +config ARM_RUNTIME_PATCH
 + bool
 +
  config NEED_SG_DMA_LENGTH
   bool
  
 diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h
 index 6c6809f..2090486 100644
 --- a/arch/arm/include/asm/module.h
 +++ b/arch/arm/include/asm/module.h
 @@ -43,9 +43,16 @@ struct mod_arch_specific {
  #define MODULE_ARCH_VERMAGIC_ARMTHUMB 
  #endif
  
 +#ifdef CONFIG_ARM_RUNTIME_PATCH
 +#define MODULE_ARCH_VERMAGIC_RT_PATCH rt-patch 
 +#else
 +#define MODULE_ARCH_VERMAGIC_RT_PATCH 
 +#endif
 +
  #define MODULE_ARCH_VERMAGIC \
   MODULE_ARCH_VERMAGIC_ARMVSN \
   MODULE_ARCH_VERMAGIC_ARMTHUMB \
 + MODULE_ARCH_VERMAGIC_RT_PATCH \
   MODULE_ARCH_VERMAGIC_P2V
  
  #endif /* _ASM_ARM_MODULE_H */
 diff --git a/arch/arm/include/asm/runtime-patch.h 
 b/arch/arm/include/asm/runtime-patch.h
 new file mode 100644
 index 000..6c6e8a2
 --- /dev/null
 +++ b/arch/arm/include/asm/runtime-patch.h
 @@ -0,0 +1,175 @@
 +/*
 + * arch/arm/include/asm/runtime-patch.h
 + * Note: this file should not be included by non-asm/.h files
 + *
 + * Copyright 2012 Texas Instruments, Inc.
 + *
 + * This program is free software; you can redistribute it and/or modify it
 + * under the terms and conditions of the GNU General Public License,
 + * version 2, as published by the Free Software Foundation.
 + *
 + * This program is distributed in the hope 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, see http://www.gnu.org/licenses/.
 + */
 +#ifndef __ASM_ARM_RUNTIME_PATCH_H
 +#define __ASM_ARM_RUNTIME_PATCH_H
 +
 +#include linux/stringify.h
 +
 +#ifndef __ASSEMBLY__
 +
 +#ifdef CONFIG_ARM_RUNTIME_PATCH
 +
 +struct patch_info {
 + void*insn;
 + u16  type;
 + u8   insn_size;
 + u8   data_size;
 + u32  data[0];
 +};
 +
 +#define patch_next(p)((void *)(p) + sizeof(*(p)) + 
 (p)-data_size)
 +
 +#define PATCH_TYPE_MASK  0x00ff
 +#define PATCH_IMM8   0x0001
 +
 +#define PATCH_EARLY  0x8000
 +
 +#define patch_stub(type, code, patch_data, ...)  
 \
 + __asm__(@ patch stub\n  

[PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-10 Thread Cyril Chemparathy
The original phys_to_virt/virt_to_phys patching implementation relied on early
patching prior to MMU initialization.  On PAE systems running out of >4G
address space, this would have entailed an additional round of patching after
switching over to the high address space.

The approach implemented here conceptually extends the original PHYS_OFFSET
patching implementation with the introduction of "early" patch stubs.  Early
patch code is required to be functional out of the box, even before the patch
is applied.  This is implemented by inserting functional (but inefficient)
load code into the .runtime.patch.code init section.  Having functional code
out of the box then allows us to defer the init time patch application until
later in the init sequence.

In addition to fitting better with our need for physical address-space
switch-over, this implementation should be somewhat more extensible by virtue
of its more readable (and hackable) C implementation.  This should prove
useful for other similar init time specialization needs, especially in light
of our multi-platform kernel initiative.

This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
(Cortex-A8) device.

Note: the obtuse use of stringified symbols in patch_stub() and
early_patch_stub() is intentional.  Theoretically this should have been
accomplished with formal operands passed into the asm block, but this requires
the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
However, the 'c' modifier has been found to ICE certain versions of GCC, and
therefore we resort to stringified symbols here.

Signed-off-by: Cyril Chemparathy 
---
 arch/arm/Kconfig |3 +
 arch/arm/include/asm/module.h|7 ++
 arch/arm/include/asm/runtime-patch.h |  175 +++
 arch/arm/kernel/Makefile |1 +
 arch/arm/kernel/module.c |4 +
 arch/arm/kernel/runtime-patch.c  |  189 ++
 arch/arm/kernel/setup.c  |3 +
 arch/arm/kernel/vmlinux.lds.S|   10 ++
 8 files changed, 392 insertions(+)
 create mode 100644 arch/arm/include/asm/runtime-patch.h
 create mode 100644 arch/arm/kernel/runtime-patch.c

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index e91c7cd..d0a04ad 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -61,6 +61,9 @@ config ARM
 config ARM_HAS_SG_CHAIN
bool
 
+config ARM_RUNTIME_PATCH
+   bool
+
 config NEED_SG_DMA_LENGTH
bool
 
diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h
index 6c6809f..2090486 100644
--- a/arch/arm/include/asm/module.h
+++ b/arch/arm/include/asm/module.h
@@ -43,9 +43,16 @@ struct mod_arch_specific {
 #define MODULE_ARCH_VERMAGIC_ARMTHUMB ""
 #endif
 
+#ifdef CONFIG_ARM_RUNTIME_PATCH
+#define MODULE_ARCH_VERMAGIC_RT_PATCH "rt-patch "
+#else
+#define MODULE_ARCH_VERMAGIC_RT_PATCH ""
+#endif
+
 #define MODULE_ARCH_VERMAGIC \
MODULE_ARCH_VERMAGIC_ARMVSN \
MODULE_ARCH_VERMAGIC_ARMTHUMB \
+   MODULE_ARCH_VERMAGIC_RT_PATCH \
MODULE_ARCH_VERMAGIC_P2V
 
 #endif /* _ASM_ARM_MODULE_H */
diff --git a/arch/arm/include/asm/runtime-patch.h 
b/arch/arm/include/asm/runtime-patch.h
new file mode 100644
index 000..6c6e8a2
--- /dev/null
+++ b/arch/arm/include/asm/runtime-patch.h
@@ -0,0 +1,175 @@
+/*
+ * arch/arm/include/asm/runtime-patch.h
+ * Note: this file should not be included by non-asm/.h files
+ *
+ * Copyright 2012 Texas Instruments, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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, see .
+ */
+#ifndef __ASM_ARM_RUNTIME_PATCH_H
+#define __ASM_ARM_RUNTIME_PATCH_H
+
+#include 
+
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_ARM_RUNTIME_PATCH
+
+struct patch_info {
+   void*insn;
+   u16  type;
+   u8   insn_size;
+   u8   data_size;
+   u32  data[0];
+};
+
+#define patch_next(p)  ((void *)(p) + sizeof(*(p)) + (p)->data_size)
+
+#define PATCH_TYPE_MASK0x00ff
+#define PATCH_IMM8 0x0001
+
+#define PATCH_EARLY0x8000
+
+#define patch_stub(type, code, patch_data, ...)
\
+   __asm__("@ patch stub\n"\
+   "1:\n"  \
+   code\
+   "2:\n"  

[PATCH v2 01/22] ARM: add mechanism for late code patching

2012-08-10 Thread Cyril Chemparathy
The original phys_to_virt/virt_to_phys patching implementation relied on early
patching prior to MMU initialization.  On PAE systems running out of 4G
address space, this would have entailed an additional round of patching after
switching over to the high address space.

The approach implemented here conceptually extends the original PHYS_OFFSET
patching implementation with the introduction of early patch stubs.  Early
patch code is required to be functional out of the box, even before the patch
is applied.  This is implemented by inserting functional (but inefficient)
load code into the .runtime.patch.code init section.  Having functional code
out of the box then allows us to defer the init time patch application until
later in the init sequence.

In addition to fitting better with our need for physical address-space
switch-over, this implementation should be somewhat more extensible by virtue
of its more readable (and hackable) C implementation.  This should prove
useful for other similar init time specialization needs, especially in light
of our multi-platform kernel initiative.

This code has been boot tested in both ARM and Thumb-2 modes on an ARMv7
(Cortex-A8) device.

Note: the obtuse use of stringified symbols in patch_stub() and
early_patch_stub() is intentional.  Theoretically this should have been
accomplished with formal operands passed into the asm block, but this requires
the use of the 'c' modifier for instantiating the long (e.g. .long %c0).
However, the 'c' modifier has been found to ICE certain versions of GCC, and
therefore we resort to stringified symbols here.

Signed-off-by: Cyril Chemparathy cy...@ti.com
---
 arch/arm/Kconfig |3 +
 arch/arm/include/asm/module.h|7 ++
 arch/arm/include/asm/runtime-patch.h |  175 +++
 arch/arm/kernel/Makefile |1 +
 arch/arm/kernel/module.c |4 +
 arch/arm/kernel/runtime-patch.c  |  189 ++
 arch/arm/kernel/setup.c  |3 +
 arch/arm/kernel/vmlinux.lds.S|   10 ++
 8 files changed, 392 insertions(+)
 create mode 100644 arch/arm/include/asm/runtime-patch.h
 create mode 100644 arch/arm/kernel/runtime-patch.c

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index e91c7cd..d0a04ad 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -61,6 +61,9 @@ config ARM
 config ARM_HAS_SG_CHAIN
bool
 
+config ARM_RUNTIME_PATCH
+   bool
+
 config NEED_SG_DMA_LENGTH
bool
 
diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h
index 6c6809f..2090486 100644
--- a/arch/arm/include/asm/module.h
+++ b/arch/arm/include/asm/module.h
@@ -43,9 +43,16 @@ struct mod_arch_specific {
 #define MODULE_ARCH_VERMAGIC_ARMTHUMB 
 #endif
 
+#ifdef CONFIG_ARM_RUNTIME_PATCH
+#define MODULE_ARCH_VERMAGIC_RT_PATCH rt-patch 
+#else
+#define MODULE_ARCH_VERMAGIC_RT_PATCH 
+#endif
+
 #define MODULE_ARCH_VERMAGIC \
MODULE_ARCH_VERMAGIC_ARMVSN \
MODULE_ARCH_VERMAGIC_ARMTHUMB \
+   MODULE_ARCH_VERMAGIC_RT_PATCH \
MODULE_ARCH_VERMAGIC_P2V
 
 #endif /* _ASM_ARM_MODULE_H */
diff --git a/arch/arm/include/asm/runtime-patch.h 
b/arch/arm/include/asm/runtime-patch.h
new file mode 100644
index 000..6c6e8a2
--- /dev/null
+++ b/arch/arm/include/asm/runtime-patch.h
@@ -0,0 +1,175 @@
+/*
+ * arch/arm/include/asm/runtime-patch.h
+ * Note: this file should not be included by non-asm/.h files
+ *
+ * Copyright 2012 Texas Instruments, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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, see http://www.gnu.org/licenses/.
+ */
+#ifndef __ASM_ARM_RUNTIME_PATCH_H
+#define __ASM_ARM_RUNTIME_PATCH_H
+
+#include linux/stringify.h
+
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_ARM_RUNTIME_PATCH
+
+struct patch_info {
+   void*insn;
+   u16  type;
+   u8   insn_size;
+   u8   data_size;
+   u32  data[0];
+};
+
+#define patch_next(p)  ((void *)(p) + sizeof(*(p)) + (p)-data_size)
+
+#define PATCH_TYPE_MASK0x00ff
+#define PATCH_IMM8 0x0001
+
+#define PATCH_EARLY0x8000
+
+#define patch_stub(type, code, patch_data, ...)
\
+   __asm__(@ patch stub\n\
+   1:\n  \
+   code\
+   2:\n