Commit f822f151 authored by Paul Mackerras's avatar Paul Mackerras Committed by Linus Torvalds

[PATCH] Move BUG/BUG_ON/WARN_ON to asm headers

This patch moves the definitions of BUG, BUG_ON and WARN_ON from
<linux/kernel.h> to <asm/bug.h> (which <linux/kernel.h> includes), and
supplies a new implementation for PPC which uses a conditional trap
instruction for BUG_ON and WARN_ON, thus avoiding a conditional
branch.  This patch trims over 50kB from the size of the kernel that I
use on powermacs.

With this patch, on PPC we have a __bug_table section in the vmlinux
binary, and also in modules if they use BUG, BUG_ON or WARN_ON.  The
__bug_table section has one entry for each BUG/BUG_ON/WARN_ON, giving
the address of the trap instruction and the corresponding line number,
filename and function name.  This information is used in the exception
handler for the exception that the trap instruction produces.  The
arch-specific module code handles the __bug_table section so that
BUG/BUG_ON/WARN_ON work correctly in modules.

Several architecture maintainers have acked this change.  It should be
completely benign for all of the other architectures (though they may
decide to do something similar if they have a conditional trap
instruction available).
parent 1eb83e09
......@@ -81,6 +81,7 @@ SECTIONS
/DISCARD/ : {
*(__ksymtab)
*(__ksymtab_strings)
*(__bug_table)
}
}
......@@ -16,6 +16,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/moduleloader.h>
#include <linux/elf.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
......@@ -29,6 +30,8 @@
#define DEBUGP(fmt , ...)
#endif
LIST_HEAD(module_bug_list);
void *module_alloc(unsigned long size)
{
if (size == 0)
......@@ -267,9 +270,48 @@ int module_finalize(const Elf_Ehdr *hdr,
const Elf_Shdr *sechdrs,
struct module *me)
{
char *secstrings;
unsigned int i;
me->arch.bug_table = NULL;
me->arch.num_bugs = 0;
/* Find the __bug_table section, if present */
secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
for (i = 1; i < hdr->e_shnum; i++) {
if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
continue;
me->arch.bug_table = (void *) sechdrs[i].sh_addr;
me->arch.num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
break;
}
/*
* Strictly speaking this should have a spinlock to protect against
* traversals, but since we only traverse on BUG()s, a spinlock
* could potentially lead to deadlock and thus be counter-productive.
*/
list_add(&me->arch.bug_list, &module_bug_list);
return 0;
}
void module_arch_cleanup(struct module *mod)
{
list_del(&mod->arch.bug_list);
}
struct bug_entry *module_find_bug(unsigned long bugaddr)
{
struct mod_arch_specific *mod;
unsigned int i;
struct bug_entry *bug;
list_for_each_entry(mod, &module_bug_list, bug_list) {
bug = mod->bug_table;
for (i = 0; i < mod->num_bugs; ++i, ++bug)
if (bugaddr == bug->bug_addr)
return bug;
}
return NULL;
}
......@@ -296,6 +296,56 @@ emulate_instruction(struct pt_regs *regs)
return(retval);
}
/*
* Look through the list of trap instructions that are used for BUG(),
* BUG_ON() and WARN_ON() and see if we hit one. At this point we know
* that the exception was caused by a trap instruction of some kind.
* Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
* otherwise.
*/
extern struct bug_entry __start___bug_table[], __stop___bug_table[];
#ifndef CONFIG_MODULES
#define module_find_bug(x) NULL
#endif
static struct bug_entry *find_bug(unsigned long bugaddr)
{
struct bug_entry *bug;
for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
if (bugaddr == bug->bug_addr)
return bug;
return module_find_bug(bugaddr);
}
int
check_bug_trap(struct pt_regs *regs)
{
struct bug_entry *bug;
unsigned long addr;
if (regs->msr & MSR_PR)
return 0; /* not in kernel */
addr = regs->nip; /* address of trap instruction */
if (addr < PAGE_OFFSET)
return 0;
bug = find_bug(regs->nip);
if (bug == NULL)
return 0;
if (bug->line & BUG_WARNING_TRAP) {
/* this is a WARN_ON rather than BUG/BUG_ON */
printk(KERN_ERR "Badness in %s at %s:%d\n",
bug->function, bug->file,
bug->line & ~BUG_WARNING_TRAP);
dump_stack();
return 1;
}
printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
bug->function, bug->file, bug->line);
return 0;
}
void
ProgramCheckException(struct pt_regs *regs)
{
......@@ -325,6 +375,10 @@ ProgramCheckException(struct pt_regs *regs)
/* trap exception */
if (debugger_bpt(regs))
return;
if (check_bug_trap(regs)) {
regs->nip += 4;
return;
}
_exception(SIGTRAP, regs);
return;
}
......
......@@ -54,6 +54,10 @@ SECTIONS
__ex_table : { *(__ex_table) }
__stop___ex_table = .;
__start___bug_table = .;
__bug_table : { *(__bug_table) }
__stop___bug_table = .;
/* Read-write section, merged into data segment: */
. = ALIGN(4096);
.data :
......
......@@ -9,6 +9,15 @@
__asm__ __volatile__("call_pal %0 # bugchk\n\t"".long %1\n\t.8byte %2" \
: : "i" (PAL_bugchk), "i"(__LINE__), "i"(__FILE__))
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) BUG()
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -18,4 +18,13 @@ extern volatile void __bug(const char *file, int line, void *data);
#endif
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -9,4 +9,13 @@
BUG(); \
} while (0)
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -9,4 +9,13 @@
BUG(); \
} while (0)
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -19,8 +19,17 @@
#define BUG() __asm__ __volatile__("ud2\n")
#endif
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -7,6 +7,16 @@
# define ia64_abort() (*(volatile int *) 0 = 0)
#endif
#define BUG() do { printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); ia64_abort(); } while (0)
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) do { BUG(); } while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -21,8 +21,20 @@
} while (0)
#endif
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -5,8 +5,20 @@
printk("%s(%d): kernel BUG!\n", __FILE__, __LINE__); \
} while (0)
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -3,6 +3,14 @@
#define __ASM_BUG_H
#define BUG() do { printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); *(int *)0=0; } while (0)
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) do { BUG(); } while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -2,6 +2,14 @@
#define _ASM_BUG_H
#define BUG() do { printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); *(int *)0=0; } while (0)
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) do { BUG(); } while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -10,8 +10,20 @@
dump_stack(); \
} while (0)
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
#ifndef _PPC_BUG_H
#define _PPC_BUG_H
#define BUG() do { \
printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \
__asm__ __volatile__(".long 0x0"); \
struct bug_entry {
unsigned long bug_addr;
int line;
const char *file;
const char *function;
};
/*
* If this bit is set in the line number it means that the trap
* is for WARN_ON rather than BUG or BUG_ON.
*/
#define BUG_WARNING_TRAP 0x1000000
#define BUG() do { \
__asm__ __volatile__( \
"1: twi 31,0,0\n" \
".section __bug_table,\"a\"\n\t" \
" .long 1b,%0,%1,%2\n" \
".previous" \
: : "i" (__LINE__), "i" (__FILE__), "i" (__FUNCTION__)); \
} while (0)
#define BUG_ON(x) do { \
__asm__ __volatile__( \
"1: twnei %0,0\n" \
".section __bug_table,\"a\"\n\t" \
" .long 1b,%1,%2,%3\n" \
".previous" \
: : "r" (x), "i" (__LINE__), "i" (__FILE__), \
"i" (__FUNCTION__)); \
} while (0)
#define PAGE_BUG(page) do { BUG(); } while (0)
#define WARN_ON(x) do { \
__asm__ __volatile__( \
"1: twnei %0,0\n" \
".section __bug_table,\"a\"\n\t" \
" .long 1b,%1,%2,%3\n" \
".previous" \
: : "r" (x), "i" (__LINE__ + BUG_WARNING_TRAP), \
"i" (__FILE__), "i" (__FUNCTION__)); \
} while (0)
#endif
......@@ -2,6 +2,9 @@
#define _ASM_PPC_MODULE_H
/* Module stuff for PPC. (C) 2001 Rusty Russell */
#include <linux/list.h>
#include <asm/bug.h>
/* Thanks to Paul M for explaining this.
PPC can only do rel jumps += 32MB, and often the kernel and other
......@@ -20,8 +23,15 @@ struct mod_arch_specific
{
/* Indices of PLT sections within module. */
unsigned int core_plt_section, init_plt_section;
/* List of BUG addresses, source line numbers and filenames */
struct list_head bug_list;
struct bug_entry *bug_table;
unsigned int num_bugs;
};
extern struct bug_entry *module_find_bug(unsigned long bugaddr);
#define Elf_Shdr Elf32_Shdr
#define Elf_Sym Elf32_Sym
#define Elf_Ehdr Elf32_Ehdr
......
......@@ -27,7 +27,16 @@ extern void xmon(struct pt_regs *excp);
} while (0)
#endif
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) do { BUG(); } while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
#endif
......@@ -6,8 +6,20 @@
__asm__ __volatile__(".long 0"); \
} while (0)
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -9,8 +9,20 @@
asm volatile("nop"); \
} while (0)
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -12,8 +12,20 @@ extern void do_BUG(const char *file, int line);
#define BUG() __builtin_trap()
#endif
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -13,8 +13,20 @@ extern void do_BUG(const char *file, int line);
#define BUG() __builtin_trap()
#endif
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -7,10 +7,22 @@
panic("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \
} while (0)
#define BUG_ON(condition) do { \
if (unlikely((condition)!=0)) \
BUG(); \
} while(0)
#define PAGE_BUG(page) do { \
BUG(); \
} while (0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
extern int foo;
#endif
......
......@@ -18,4 +18,13 @@ extern void __bug (void) __attribute__ ((noreturn));
#define BUG() __bug()
#define PAGE_BUG(page) __bug()
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif /* __V850_BUG_H__ */
......@@ -18,7 +18,15 @@ struct bug_frame {
#define BUG() \
asm volatile("ud2 ; .quad %c1 ; .short %c0" :: \
"i"(__LINE__), "i" (__stringify(KBUILD_BASENAME)))
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define PAGE_BUG(page) BUG()
void out_of_line_bug(void);
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
#endif
......@@ -228,14 +228,6 @@ struct sysinfo {
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
};
#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
#define WARN_ON(condition) do { \
if (unlikely((condition)!=0)) { \
printk("Badness in %s at %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
dump_stack(); \
} \
} while (0)
extern void BUILD_BUG(void);
#define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment