blob: 0b3101f40b71e9c555b52bede4453b8901ee68a6 [file] [log] [blame]
/*
* Kernel Debugger Architecture Independent Support Functions
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
* 03/02/13 added new 2.5 kallsyms <xavier.bru@bull.net>
*/
#include <stdarg.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/kallsyms.h>
#include <linux/stddef.h>
#include <linux/vmalloc.h>
#include <linux/ptrace.h>
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/hardirq.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <linux/lkdb.h>
#include <linux/kdbprivate.h>
/*
* Symbol table functions.
*/
/*
* lkdbgetsymval
*
* Return the address of the given symbol.
*
* Parameters:
* symname Character string containing symbol name
* symtab Structure to receive results
* Outputs:
* Returns:
* 0 Symbol not found, symtab zero filled
* 1 Symbol mapped to module/symbol/section, data in symtab
* Locking:
* None.
* Remarks:
*/
int
lkdbgetsymval(const char *symname, lkdb_symtab_t *symtab)
{
if (KDB_DEBUG(AR))
lkdb_printf("lkdbgetsymval: symname=%s, symtab=%p\n", symname, symtab);
memset(symtab, 0, sizeof(*symtab));
if ((symtab->sym_start = kallsyms_lookup_name(symname))) {
if (KDB_DEBUG(AR))
lkdb_printf("lkdbgetsymval: returns 1, symtab->sym_start=0x%lx\n", symtab->sym_start);
return 1;
}
if (KDB_DEBUG(AR))
lkdb_printf("lkdbgetsymval: returns 0\n");
return 0;
}
EXPORT_SYMBOL(lkdbgetsymval);
/*
* lkdbnearsym
*
* Return the name of the symbol with the nearest address
* less than 'addr'.
*
* Parameters:
* addr Address to check for symbol near
* symtab Structure to receive results
* Outputs:
* Returns:
* 0 No sections contain this address, symtab zero filled
* 1 Address mapped to module/symbol/section, data in symtab
* Locking:
* None.
* Remarks:
* 2.6 kallsyms has a "feature" where it unpacks the name into a string.
* If that string is reused before the caller expects it then the caller
* sees its string change without warning. To avoid cluttering up the
* main kdb code with lots of lkdb_strdup, tests and kfree calls, lkdbnearsym
* maintains an LRU list of the last few unique strings. The list is sized
* large enough to hold active strings, no kdb caller of lkdbnearsym makes
* more than ~20 later calls before using a saved value.
*/
static char *kdb_name_table[100]; /* arbitrary size */
int
lkdbnearsym(unsigned long addr, lkdb_symtab_t *symtab)
{
int ret = 0;
unsigned long symbolsize;
unsigned long offset;
#define knt1_size 128 /* must be >= kallsyms table size */
char *knt1 = NULL;
if (KDB_DEBUG(AR))
lkdb_printf("lkdbnearsym: addr=0x%lx, symtab=%p\n", addr, symtab);
memset(symtab, 0, sizeof(*symtab));
if (addr < 4096)
goto out;
knt1 = ldebug_kmalloc(knt1_size, GFP_ATOMIC);
if (!knt1) {
lkdb_printf("lkdbnearsym: addr=0x%lx cannot kmalloc knt1\n", addr);
goto out;
}
symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset, (char **)(&symtab->mod_name), knt1);
if (offset > 8*1024*1024) {
symtab->sym_name = NULL;
addr = offset = symbolsize = 0;
}
symtab->sym_start = addr - offset;
symtab->sym_end = symtab->sym_start + symbolsize;
ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
if (ret) {
int i;
/* Another 2.6 kallsyms "feature". Sometimes the sym_name is
* set but the buffer passed into kallsyms_lookup is not used,
* so it contains garbage. The caller has to work out which
* buffer needs to be saved.
*
* What was Rusty smoking when he wrote that code?
*/
if (symtab->sym_name != knt1) {
strncpy(knt1, symtab->sym_name, knt1_size);
knt1[knt1_size-1] = '\0';
}
for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
if (kdb_name_table[i] && strcmp(kdb_name_table[i], knt1) == 0)
break;
}
if (i >= ARRAY_SIZE(kdb_name_table)) {
ldebug_kfree(kdb_name_table[0]);
memcpy(kdb_name_table, kdb_name_table+1,
sizeof(kdb_name_table[0])*(ARRAY_SIZE(kdb_name_table)-1));
} else {
ldebug_kfree(knt1);
knt1 = kdb_name_table[i];
memcpy(kdb_name_table+i, kdb_name_table+i+1,
sizeof(kdb_name_table[0])*(ARRAY_SIZE(kdb_name_table)-i-1));
}
i = ARRAY_SIZE(kdb_name_table) - 1;
kdb_name_table[i] = knt1;
symtab->sym_name = kdb_name_table[i];
knt1 = NULL;
}
if (symtab->mod_name == NULL)
symtab->mod_name = "kernel";
if (KDB_DEBUG(AR))
lkdb_printf("lkdbnearsym: returns %d symtab->sym_start=0x%lx, symtab->mod_name=%p, symtab->sym_name=%p (%s)\n", ret, symtab->sym_start, symtab->mod_name, symtab->sym_name, symtab->sym_name);
out:
ldebug_kfree(knt1);
return ret;
}
void
lkdbnearsym_cleanup(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
if (kdb_name_table[i]) {
ldebug_kfree(kdb_name_table[i]);
kdb_name_table[i] = NULL;
}
}
}
/*
* lkallsyms_symbol_complete
*
* Parameters:
* prefix_name prefix of a symbol name to lookup
* max_len maximum length that can be returned
* Returns:
* Number of symbols which match the given prefix.
* Notes:
* prefix_name is changed to contain the longest unique prefix that
* starts with this prefix (tab completion).
*/
static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
int lkallsyms_symbol_complete(char *prefix_name, int max_len)
{
loff_t pos = 0;
int prefix_len = strlen(prefix_name), prev_len = 0;
int i, number = 0;
const char *name;
while ((name = kdb_walk_kallsyms(&pos))) {
if (strncmp(name, prefix_name, prefix_len) == 0) {
strcpy(ks_namebuf, name);
/* Work out the longest name that matches the prefix */
if (++number == 1) {
prev_len = min_t(int, max_len-1, strlen(ks_namebuf));
memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
ks_namebuf_prev[prev_len] = '\0';
} else for (i = 0; i < prev_len; ++i) {
if (ks_namebuf[i] != ks_namebuf_prev[i]) {
prev_len = i;
ks_namebuf_prev[i] = '\0';
break;
}
}
}
}
if (prev_len > prefix_len)
memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
return number;
}
/*
* lkallsyms_symbol_next
*
* Parameters:
* prefix_name prefix of a symbol name to lookup
* flag 0 means search from the head, 1 means continue search.
* Returns:
* 1 if a symbol matches the given prefix.
* 0 if no string found
*/
int lkallsyms_symbol_next(char *prefix_name, int flag)
{
int prefix_len = strlen(prefix_name);
static loff_t pos;
const char *name;
if (!flag)
pos = 0;
while ((name = kdb_walk_kallsyms(&pos))) {
if (strncmp(name, prefix_name, prefix_len) == 0) {
strncpy(prefix_name, name, strlen(name)+1);
return 1;
}
}
return 0;
}
#if defined(CONFIG_SMP)
/*
* kdb_ipi
*
* This function is called from the non-maskable interrupt
* handler to handle a kdb IPI instruction.
*
* Inputs:
* regs = Exception frame pointer
* Outputs:
* None.
* Returns:
* 0 - Did not handle NMI
* 1 - Handled NMI
* Locking:
* None.
* Remarks:
* Initially one processor is invoked in the kdb() code. That
* processor sends an ipi which drives this routine on the other
* processors. All this does is call kdb() with reason SWITCH.
* This puts all processors into the kdb() routine and all the
* code for breakpoints etc. is in one place.
* One problem with the way the kdb NMI is sent, the NMI has no
* identification that says it came from kdb. If the cpu's kdb state is
* marked as "waiting for kdb_ipi" then the NMI is treated as coming from
* kdb, otherwise it is assumed to be for another reason and is ignored.
*/
int
kdb_ipi(struct pt_regs *regs, void (*ack_interrupt)(void))
{
/* Do not print before checking and clearing WAIT_IPI, IPIs are
* going all the time.
*/
if (KDB_STATE(WAIT_IPI)) {
/*
* Stopping other processors via smp_kdb_stop().
*/
if (ack_interrupt)
(*ack_interrupt)(); /* Acknowledge the interrupt */
KDB_STATE_CLEAR(WAIT_IPI);
KDB_DEBUG_STATE("kdb_ipi 1", 0);
kdb(LKDB_REASON_SWITCH, 0, regs); /* Spin in kdb() */
KDB_DEBUG_STATE("kdb_ipi 2", 0);
return 1;
}
return 0;
}
#endif /* CONFIG_SMP */
/*
* lkdb_symbol_print
*
* Standard method for printing a symbol name and offset.
* Inputs:
* addr Address to be printed.
* symtab Address of symbol data, if NULL this routine does its
* own lookup.
* punc Punctuation for string, bit field.
* Outputs:
* None.
* Returns:
* Always 0.
* Locking:
* none.
* Remarks:
* The string and its punctuation is only printed if the address
* is inside the kernel, except that the value is always printed
* when requested.
*/
void
lkdb_symbol_print(kdb_machreg_t addr, const lkdb_symtab_t *symtab_p, unsigned int punc)
{
lkdb_symtab_t symtab, *symtab_p2;
if (symtab_p) {
symtab_p2 = (lkdb_symtab_t *)symtab_p;
}
else {
symtab_p2 = &symtab;
lkdbnearsym(addr, symtab_p2);
}
if (symtab_p2->sym_name || (punc & KDB_SP_VALUE)) {
; /* drop through */
}
else {
return;
}
if (punc & KDB_SP_SPACEB) {
lkdb_printf(" ");
}
if (punc & KDB_SP_VALUE) {
lkdb_printf(kdb_machreg_fmt0, addr);
}
if (symtab_p2->sym_name) {
if (punc & KDB_SP_VALUE) {
lkdb_printf(" ");
}
if (punc & KDB_SP_PAREN) {
lkdb_printf("(");
}
if (strcmp(symtab_p2->mod_name, "kernel")) {
lkdb_printf("[%s]", symtab_p2->mod_name);
}
lkdb_printf("%s", symtab_p2->sym_name);
if (addr != symtab_p2->sym_start) {
lkdb_printf("+0x%lx", addr - symtab_p2->sym_start);
}
if (punc & KDB_SP_SYMSIZE) {
lkdb_printf("/0x%lx", symtab_p2->sym_end - symtab_p2->sym_start);
}
if (punc & KDB_SP_PAREN) {
lkdb_printf(")");
}
}
if (punc & KDB_SP_SPACEA) {
lkdb_printf(" ");
}
if (punc & KDB_SP_NEWLINE) {
lkdb_printf("\n");
}
}
/*
* lkdb_strdup
*
* kdb equivalent of strdup, for disasm code.
* Inputs:
* str The string to duplicate.
* type Flags to kmalloc for the new string.
* Outputs:
* None.
* Returns:
* Address of the new string, NULL if storage could not be allocated.
* Locking:
* none.
* Remarks:
* This is not in lib/string.c because it uses kmalloc which is not
* available when string.o is used in boot loaders.
*/
char *lkdb_strdup(const char *str, gfp_t type)
{
int n = strlen(str)+1;
char *s = kmalloc(n, type);
if (!s) return NULL;
return strcpy(s, str);
}
/*
* lkdb_getarea_size
*
* Read an area of data. The kdb equivalent of copy_from_user, with
* kdb messages for invalid addresses.
* Inputs:
* res Pointer to the area to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int lkdb_getarea_size(void *res, unsigned long addr, size_t size)
{
int ret = kdba_getarea_size(res, addr, size);
if (ret) {
if (!KDB_STATE(SUPPRESS)) {
lkdb_printf("lkdb_getarea: Bad address 0x%lx\n", addr);
KDB_STATE_SET(SUPPRESS);
}
ret = LKDB_BADADDR;
}
else {
KDB_STATE_CLEAR(SUPPRESS);
}
return(ret);
}
/*
* lkdb_putarea_size
*
* Write an area of data. The kdb equivalent of copy_to_user, with
* kdb messages for invalid addresses.
* Inputs:
* addr Address of the area to write to.
* res Pointer to the area holding the data.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int lkdb_putarea_size(unsigned long addr, void *res, size_t size)
{
int ret = kdba_putarea_size(addr, res, size);
if (ret) {
if (!KDB_STATE(SUPPRESS)) {
lkdb_printf("lkdb_putarea: Bad address 0x%lx\n", addr);
KDB_STATE_SET(SUPPRESS);
}
ret = LKDB_BADADDR;
}
else {
KDB_STATE_CLEAR(SUPPRESS);
}
return(ret);
}
/*
* kdb_getphys
*
* Read data from a physical address. Validate the address is in range,
* use kmap_atomic() to get data
*
* Similar to lkdb_getarea() - but for phys addresses
*
* Inputs:
* res Pointer to the word to receive the result
* addr Physical address of the area to copy
* size Size of the area
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
static int kdb_getphys(void *res, unsigned long addr, size_t size)
{
unsigned long pfn;
void *vaddr;
struct page *page;
pfn = (addr >> PAGE_SHIFT);
if (!pfn_valid(pfn))
return 1;
page = pfn_to_page(pfn);
vaddr = kmap_atomic(page, KM_KDB);
memcpy(res, vaddr + (addr & (PAGE_SIZE -1)), size);
kunmap_atomic(vaddr, KM_KDB);
return 0;
}
/*
* lkdb_getphysword
*
* Inputs:
* word Pointer to the word to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int lkdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
*word = 0; /* Default value if addr or size is invalid */
switch (size) {
case 1:
if (!(diag = kdb_getphys(&w1, addr, sizeof(w1))))
*word = w1;
break;
case 2:
if (!(diag = kdb_getphys(&w2, addr, sizeof(w2))))
*word = w2;
break;
case 4:
if (!(diag = kdb_getphys(&w4, addr, sizeof(w4))))
*word = w4;
break;
case 8:
if (size <= sizeof(*word)) {
if (!(diag = kdb_getphys(&w8, addr, sizeof(w8))))
*word = w8;
break;
}
/* drop through */
default:
diag = LKDB_BADWIDTH;
lkdb_printf("lkdb_getphysword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* lkdb_getword
*
* Read a binary value. Unlike lkdb_getarea, this treats data as numbers.
* Inputs:
* word Pointer to the word to receive the result.
* addr Address of the area to copy.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int lkdb_getword(unsigned long *word, unsigned long addr, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
*word = 0; /* Default value if addr or size is invalid */
switch (size) {
case 1:
if (!(diag = lkdb_getarea(w1, addr)))
*word = w1;
break;
case 2:
if (!(diag = lkdb_getarea(w2, addr)))
*word = w2;
break;
case 4:
if (!(diag = lkdb_getarea(w4, addr)))
*word = w4;
break;
case 8:
if (size <= sizeof(*word)) {
if (!(diag = lkdb_getarea(w8, addr)))
*word = w8;
break;
}
/* drop through */
default:
diag = LKDB_BADWIDTH;
lkdb_printf("lkdb_getword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* lkdb_putword
*
* Write a binary value. Unlike lkdb_putarea, this treats data as numbers.
* Inputs:
* addr Address of the area to write to..
* word The value to set.
* size Size of the area.
* Outputs:
* none.
* Returns:
* 0 for success, < 0 for error.
* Locking:
* none.
*/
int lkdb_putword(unsigned long addr, unsigned long word, size_t size)
{
int diag;
__u8 w1;
__u16 w2;
__u32 w4;
__u64 w8;
switch (size) {
case 1:
w1 = word;
diag = lkdb_putarea(addr, w1);
break;
case 2:
w2 = word;
diag = lkdb_putarea(addr, w2);
break;
case 4:
w4 = word;
diag = lkdb_putarea(addr, w4);
break;
case 8:
if (size <= sizeof(word)) {
w8 = word;
diag = lkdb_putarea(addr, w8);
break;
}
/* drop through */
default:
diag = LKDB_BADWIDTH;
lkdb_printf("lkdb_putword: bad width %ld\n", (long) size);
}
return(diag);
}
/*
* lkdb_task_state_string
*
* Convert a string containing any of the letters DRSTCZEUIMA to a mask
* for the process state field and return the value. If no argument is
* supplied, return the mask that corresponds to environment variable PS,
* DRSTCZEU by default.
* Inputs:
* s String to convert
* Outputs:
* none.
* Returns:
* Mask for process state.
* Locking:
* none.
* Notes:
* The mask folds data from several sources into a single long value, so
* be carefull not to overlap the bits. TASK_* bits are in the LSB,
* special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there
* is no overlap between TASK_* and EXIT_* but that may not always be
* true, so EXIT_* bits are shifted left 16 bits before being stored in
* the mask.
*/
#define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1)) /* unrunnable is < 0 */
#define RUNNING (1UL << (8*sizeof(unsigned long) - 2))
#define IDLE (1UL << (8*sizeof(unsigned long) - 3))
#define DAEMON (1UL << (8*sizeof(unsigned long) - 4))
unsigned long
lkdb_task_state_string(const char *s)
{
long res = 0;
if (!s && !(s = lkdbgetenv("PS"))) {
s = "DRSTCZEU"; /* default value for ps */
}
while (*s) {
switch (*s) {
case 'D': res |= TASK_UNINTERRUPTIBLE; break;
case 'R': res |= RUNNING; break;
case 'S': res |= TASK_INTERRUPTIBLE; break;
case 'T': res |= TASK_STOPPED; break;
case 'C': res |= TASK_TRACED; break;
case 'Z': res |= EXIT_ZOMBIE << 16; break;
case 'E': res |= EXIT_DEAD << 16; break;
case 'U': res |= UNRUNNABLE; break;
case 'I': res |= IDLE; break;
case 'M': res |= DAEMON; break;
case 'A': res = ~0UL; break;
default:
lkdb_printf("%s: unknown flag '%c' ignored\n", __FUNCTION__, *s);
break;
}
++s;
}
return res;
}
/*
* lkdb_task_state_char
*
* Return the character that represents the task state.
* Inputs:
* p struct task for the process
* Outputs:
* none.
* Returns:
* One character to represent the task state.
* Locking:
* none.
*/
char
lkdb_task_state_char (const struct task_struct *p)
{
int cpu = lkdb_process_cpu(p);
struct lkdb_running_process *krp = lkdb_running_process + cpu;
char state = (p->state == 0) ? 'R' :
(p->state < 0) ? 'U' :
(p->state & TASK_UNINTERRUPTIBLE) ? 'D' :
(p->state & TASK_STOPPED) ? 'T' :
(p->state & TASK_TRACED) ? 'C' :
(p->exit_state & EXIT_ZOMBIE) ? 'Z' :
(p->exit_state & EXIT_DEAD) ? 'E' :
(p->state & TASK_INTERRUPTIBLE) ? 'S' : '?';
if (p->pid == 0) {
/*
* Idle task. Is it really idle, apart from the kdb interrupt?
* In 3.0 all the idle threads except the first are 'kworker's.
*/
if (!lkdb_task_has_cpu(p) || krp->irq_depth == 1) {
/* There is a corner case when the idle task takes an
* interrupt and dies in the interrupt code. It has an
* interrupt count of 1 but that did not come from kdb.
* This corner case can only occur on the initial cpu,
* all the others were entered via the kdb IPI.
*/
if (cpu != lkdb_initial_cpu ||
KDB_STATE_CPU(KEYBOARD, cpu))
state = 'I'; /* idle task */
}
} else if (!p->mm && state == 'S') {
state = 'M'; /* sleeping system daemon */
}
return state;
}
/*
* lkdb_task_state
*
* Return true if a process has the desired state given by the mask.
* Inputs:
* p struct task for the process
* mask mask from lkdb_task_state_string to select processes
* Outputs:
* none.
* Returns:
* True if the process matches at least one criteria defined by the mask.
* Locking:
* none.
*/
unsigned long
lkdb_task_state(const struct task_struct *p, unsigned long mask)
{
char state[] = { lkdb_task_state_char(p), '\0' };
return (mask & lkdb_task_state_string(state)) != 0;
}
struct lkdb_running_process lkdb_running_process[NR_CPUS];
/* Save the state of a running process and invoke lkdb_main_loop. This is
* invoked on the current process on each cpu (assuming the cpu is responding).
*/
int
kdb_save_running(struct pt_regs *regs, lkdb_reason_t reason,
lkdb_reason_t reason2, int error, kdb_dbtrap_t db_result)
{
struct lkdb_running_process *krp = lkdb_running_process + smp_processor_id();
krp->p = current;
krp->regs = regs;
krp->seqno = lkdb_seqno;
krp->irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
kdba_save_running(&(krp->arch), regs);
return lkdb_main_loop(reason, reason2, error, db_result, regs);
}
/*
* kdb_unsave_running
*
* Reverse the effect of kdb_save_running.
* Inputs:
* regs struct pt_regs for the process
* Outputs:
* Updates lkdb_running_process[] for this cpu.
* Returns:
* none.
* Locking:
* none.
*/
void
kdb_unsave_running(struct pt_regs *regs)
{
struct lkdb_running_process *krp = lkdb_running_process + smp_processor_id();
kdba_unsave_running(&(krp->arch), regs);
krp->seqno = 0;
}
/*
* lkdb_print_nameval
*
* Print a name and its value, converting the value to a symbol lookup
* if possible.
* Inputs:
* name field name to print
* val value of field
* Outputs:
* none.
* Returns:
* none.
* Locking:
* none.
*/
void
lkdb_print_nameval(const char *name, unsigned long val)
{
lkdb_symtab_t symtab;
lkdb_printf(" %-11.11s ", name);
if (lkdbnearsym(val, &symtab))
lkdb_symbol_print(val, &symtab, KDB_SP_VALUE|KDB_SP_SYMSIZE|KDB_SP_NEWLINE);
else
lkdb_printf("0x%lx\n", val);
}
static struct page * kdb_get_one_user_page(const struct task_struct *tsk, unsigned long start,
int len, int write)
{
struct mm_struct *mm = tsk->mm;
unsigned int flags;
struct vm_area_struct * vma;
/* shouldn't cross a page boundary. */
if ((start & PAGE_MASK) != ((start+len) & PAGE_MASK))
return NULL;
/* we need to align start address to the current page boundy, PAGE_ALIGN
* aligns to next page boundry.
* FIXME: What about hugetlb?
*/
start = start & PAGE_MASK;
flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
vma = find_extend_vma(mm, start);
/* may be we can allow access to VM_IO pages inside KDB? */
if (!vma || (vma->vm_flags & VM_IO) || !(flags & vma->vm_flags))
return NULL;
return follow_page(vma, start, write ? FOLL_WRITE : 0);
}
int kdb_getuserarea_size(void *to, unsigned long from, size_t size)
{
struct page *page;
void *vaddr;
page = kdb_get_one_user_page(lkdb_current_task, from, size, 0);
if (!page)
return size;
vaddr = kmap_atomic(page, KM_KDB);
memcpy(to, vaddr+ (from & (PAGE_SIZE - 1)), size);
kunmap_atomic(vaddr, KM_KDB);
return 0;
}
int kdb_putuserarea_size(unsigned long to, void *from, size_t size)
{
struct page *page;
void *vaddr;
page = kdb_get_one_user_page(lkdb_current_task, to, size, 1);
if (!page)
return size;
vaddr = kmap_atomic(page, KM_KDB);
memcpy(vaddr+ (to & (PAGE_SIZE - 1)), from, size);
kunmap_atomic(vaddr, KM_KDB);
return 0;
}
/* Last ditch allocator for debugging, so we can still debug even when the
* GFP_ATOMIC pool has been exhausted. The algorithms are tuned for space
* usage, not for speed. One smallish memory pool, the free chain is always in
* ascending address order to allow coalescing, allocations are done in brute
* force best fit.
*/
struct debug_alloc_header {
u32 next; /* offset of next header from start of pool */
u32 size;
void *caller;
};
/* The memory returned by this allocator must be aligned, which means so must
* the header size. Do not assume that sizeof(struct debug_alloc_header) is a
* multiple of the alignment, explicitly calculate the overhead of this header,
* including the alignment. The rest of this code must not use sizeof() on any
* header or pointer to a header.
*/
#define dah_align 8
#define dah_overhead ALIGN(sizeof(struct debug_alloc_header), dah_align)
static u64 debug_alloc_pool_aligned[256*1024/dah_align]; /* 256K pool */
static char *debug_alloc_pool = (char *)debug_alloc_pool_aligned;
static u32 dah_first, dah_first_call = 1, dah_used = 0, dah_used_max = 0;
/* Locking is awkward. The debug code is called from all contexts, including
* non maskable interrupts. A normal spinlock is not safe in NMI context. Try
* to get the debug allocator lock, if it cannot be obtained after a second
* then give up. If the lock could not be previously obtained on this cpu then
* only try once.
*
* sparse has no annotation for "this function _sometimes_ acquires a lock", so
* fudge the acquire/release notation.
*/
static DEFINE_SPINLOCK(dap_lock);
static int
get_dap_lock(void)
__acquires(dap_lock)
{
static int dap_locked = -1;
int count;
if (dap_locked == smp_processor_id())
count = 1;
else
count = 1000;
while (1) {
if (spin_trylock(&dap_lock)) {
dap_locked = -1;
return 1;
}
if (!count--)
break;
udelay(1000);
}
dap_locked = smp_processor_id();
__acquire(dap_lock);
return 0;
}
void
*ldebug_kmalloc(size_t size, gfp_t flags)
{
unsigned int rem, h_offset;
struct debug_alloc_header *best, *bestprev, *prev, *h;
void *p = NULL;
if (!get_dap_lock()) {
__release(dap_lock); /* we never actually got it */
return NULL;
}
h = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
if (dah_first_call) {
h->size = sizeof(debug_alloc_pool_aligned) - dah_overhead;
dah_first_call = 0;
}
size = ALIGN(size, dah_align);
prev = best = bestprev = NULL;
while (1) {
if (h->size >= size && (!best || h->size < best->size)) {
best = h;
bestprev = prev;
if (h->size == size)
break;
}
if (!h->next)
break;
prev = h;
h = (struct debug_alloc_header *)(debug_alloc_pool + h->next);
}
if (!best)
goto out;
rem = best->size - size;
/* The pool must always contain at least one header */
if (best->next == 0 && bestprev == NULL && rem < dah_overhead)
goto out;
if (rem >= dah_overhead) {
best->size = size;
h_offset = ((char *)best - debug_alloc_pool) +
dah_overhead + best->size;
h = (struct debug_alloc_header *)(debug_alloc_pool + h_offset);
h->size = rem - dah_overhead;
h->next = best->next;
} else
h_offset = best->next;
best->caller = __builtin_return_address(0);
dah_used += best->size;
dah_used_max = max(dah_used, dah_used_max);
if (bestprev)
bestprev->next = h_offset;
else
dah_first = h_offset;
p = (char *)best + dah_overhead;
memset(p, POISON_INUSE, best->size - 1);
*((char *)p + best->size - 1) = POISON_END;
out:
spin_unlock(&dap_lock);
return p;
}
void
ldebug_kfree(void *p)
{
struct debug_alloc_header *h;
unsigned int h_offset;
if (!p)
return;
if ((char *)p < debug_alloc_pool ||
(char *)p >= debug_alloc_pool + sizeof(debug_alloc_pool_aligned)) {
kfree(p);
return;
}
if (!get_dap_lock()) {
__release(dap_lock); /* we never actually got it */
return; /* memory leak, cannot be helped */
}
h = (struct debug_alloc_header *)((char *)p - dah_overhead);
memset(p, POISON_FREE, h->size - 1);
*((char *)p + h->size - 1) = POISON_END;
h->caller = NULL;
dah_used -= h->size;
h_offset = (char *)h - debug_alloc_pool;
if (h_offset < dah_first) {
h->next = dah_first;
dah_first = h_offset;
} else {
struct debug_alloc_header *prev;
unsigned int prev_offset;
prev = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
while (1) {
if (!prev->next || prev->next > h_offset)
break;
prev = (struct debug_alloc_header *)
(debug_alloc_pool + prev->next);
}
prev_offset = (char *)prev - debug_alloc_pool;
if (prev_offset + dah_overhead + prev->size == h_offset) {
prev->size += dah_overhead + h->size;
memset(h, POISON_FREE, dah_overhead - 1);
*((char *)h + dah_overhead - 1) = POISON_END;
h = prev;
h_offset = prev_offset;
} else {
h->next = prev->next;
prev->next = h_offset;
}
}
if (h_offset + dah_overhead + h->size == h->next) {
struct debug_alloc_header *next;
next = (struct debug_alloc_header *)
(debug_alloc_pool + h->next);
h->size += dah_overhead + next->size;
h->next = next->next;
memset(next, POISON_FREE, dah_overhead - 1);
*((char *)next + dah_overhead - 1) = POISON_END;
}
spin_unlock(&dap_lock);
}
void
ldebug_kusage(void)
{
struct debug_alloc_header *h_free, *h_used;
#ifdef CONFIG_IA64
/* FIXME: using dah for ia64 unwind always results in a memory leak.
* Fix that memory leak first, then set ldebug_kusage_one_time = 1 for
* all architectures.
*/
static int ldebug_kusage_one_time = 0;
#else
static int ldebug_kusage_one_time = 1;
#endif
if (!get_dap_lock()) {
__release(dap_lock); /* we never actually got it */
return;
}
h_free = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
if (dah_first == 0 &&
(h_free->size == sizeof(debug_alloc_pool_aligned) - dah_overhead ||
dah_first_call))
goto out;
if (!ldebug_kusage_one_time)
goto out;
ldebug_kusage_one_time = 0;
lkdb_printf("%s: ldebug_kmalloc memory leak dah_first %d\n",
__FUNCTION__, dah_first);
if (dah_first) {
h_used = (struct debug_alloc_header *)debug_alloc_pool;
lkdb_printf("%s: h_used %p size %d\n", __FUNCTION__, h_used, h_used->size);
}
do {
h_used = (struct debug_alloc_header *)
((char *)h_free + dah_overhead + h_free->size);
lkdb_printf("%s: h_used %p size %d caller %p\n",
__FUNCTION__, h_used, h_used->size, h_used->caller);
h_free = (struct debug_alloc_header *)
(debug_alloc_pool + h_free->next);
} while (h_free->next);
h_used = (struct debug_alloc_header *)
((char *)h_free + dah_overhead + h_free->size);
if ((char *)h_used - debug_alloc_pool !=
sizeof(debug_alloc_pool_aligned))
lkdb_printf("%s: h_used %p size %d caller %p\n",
__FUNCTION__, h_used, h_used->size, h_used->caller);
out:
spin_unlock(&dap_lock);
}
/* Maintain a small stack of lkdb_flags to allow recursion without disturbing
* the global kdb state.
*/
static int lkdb_flags_stack[4], lkdb_flags_index;
void
lkdb_save_flags(void)
{
BUG_ON(lkdb_flags_index >= ARRAY_SIZE(lkdb_flags_stack));
lkdb_flags_stack[lkdb_flags_index++] = lkdb_flags;
}
void
lkdb_restore_flags(void)
{
BUG_ON(lkdb_flags_index <= 0);
lkdb_flags = lkdb_flags_stack[--lkdb_flags_index];
}