MIPS: Add support for CONFIG_DEBUG_VIRTUAL
PATCH 1/1 MIPS: Add support for CONFIG_DEBUG_VIRTUAL
Provide hooks to intercept bad usages of virt_to_phys() and __pa_symbol() throughout the kernel. To make this possible, we need to rename the current implement of virt_to_phys() into __virt_to_phys_nodebug() and wrap it around depending on CONFIG_DEBUG_VIRTUAL.
A similar thing is needed for __pa_symbol() which is now aliased to __phys_addr_symbol() whose implementation is either the direct return of RELOC_HIDE or goes through the debug version.
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/io.h | 14 +++++++++++++- arch/mips/include/asm/page.h | 9 ++++++++- arch/mips/mm/Makefile | 2 ++ arch/mips/mm/physaddr.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 arch/mips/mm/physaddr.c
arch/mips/Kconfig +1 -0
@@ -70,6 +70,7 @@ config MIPS select HAVE_EXIT_THREAD select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_ARCH_HARDENED_USERCOPY + select ARCH_HAS_DEBUG_VIRTUAL menu "Machine selection"
arch/mips/include/asm/io.h +13 -1
@@ -116,11 +116,23 @@ static inline void set_io_port_base(unsigned long base)* almost all conceivable cases a device driver should not be using * this function */ -static inline unsigned long virt_to_phys (volatile const void *address) +static inline unsigned long __virt_to_phys_nodebug (volatile const void *address) {return __pa (address); } +#ifdef CONFIG_DEBUG_VIRTUAL +extern phys_addr_t __virt_to_phys (volatile const void *x); +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif + +#define virt_to_phys virt_to_phys +static inline phys_addr_t virt_to_phys (const volatile void *x) +{ +return __virt_to_phys (x); +} +/* * phys_to_virt - map physical address to virtual * @address: address to remap
arch/mips/include/asm/page.h +8 -1
@@ -205,9 +205,16 @@ static inline unsigned long ___pa(unsigned long x)* until GCC 3.x has been retired before we can apply * https://patchwork.linux-mips.org/patch/1541/ */ +#define __pa_symbol_nodebug (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) + +#ifdef CONFIG_DEBUG_VIRTUAL +extern phys_addr_t __phys_addr_symbol (unsigned long x); +#else +#define __phys_addr_symbol (x) __pa_symbol_nodebug(x) +#endif #ifndef __pa_symbol -#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) +#define __pa_symbol (x) __phys_addr_symbol((unsigned long)(x))#endif #define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)
arch/mips/mm/Makefile +2 -0
@@ -29,3 +29,5 @@ obj-$(CONFIG_R5000_CPU_SCACHE) += sc-r5k.o obj-$(CONFIG_RM7000_CPU_SCACHE) += sc-rm7k.o obj-$(CONFIG_MIPS_CPU_SCACHE) += sc-mips.o obj-$(CONFIG_SCACHE_DEBUGFS) += sc-debugfs.o + +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
arch/mips/mm/physaddr.c new file +40 -0
@@ -0,0 +1,40 @@ +#include <linux/bug.h> +#include <linux/export.h> +#include <linux/types.h> +#include <linux/mmdebug.h> +#include <linux/mm.h> + +#include <asm/sections.h> +#include <asm/io.h> +#include <asm/page.h> +#include <asm/dma.h> + +static inline bool __debug_virt_addr_valid (unsigned long x) +{ +if (x >=PAGE_OFFSET && x < (unsigned long )high_memory) +return true; + +return false; +} + +phys_addr_t __virt_to_phys (volatile const void *x) +{ +WARN (!__debug_virt_addr_valid ((unsigned long )x), +"virt_to_phys used for non-linear address: %pK (%pS)\n" , + x, x); + +return __virt_to_phys_nodebug (x); +} +EXPORT_SYMBOL (__virt_to_phys); + +phys_addr_t __phys_addr_symbol (unsigned long x) +{ +/* This is bounds checking against the kernel image only. +* __pa_symbol should only be used on kernel symbol addresses. +*/ +VIRTUAL_BUG_ON (x < (unsigned long )_text || + x > (unsigned long )_end); + +return __pa_symbol_nodebug (x); +} +EXPORT_SYMBOL (__phys_addr_symbol);
Signature · 1 line
2.9.3
Quoted 9 lines · Florian Fainelli
Provide hooks to intercept bad usages of
virt_to_phys()and__pa_symbol()throughout the kernel. To make this possible, we need to rename the current implement ofvirt_to_phys()into__virt_to_phys_nodebug()and wrap it around depending onCONFIG_DEBUG_VIRTUAL.A similar thing is needed for
__pa_symbol()which is now aliased to__phys_addr_symbol()whose implementation is either the direct return ofRELOC_HIDEor goes through the debug version.
Ping? This may not longer apply cleanly against arch/mips/Kconfig, but the conflicts should be trivial to resolve.
Thanks.
Quoted 131 lines
Signed-off-by Florian Fainelli <[email protected]>--- arch/mips/Kconfig | 1 + arch/mips/include/asm/io.h | 14 +++++++++++++- arch/mips/include/asm/page.h | 9 ++++++++- arch/mips/mm/Makefile | 2 ++ arch/mips/mm/physaddr.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 arch/mips/mm/physaddr.carch/mips/Kconfig +1
-0@@ -70,6 +70,7 @@ config MIPS select HAVE_EXIT_THREAD select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_ARCH_HARDENED_USERCOPY + select ARCH_HAS_DEBUG_VIRTUAL menu "Machine selection"arch/mips/include/asm/io.h +13
-1@@ -116,11 +116,23 @@ static inline void set_io_port_base(unsigned long base)* almost all conceivable cases a device driver should not be using * this function */ -static inline unsigned long virt_to_phys (volatile const void *address) +static inline unsigned long __virt_to_phys_nodebug (volatile const void *address) {return __pa (address); } +#ifdef CONFIG_DEBUG_VIRTUAL +extern phys_addr_t __virt_to_phys (volatile const void *x); +#else +#define __virt_to_phys (x) __virt_to_phys_nodebug(x) +#endif + +#define virt_to_phys virt_to_phys +static inline phys_addr_t virt_to_phys (const volatile void *x) +{ +return __virt_to_phys (x); +} +/* * phys_to_virt - map physical address to virtual * @address: address to remap arch/mips/include/asm/page.h +8
-1@@ -205,9 +205,16 @@ static inline unsigned long ___pa(unsigned long x)* until GCC 3.x has been retired before we can apply * https://patchwork.linux-mips.org/patch/1541/ */ +#define __pa_symbol_nodebug (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) + +#ifdef CONFIG_DEBUG_VIRTUAL +extern phys_addr_t __phys_addr_symbol (unsigned long x); +#else +#define __phys_addr_symbol (x) __pa_symbol_nodebug(x) +#endif #ifndef __pa_symbol -#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) +#define __pa_symbol (x) __phys_addr_symbol((unsigned long)(x))#endif #define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT)arch/mips/mm/Makefile +2
-0@@ -29,3 +29,5 @@ obj-$(CONFIG_R5000_CPU_SCACHE) += sc-r5k.o obj-$(CONFIG_RM7000_CPU_SCACHE) += sc-rm7k.o obj-$(CONFIG_MIPS_CPU_SCACHE) += sc-mips.o obj-$(CONFIG_SCACHE_DEBUGFS) += sc-debugfs.o + +obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.oarch/mips/mm/physaddr.c new file +40
-0@@ -0,0 +1,40 @@ +#include <linux/bug.h> +#include <linux/export.h> +#include <linux/types.h> +#include <linux/mmdebug.h> +#include <linux/mm.h> + +#include <asm/sections.h> +#include <asm/io.h> +#include <asm/page.h> +#include <asm/dma.h> + +static inline bool __debug_virt_addr_valid (unsigned long x) +{ +if (x >=PAGE_OFFSET && x < (unsigned long )high_memory) +return true; + +return false; +} + +phys_addr_t __virt_to_phys (volatile const void *x) +{ +WARN (!__debug_virt_addr_valid ((unsigned long )x), +"virt_to_phys used for non-linear address: %pK (%pS)\n" , + x, x); + +return __virt_to_phys_nodebug (x); +} +EXPORT_SYMBOL (__virt_to_phys); + +phys_addr_t __phys_addr_symbol (unsigned long x) +{ +/* This is bounds checking against the kernel image only. +* __pa_symbol should only be used on kernel symbol addresses. +*/ +VIRTUAL_BUG_ON (x < (unsigned long )_text || + x > (unsigned long )_end); + +return __pa_symbol_nodebug (x); +} +EXPORT_SYMBOL (__phys_addr_symbol);
Signature · 1 line
Florian