1182 lines · 30.8 KB · C
RawDownload
/* * Copyright (C) 2002-2006 Novell, Inc. *	Jan Beulich <jbeulich@novell.com> * This code is released under version 2 of the GNU GPL. * * A simple API for unwinding kernel stacks.  This is used for * debugging and error reporting purposes.  The kernel doesn't need * full-blown stack unwinding with all the bells and whistles, so there * is not much point in implementing the full Dwarf2 unwind API. */ #include <linux/unwind.h>#include <linux/module.h>#include <linux/bootmem.h>#include <linux/sort.h>#include <linux/stop_machine.h>#include <asm/sections.h>#include <asm/uaccess.h>#include <asm/unaligned.h> extern char __start_unwind[], __end_unwind[];extern const u8 __start_unwind_hdr[], __end_unwind_hdr[]; #define MAX_STACK_DEPTH 8 #define EXTRA_INFO(f) { \		BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \		                  % FIELD_SIZEOF(struct unwind_frame_info, f)) \		+ offsetof(struct unwind_frame_info, f) \		  / FIELD_SIZEOF(struct unwind_frame_info, f), \		FIELD_SIZEOF(struct unwind_frame_info, f) \	}#define PTREGS_INFO(f) EXTRA_INFO(regs.f) static const struct {	unsigned offs:BITS_PER_LONG / 2;	unsigned width:BITS_PER_LONG / 2;} reg_info[] = {	UNW_REGISTER_INFO}; #undef PTREGS_INFO#undef EXTRA_INFO #ifndef REG_INVALID#define REG_INVALID(r) (reg_info[r].width == 0)#endif #define DW_CFA_nop                          0x00#define DW_CFA_set_loc                      0x01#define DW_CFA_advance_loc1                 0x02#define DW_CFA_advance_loc2                 0x03#define DW_CFA_advance_loc4                 0x04#define DW_CFA_offset_extended              0x05#define DW_CFA_restore_extended             0x06#define DW_CFA_undefined                    0x07#define DW_CFA_same_value                   0x08#define DW_CFA_register                     0x09#define DW_CFA_remember_state               0x0a#define DW_CFA_restore_state                0x0b#define DW_CFA_def_cfa                      0x0c#define DW_CFA_def_cfa_register             0x0d#define DW_CFA_def_cfa_offset               0x0e#define DW_CFA_def_cfa_expression           0x0f#define DW_CFA_expression                   0x10#define DW_CFA_offset_extended_sf           0x11#define DW_CFA_def_cfa_sf                   0x12#define DW_CFA_def_cfa_offset_sf            0x13#define DW_CFA_val_offset                   0x14#define DW_CFA_val_offset_sf                0x15#define DW_CFA_val_expression               0x16#define DW_CFA_lo_user                      0x1c#define DW_CFA_GNU_window_save              0x2d#define DW_CFA_GNU_args_size                0x2e#define DW_CFA_GNU_negative_offset_extended 0x2f#define DW_CFA_hi_user                      0x3f #define DW_EH_PE_FORM     0x07#define DW_EH_PE_native   0x00#define DW_EH_PE_leb128   0x01#define DW_EH_PE_data2    0x02#define DW_EH_PE_data4    0x03#define DW_EH_PE_data8    0x04#define DW_EH_PE_signed   0x08#define DW_EH_PE_ADJUST   0x70#define DW_EH_PE_abs      0x00#define DW_EH_PE_pcrel    0x10#define DW_EH_PE_textrel  0x20#define DW_EH_PE_datarel  0x30#define DW_EH_PE_funcrel  0x40#define DW_EH_PE_aligned  0x50#define DW_EH_PE_indirect 0x80#define DW_EH_PE_omit     0xff typedef unsigned long uleb128_t;typedef   signed long sleb128_t; static struct unwind_table {	struct {		unsigned long pc;		unsigned long range;	} core, init;	const void *address;	unsigned long size;	const unsigned char *header;	unsigned long hdrsz;	struct unwind_table *link;	const char *name;} root_table; struct unwind_item {	enum item_location {		Nowhere,		Memory,		Register,		Value	} where;	uleb128_t value;}; struct unwind_state {	uleb128_t loc, org;	const u8 *cieStart, *cieEnd;	uleb128_t codeAlign;	sleb128_t dataAlign;	struct cfa {		uleb128_t reg, offs;	} cfa;	struct unwind_item regs[ARRAY_SIZE(reg_info)];	unsigned stackDepth:8;	unsigned version:8;	const u8 *label;	const u8 *stack[MAX_STACK_DEPTH];}; static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 }; static struct unwind_table *find_table(unsigned long pc){	struct unwind_table *table; 	for (table = &root_table; table; table = table->link)		if ((pc >= table->core.pc		     && pc < table->core.pc + table->core.range)		    || (pc >= table->init.pc		        && pc < table->init.pc + table->init.range))			break; 	return table;} static unsigned long read_pointer(const u8 **pLoc,                                  const void *end,                                  signed ptrType); static void init_unwind_table(struct unwind_table *table,                              const char *name,                              const void *core_start,                              unsigned long core_size,                              const void *init_start,                              unsigned long init_size,                              const void *table_start,                              unsigned long table_size,                              const u8 *header_start,                              unsigned long header_size){	const u8 *ptr = header_start + 4;	const u8 *end = header_start + header_size; 	table->core.pc = (unsigned long)core_start;	table->core.range = core_size;	table->init.pc = (unsigned long)init_start;	table->init.range = init_size;	table->address = table_start;	table->size = table_size;	/* See if the linker provided table looks valid. */	if (header_size <= 4	    || header_start[0] != 1	    || (void *)read_pointer(&ptr, end, header_start[1]) != table_start	    || header_start[2] == DW_EH_PE_omit	    || read_pointer(&ptr, end, header_start[2]) <= 0	    || header_start[3] == DW_EH_PE_omit)		header_start = NULL;	table->hdrsz = header_size;	smp_wmb();	table->header = header_start;	table->link = NULL;	table->name = name;} void __init unwind_init(void){	init_unwind_table(&root_table, "kernel",	                  _text, _end - _text,	                  NULL, 0,	                  __start_unwind, __end_unwind - __start_unwind,	                  __start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);} static const u32 bad_cie, not_fde;static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *);static signed fde_pointer_type(const u32 *cie); struct eh_frame_hdr_table_entry {	unsigned long start, fde;}; static int cmp_eh_frame_hdr_table_entries(const void *p1, const void *p2){	const struct eh_frame_hdr_table_entry *e1 = p1;	const struct eh_frame_hdr_table_entry *e2 = p2; 	return (e1->start > e2->start) - (e1->start < e2->start);} static void swap_eh_frame_hdr_table_entries(void *p1, void *p2, int size){	struct eh_frame_hdr_table_entry *e1 = p1;	struct eh_frame_hdr_table_entry *e2 = p2;	unsigned long v; 	v = e1->start;	e1->start = e2->start;	e2->start = v;	v = e1->fde;	e1->fde = e2->fde;	e2->fde = v;} static void __init setup_unwind_table(struct unwind_table *table,					void *(*alloc)(unsigned long)){	const u8 *ptr;	unsigned long tableSize = table->size, hdrSize;	unsigned n;	const u32 *fde;	struct {		u8 version;		u8 eh_frame_ptr_enc;		u8 fde_count_enc;		u8 table_enc;		unsigned long eh_frame_ptr;		unsigned int fde_count;		struct eh_frame_hdr_table_entry table[];	} __attribute__((__packed__)) *header; 	if (table->header)		return; 	if (table->hdrsz)		printk(KERN_WARNING ".eh_frame_hdr for '%s' present but unusable\n",		       table->name); 	if (tableSize & (sizeof(*fde) - 1))		return; 	for (fde = table->address, n = 0;	     tableSize > sizeof(*fde) && tableSize - sizeof(*fde) >= *fde;	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {		const u32 *cie = cie_for_fde(fde, table);		signed ptrType; 		if (cie == &not_fde)			continue;		if (cie == NULL		    || cie == &bad_cie		    || (ptrType = fde_pointer_type(cie)) < 0)			return;		ptr = (const u8 *)(fde + 2);		if (!read_pointer(&ptr,		                  (const u8 *)(fde + 1) + *fde,		                  ptrType))			return;		++n;	} 	if (tableSize || !n)		return; 	hdrSize = 4 + sizeof(unsigned long) + sizeof(unsigned int)	        + 2 * n * sizeof(unsigned long);	header = alloc(hdrSize);	if (!header)		return;	header->version          = 1;	header->eh_frame_ptr_enc = DW_EH_PE_abs|DW_EH_PE_native;	header->fde_count_enc    = DW_EH_PE_abs|DW_EH_PE_data4;	header->table_enc        = DW_EH_PE_abs|DW_EH_PE_native;	put_unaligned((unsigned long)table->address, &header->eh_frame_ptr);	BUILD_BUG_ON(offsetof(typeof(*header), fde_count)	             % __alignof(typeof(header->fde_count)));	header->fde_count        = n; 	BUILD_BUG_ON(offsetof(typeof(*header), table)	             % __alignof(typeof(*header->table)));	for (fde = table->address, tableSize = table->size, n = 0;	     tableSize;	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {		const u32 *cie = fde + 1 - fde[1] / sizeof(*fde); 		if (!fde[1])			continue; /* this is a CIE */		ptr = (const u8 *)(fde + 2);		header->table[n].start = read_pointer(&ptr,		                                      (const u8 *)(fde + 1) + *fde,		                                      fde_pointer_type(cie));		header->table[n].fde = (unsigned long)fde;		++n;	}	WARN_ON(n != header->fde_count); 	sort(header->table,	     n,	     sizeof(*header->table),	     cmp_eh_frame_hdr_table_entries,	     swap_eh_frame_hdr_table_entries); 	table->hdrsz = hdrSize;	smp_wmb();	table->header = (const void *)header;} static void *__init balloc(unsigned long sz){	return __alloc_bootmem_nopanic(sz,	                               sizeof(unsigned int),	                               __pa(MAX_DMA_ADDRESS));} void __init unwind_setup(void){	setup_unwind_table(&root_table, balloc);} #ifdef CONFIG_MODULES static struct unwind_table *last_table; /* Must be called with module_mutex held. */void *unwind_add_table(struct module *module,                       const void *table_start,                       unsigned long table_size){	struct unwind_table *table; 	if (table_size <= 0)		return NULL; 	table = kmalloc(sizeof(*table), GFP_KERNEL);	if (!table)		return NULL; 	init_unwind_table(table, module->name,	                  module->module_core, module->core_size,	                  module->module_init, module->init_size,	                  table_start, table_size,	                  NULL, 0); 	if (last_table)		last_table->link = table;	else		root_table.link = table;	last_table = table; 	return table;} struct unlink_table_info{	struct unwind_table *table;	int init_only;}; static int unlink_table(void *arg){	struct unlink_table_info *info = arg;	struct unwind_table *table = info->table, *prev; 	for (prev = &root_table; prev->link && prev->link != table; prev = prev->link)		; 	if (prev->link) {		if (info->init_only) {			table->init.pc = 0;			table->init.range = 0;			info->table = NULL;		} else {			prev->link = table->link;			if (!prev->link)				last_table = prev;		}	} else		info->table = NULL; 	return 0;} /* Must be called with module_mutex held. */void unwind_remove_table(void *handle, int init_only){	struct unwind_table *table = handle;	struct unlink_table_info info; 	if (!table || table == &root_table)		return; 	if (init_only && table == last_table) {		table->init.pc = 0;		table->init.range = 0;		return;	} 	info.table = table;	info.init_only = init_only;	stop_machine_run(unlink_table, &info, NR_CPUS); 	if (info.table)		kfree(table);} #endif /* CONFIG_MODULES */ static uleb128_t get_uleb128(const u8 **pcur, const u8 *end){	const u8 *cur = *pcur;	uleb128_t value;	unsigned shift; 	for (shift = 0, value = 0; cur < end; shift += 7) {		if (shift + 7 > 8 * sizeof(value)		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {			cur = end + 1;			break;		}		value |= (uleb128_t)(*cur & 0x7f) << shift;		if (!(*cur++ & 0x80))			break;	}	*pcur = cur; 	return value;} static sleb128_t get_sleb128(const u8 **pcur, const u8 *end){	const u8 *cur = *pcur;	sleb128_t value;	unsigned shift; 	for (shift = 0, value = 0; cur < end; shift += 7) {		if (shift + 7 > 8 * sizeof(value)		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {			cur = end + 1;			break;		}		value |= (sleb128_t)(*cur & 0x7f) << shift;		if (!(*cur & 0x80)) {			value |= -(*cur++ & 0x40) << shift;			break;		}	}	*pcur = cur; 	return value;} static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *table){	const u32 *cie; 	if (!*fde || (*fde & (sizeof(*fde) - 1)))		return &bad_cie;	if (!fde[1])		return &not_fde; /* this is a CIE */	if ((fde[1] & (sizeof(*fde) - 1))	    || fde[1] > (unsigned long)(fde + 1) - (unsigned long)table->address)		return NULL; /* this is not a valid FDE */	cie = fde + 1 - fde[1] / sizeof(*fde);	if (*cie <= sizeof(*cie) + 4	    || *cie >= fde[1] - sizeof(*fde)	    || (*cie & (sizeof(*cie) - 1))	    || cie[1])		return NULL; /* this is not a (valid) CIE */	return cie;} static unsigned long read_pointer(const u8 **pLoc,                                  const void *end,                                  signed ptrType){	unsigned long value = 0;	union {		const u8 *p8;		const u16 *p16u;		const s16 *p16s;		const u32 *p32u;		const s32 *p32s;		const unsigned long *pul;	} ptr; 

Lines 1–500 of 1182.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t