kdb_support.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Kernel Debugger Architecture Independent Support Functions
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
  9. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  10. * 03/02/13 added new 2.5 kallsyms <xavier.bru@bull.net>
  11. */
  12. #include <stdarg.h>
  13. #include <linux/types.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/stddef.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/module.h>
  21. #include <linux/highmem.h>
  22. #include <linux/hardirq.h>
  23. #include <linux/delay.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/kdb.h>
  26. #include <linux/slab.h>
  27. #include "kdb_private.h"
  28. #ifdef CONFIG_MTK_EXTMEM
  29. #include <linux/exm_driver.h>
  30. #endif
  31. /*
  32. * kdbgetsymval - Return the address of the given symbol.
  33. *
  34. * Parameters:
  35. * symname Character string containing symbol name
  36. * symtab Structure to receive results
  37. * Returns:
  38. * 0 Symbol not found, symtab zero filled
  39. * 1 Symbol mapped to module/symbol/section, data in symtab
  40. */
  41. int kdbgetsymval(const char *symname, kdb_symtab_t *symtab)
  42. {
  43. if (KDB_DEBUG(AR))
  44. kdb_printf("kdbgetsymval: symname=%s, symtab=%p\n", symname,
  45. symtab);
  46. memset(symtab, 0, sizeof(*symtab));
  47. symtab->sym_start = kallsyms_lookup_name(symname);
  48. if (symtab->sym_start) {
  49. if (KDB_DEBUG(AR))
  50. kdb_printf("kdbgetsymval: returns 1, "
  51. "symtab->sym_start=0x%lx\n",
  52. symtab->sym_start);
  53. return 1;
  54. }
  55. if (KDB_DEBUG(AR))
  56. kdb_printf("kdbgetsymval: returns 0\n");
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(kdbgetsymval);
  60. static char *kdb_name_table[100]; /* arbitrary size */
  61. /*
  62. * kdbnearsym - Return the name of the symbol with the nearest address
  63. * less than 'addr'.
  64. *
  65. * Parameters:
  66. * addr Address to check for symbol near
  67. * symtab Structure to receive results
  68. * Returns:
  69. * 0 No sections contain this address, symtab zero filled
  70. * 1 Address mapped to module/symbol/section, data in symtab
  71. * Remarks:
  72. * 2.6 kallsyms has a "feature" where it unpacks the name into a
  73. * string. If that string is reused before the caller expects it
  74. * then the caller sees its string change without warning. To
  75. * avoid cluttering up the main kdb code with lots of kdb_strdup,
  76. * tests and kfree calls, kdbnearsym maintains an LRU list of the
  77. * last few unique strings. The list is sized large enough to
  78. * hold active strings, no kdb caller of kdbnearsym makes more
  79. * than ~20 later calls before using a saved value.
  80. */
  81. int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab)
  82. {
  83. int ret = 0;
  84. unsigned long symbolsize = 0;
  85. unsigned long offset = 0;
  86. #define knt1_size 128 /* must be >= kallsyms table size */
  87. char *knt1 = NULL;
  88. if (KDB_DEBUG(AR))
  89. kdb_printf("kdbnearsym: addr=0x%lx, symtab=%p\n", addr, symtab);
  90. memset(symtab, 0, sizeof(*symtab));
  91. if (addr < 4096)
  92. goto out;
  93. knt1 = debug_kmalloc(knt1_size, GFP_ATOMIC);
  94. if (!knt1) {
  95. kdb_printf("kdbnearsym: addr=0x%lx cannot kmalloc knt1\n",
  96. addr);
  97. goto out;
  98. }
  99. symtab->sym_name = kallsyms_lookup(addr, &symbolsize , &offset,
  100. (char **)(&symtab->mod_name), knt1);
  101. if (offset > 8*1024*1024) {
  102. symtab->sym_name = NULL;
  103. addr = offset = symbolsize = 0;
  104. }
  105. symtab->sym_start = addr - offset;
  106. symtab->sym_end = symtab->sym_start + symbolsize;
  107. ret = symtab->sym_name != NULL && *(symtab->sym_name) != '\0';
  108. if (ret) {
  109. int i;
  110. /* Another 2.6 kallsyms "feature". Sometimes the sym_name is
  111. * set but the buffer passed into kallsyms_lookup is not used,
  112. * so it contains garbage. The caller has to work out which
  113. * buffer needs to be saved.
  114. *
  115. * What was Rusty smoking when he wrote that code?
  116. */
  117. if (symtab->sym_name != knt1) {
  118. strncpy(knt1, symtab->sym_name, knt1_size);
  119. knt1[knt1_size-1] = '\0';
  120. }
  121. for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
  122. if (kdb_name_table[i] &&
  123. strcmp(kdb_name_table[i], knt1) == 0)
  124. break;
  125. }
  126. if (i >= ARRAY_SIZE(kdb_name_table)) {
  127. debug_kfree(kdb_name_table[0]);
  128. memcpy(kdb_name_table, kdb_name_table+1,
  129. sizeof(kdb_name_table[0]) *
  130. (ARRAY_SIZE(kdb_name_table)-1));
  131. } else {
  132. debug_kfree(knt1);
  133. knt1 = kdb_name_table[i];
  134. memcpy(kdb_name_table+i, kdb_name_table+i+1,
  135. sizeof(kdb_name_table[0]) *
  136. (ARRAY_SIZE(kdb_name_table)-i-1));
  137. }
  138. i = ARRAY_SIZE(kdb_name_table) - 1;
  139. kdb_name_table[i] = knt1;
  140. symtab->sym_name = kdb_name_table[i];
  141. knt1 = NULL;
  142. }
  143. if (symtab->mod_name == NULL)
  144. symtab->mod_name = "kernel";
  145. if (KDB_DEBUG(AR))
  146. kdb_printf("kdbnearsym: returns %d symtab->sym_start=0x%lx, "
  147. "symtab->mod_name=%p, symtab->sym_name=%p (%s)\n", ret,
  148. symtab->sym_start, symtab->mod_name, symtab->sym_name,
  149. symtab->sym_name);
  150. out:
  151. debug_kfree(knt1);
  152. return ret;
  153. }
  154. void kdbnearsym_cleanup(void)
  155. {
  156. int i;
  157. for (i = 0; i < ARRAY_SIZE(kdb_name_table); ++i) {
  158. if (kdb_name_table[i]) {
  159. debug_kfree(kdb_name_table[i]);
  160. kdb_name_table[i] = NULL;
  161. }
  162. }
  163. }
  164. static char ks_namebuf[KSYM_NAME_LEN+1], ks_namebuf_prev[KSYM_NAME_LEN+1];
  165. /*
  166. * kallsyms_symbol_complete
  167. *
  168. * Parameters:
  169. * prefix_name prefix of a symbol name to lookup
  170. * max_len maximum length that can be returned
  171. * Returns:
  172. * Number of symbols which match the given prefix.
  173. * Notes:
  174. * prefix_name is changed to contain the longest unique prefix that
  175. * starts with this prefix (tab completion).
  176. */
  177. int kallsyms_symbol_complete(char *prefix_name, int max_len)
  178. {
  179. loff_t pos = 0;
  180. int prefix_len = strlen(prefix_name), prev_len = 0;
  181. int i, number = 0;
  182. const char *name;
  183. while ((name = kdb_walk_kallsyms(&pos))) {
  184. if (strncmp(name, prefix_name, prefix_len) == 0) {
  185. strcpy(ks_namebuf, name);
  186. /* Work out the longest name that matches the prefix */
  187. if (++number == 1) {
  188. prev_len = min_t(int, max_len-1,
  189. strlen(ks_namebuf));
  190. memcpy(ks_namebuf_prev, ks_namebuf, prev_len);
  191. ks_namebuf_prev[prev_len] = '\0';
  192. continue;
  193. }
  194. for (i = 0; i < prev_len; i++) {
  195. if (ks_namebuf[i] != ks_namebuf_prev[i]) {
  196. prev_len = i;
  197. ks_namebuf_prev[i] = '\0';
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. if (prev_len > prefix_len)
  204. memcpy(prefix_name, ks_namebuf_prev, prev_len+1);
  205. return number;
  206. }
  207. /*
  208. * kallsyms_symbol_next
  209. *
  210. * Parameters:
  211. * prefix_name prefix of a symbol name to lookup
  212. * flag 0 means search from the head, 1 means continue search.
  213. * Returns:
  214. * 1 if a symbol matches the given prefix.
  215. * 0 if no string found
  216. */
  217. int kallsyms_symbol_next(char *prefix_name, int flag)
  218. {
  219. int prefix_len = strlen(prefix_name);
  220. static loff_t pos;
  221. const char *name;
  222. if (!flag)
  223. pos = 0;
  224. while ((name = kdb_walk_kallsyms(&pos))) {
  225. if (strncmp(name, prefix_name, prefix_len) == 0) {
  226. strncpy(prefix_name, name, strlen(name)+1);
  227. return 1;
  228. }
  229. }
  230. return 0;
  231. }
  232. /*
  233. * kdb_symbol_print - Standard method for printing a symbol name and offset.
  234. * Inputs:
  235. * addr Address to be printed.
  236. * symtab Address of symbol data, if NULL this routine does its
  237. * own lookup.
  238. * punc Punctuation for string, bit field.
  239. * Remarks:
  240. * The string and its punctuation is only printed if the address
  241. * is inside the kernel, except that the value is always printed
  242. * when requested.
  243. */
  244. void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
  245. unsigned int punc)
  246. {
  247. kdb_symtab_t symtab, *symtab_p2;
  248. if (symtab_p) {
  249. symtab_p2 = (kdb_symtab_t *)symtab_p;
  250. } else {
  251. symtab_p2 = &symtab;
  252. kdbnearsym(addr, symtab_p2);
  253. }
  254. if (!(symtab_p2->sym_name || (punc & KDB_SP_VALUE)))
  255. return;
  256. if (punc & KDB_SP_SPACEB)
  257. kdb_printf(" ");
  258. if (punc & KDB_SP_VALUE)
  259. kdb_printf(kdb_machreg_fmt0, addr);
  260. if (symtab_p2->sym_name) {
  261. if (punc & KDB_SP_VALUE)
  262. kdb_printf(" ");
  263. if (punc & KDB_SP_PAREN)
  264. kdb_printf("(");
  265. if (strcmp(symtab_p2->mod_name, "kernel"))
  266. kdb_printf("[%s]", symtab_p2->mod_name);
  267. kdb_printf("%s", symtab_p2->sym_name);
  268. if (addr != symtab_p2->sym_start)
  269. kdb_printf("+0x%lx", addr - symtab_p2->sym_start);
  270. if (punc & KDB_SP_SYMSIZE)
  271. kdb_printf("/0x%lx",
  272. symtab_p2->sym_end - symtab_p2->sym_start);
  273. if (punc & KDB_SP_PAREN)
  274. kdb_printf(")");
  275. }
  276. if (punc & KDB_SP_SPACEA)
  277. kdb_printf(" ");
  278. if (punc & KDB_SP_NEWLINE)
  279. kdb_printf("\n");
  280. }
  281. /*
  282. * kdb_strdup - kdb equivalent of strdup, for disasm code.
  283. * Inputs:
  284. * str The string to duplicate.
  285. * type Flags to kmalloc for the new string.
  286. * Returns:
  287. * Address of the new string, NULL if storage could not be allocated.
  288. * Remarks:
  289. * This is not in lib/string.c because it uses kmalloc which is not
  290. * available when string.o is used in boot loaders.
  291. */
  292. char *kdb_strdup(const char *str, gfp_t type)
  293. {
  294. int n = strlen(str)+1;
  295. char *s = kmalloc(n, type);
  296. if (!s)
  297. return NULL;
  298. return strcpy(s, str);
  299. }
  300. /*
  301. * kdb_getarea_size - Read an area of data. The kdb equivalent of
  302. * copy_from_user, with kdb messages for invalid addresses.
  303. * Inputs:
  304. * res Pointer to the area to receive the result.
  305. * addr Address of the area to copy.
  306. * size Size of the area.
  307. * Returns:
  308. * 0 for success, < 0 for error.
  309. */
  310. int kdb_getarea_size(void *res, unsigned long addr, size_t size)
  311. {
  312. int ret = probe_kernel_read((char *)res, (char *)addr, size);
  313. if (ret) {
  314. if (!KDB_STATE(SUPPRESS)) {
  315. kdb_printf("kdb_getarea: Bad address 0x%lx\n", addr);
  316. KDB_STATE_SET(SUPPRESS);
  317. }
  318. ret = KDB_BADADDR;
  319. } else {
  320. KDB_STATE_CLEAR(SUPPRESS);
  321. }
  322. return ret;
  323. }
  324. /*
  325. * kdb_putarea_size - Write an area of data. The kdb equivalent of
  326. * copy_to_user, with kdb messages for invalid addresses.
  327. * Inputs:
  328. * addr Address of the area to write to.
  329. * res Pointer to the area holding the data.
  330. * size Size of the area.
  331. * Returns:
  332. * 0 for success, < 0 for error.
  333. */
  334. int kdb_putarea_size(unsigned long addr, void *res, size_t size)
  335. {
  336. int ret = probe_kernel_read((char *)addr, (char *)res, size);
  337. if (ret) {
  338. if (!KDB_STATE(SUPPRESS)) {
  339. kdb_printf("kdb_putarea: Bad address 0x%lx\n", addr);
  340. KDB_STATE_SET(SUPPRESS);
  341. }
  342. ret = KDB_BADADDR;
  343. } else {
  344. KDB_STATE_CLEAR(SUPPRESS);
  345. }
  346. return ret;
  347. }
  348. /*
  349. * kdb_getphys - Read data from a physical address. Validate the
  350. * address is in range, use kmap_atomic() to get data
  351. * similar to kdb_getarea() - but for phys addresses
  352. * Inputs:
  353. * res Pointer to the word to receive the result
  354. * addr Physical address of the area to copy
  355. * size Size of the area
  356. * Returns:
  357. * 0 for success, < 0 for error.
  358. */
  359. static int kdb_getphys(void *res, unsigned long addr, size_t size)
  360. {
  361. unsigned long pfn;
  362. void *vaddr;
  363. struct page *page;
  364. pfn = (addr >> PAGE_SHIFT);
  365. if (!pfn_valid(pfn))
  366. return 1;
  367. page = pfn_to_page(pfn);
  368. vaddr = kmap_atomic(page);
  369. memcpy(res, vaddr + (addr & (PAGE_SIZE - 1)), size);
  370. kunmap_atomic(vaddr);
  371. return 0;
  372. }
  373. /*
  374. * kdb_getphysword
  375. * Inputs:
  376. * word Pointer to the word to receive the result.
  377. * addr Address of the area to copy.
  378. * size Size of the area.
  379. * Returns:
  380. * 0 for success, < 0 for error.
  381. */
  382. int kdb_getphysword(unsigned long *word, unsigned long addr, size_t size)
  383. {
  384. int diag;
  385. __u8 w1;
  386. __u16 w2;
  387. __u32 w4;
  388. __u64 w8;
  389. *word = 0; /* Default value if addr or size is invalid */
  390. switch (size) {
  391. case 1:
  392. diag = kdb_getphys(&w1, addr, sizeof(w1));
  393. if (!diag)
  394. *word = w1;
  395. break;
  396. case 2:
  397. diag = kdb_getphys(&w2, addr, sizeof(w2));
  398. if (!diag)
  399. *word = w2;
  400. break;
  401. case 4:
  402. diag = kdb_getphys(&w4, addr, sizeof(w4));
  403. if (!diag)
  404. *word = w4;
  405. break;
  406. case 8:
  407. if (size <= sizeof(*word)) {
  408. diag = kdb_getphys(&w8, addr, sizeof(w8));
  409. if (!diag)
  410. *word = w8;
  411. break;
  412. }
  413. /* drop through */
  414. default:
  415. diag = KDB_BADWIDTH;
  416. kdb_printf("kdb_getphysword: bad width %ld\n", (long) size);
  417. }
  418. return diag;
  419. }
  420. /*
  421. * kdb_getword - Read a binary value. Unlike kdb_getarea, this treats
  422. * data as numbers.
  423. * Inputs:
  424. * word Pointer to the word to receive the result.
  425. * addr Address of the area to copy.
  426. * size Size of the area.
  427. * Returns:
  428. * 0 for success, < 0 for error.
  429. */
  430. int kdb_getword(unsigned long *word, unsigned long addr, size_t size)
  431. {
  432. int diag;
  433. __u8 w1;
  434. __u16 w2;
  435. __u32 w4;
  436. __u64 w8;
  437. *word = 0; /* Default value if addr or size is invalid */
  438. switch (size) {
  439. case 1:
  440. diag = kdb_getarea(w1, addr);
  441. if (!diag)
  442. *word = w1;
  443. break;
  444. case 2:
  445. diag = kdb_getarea(w2, addr);
  446. if (!diag)
  447. *word = w2;
  448. break;
  449. case 4:
  450. diag = kdb_getarea(w4, addr);
  451. if (!diag)
  452. *word = w4;
  453. break;
  454. case 8:
  455. if (size <= sizeof(*word)) {
  456. diag = kdb_getarea(w8, addr);
  457. if (!diag)
  458. *word = w8;
  459. break;
  460. }
  461. /* drop through */
  462. default:
  463. diag = KDB_BADWIDTH;
  464. kdb_printf("kdb_getword: bad width %ld\n", (long) size);
  465. }
  466. return diag;
  467. }
  468. /*
  469. * kdb_putword - Write a binary value. Unlike kdb_putarea, this
  470. * treats data as numbers.
  471. * Inputs:
  472. * addr Address of the area to write to..
  473. * word The value to set.
  474. * size Size of the area.
  475. * Returns:
  476. * 0 for success, < 0 for error.
  477. */
  478. int kdb_putword(unsigned long addr, unsigned long word, size_t size)
  479. {
  480. int diag;
  481. __u8 w1;
  482. __u16 w2;
  483. __u32 w4;
  484. __u64 w8;
  485. switch (size) {
  486. case 1:
  487. w1 = word;
  488. diag = kdb_putarea(addr, w1);
  489. break;
  490. case 2:
  491. w2 = word;
  492. diag = kdb_putarea(addr, w2);
  493. break;
  494. case 4:
  495. w4 = word;
  496. diag = kdb_putarea(addr, w4);
  497. break;
  498. case 8:
  499. if (size <= sizeof(word)) {
  500. w8 = word;
  501. diag = kdb_putarea(addr, w8);
  502. break;
  503. }
  504. /* drop through */
  505. default:
  506. diag = KDB_BADWIDTH;
  507. kdb_printf("kdb_putword: bad width %ld\n", (long) size);
  508. }
  509. return diag;
  510. }
  511. /*
  512. * kdb_task_state_string - Convert a string containing any of the
  513. * letters DRSTCZEUIMA to a mask for the process state field and
  514. * return the value. If no argument is supplied, return the mask
  515. * that corresponds to environment variable PS, DRSTCZEU by
  516. * default.
  517. * Inputs:
  518. * s String to convert
  519. * Returns:
  520. * Mask for process state.
  521. * Notes:
  522. * The mask folds data from several sources into a single long value, so
  523. * be careful not to overlap the bits. TASK_* bits are in the LSB,
  524. * special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there
  525. * is no overlap between TASK_* and EXIT_* but that may not always be
  526. * true, so EXIT_* bits are shifted left 16 bits before being stored in
  527. * the mask.
  528. */
  529. /* unrunnable is < 0 */
  530. #define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1))
  531. #define RUNNING (1UL << (8*sizeof(unsigned long) - 2))
  532. #define IDLE (1UL << (8*sizeof(unsigned long) - 3))
  533. #define DAEMON (1UL << (8*sizeof(unsigned long) - 4))
  534. unsigned long kdb_task_state_string(const char *s)
  535. {
  536. long res = 0;
  537. if (!s) {
  538. s = kdbgetenv("PS");
  539. if (!s)
  540. s = "DRSTCZEU"; /* default value for ps */
  541. }
  542. while (*s) {
  543. switch (*s) {
  544. case 'D':
  545. res |= TASK_UNINTERRUPTIBLE;
  546. break;
  547. case 'R':
  548. res |= RUNNING;
  549. break;
  550. case 'S':
  551. res |= TASK_INTERRUPTIBLE;
  552. break;
  553. case 'T':
  554. res |= TASK_STOPPED;
  555. break;
  556. case 'C':
  557. res |= TASK_TRACED;
  558. break;
  559. case 'Z':
  560. res |= EXIT_ZOMBIE << 16;
  561. break;
  562. case 'E':
  563. res |= EXIT_DEAD << 16;
  564. break;
  565. case 'U':
  566. res |= UNRUNNABLE;
  567. break;
  568. case 'I':
  569. res |= IDLE;
  570. break;
  571. case 'M':
  572. res |= DAEMON;
  573. break;
  574. case 'A':
  575. res = ~0UL;
  576. break;
  577. default:
  578. kdb_printf("%s: unknown flag '%c' ignored\n",
  579. __func__, *s);
  580. break;
  581. }
  582. ++s;
  583. }
  584. return res;
  585. }
  586. /*
  587. * kdb_task_state_char - Return the character that represents the task state.
  588. * Inputs:
  589. * p struct task for the process
  590. * Returns:
  591. * One character to represent the task state.
  592. */
  593. char kdb_task_state_char (const struct task_struct *p)
  594. {
  595. int cpu;
  596. char state;
  597. unsigned long tmp;
  598. if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long)))
  599. return 'E';
  600. cpu = kdb_process_cpu(p);
  601. state = (p->state == 0) ? 'R' :
  602. (p->state < 0) ? 'U' :
  603. (p->state & TASK_UNINTERRUPTIBLE) ? 'D' :
  604. (p->state & TASK_STOPPED) ? 'T' :
  605. (p->state & TASK_TRACED) ? 'C' :
  606. (p->exit_state & EXIT_ZOMBIE) ? 'Z' :
  607. (p->exit_state & EXIT_DEAD) ? 'E' :
  608. (p->state & TASK_INTERRUPTIBLE) ? 'S' : '?';
  609. if (is_idle_task(p)) {
  610. /* Idle task. Is it really idle, apart from the kdb
  611. * interrupt? */
  612. if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {
  613. if (cpu != kdb_initial_cpu)
  614. state = 'I'; /* idle task */
  615. }
  616. } else if (!p->mm && state == 'S') {
  617. state = 'M'; /* sleeping system daemon */
  618. }
  619. return state;
  620. }
  621. /*
  622. * kdb_task_state - Return true if a process has the desired state
  623. * given by the mask.
  624. * Inputs:
  625. * p struct task for the process
  626. * mask mask from kdb_task_state_string to select processes
  627. * Returns:
  628. * True if the process matches at least one criteria defined by the mask.
  629. */
  630. unsigned long kdb_task_state(const struct task_struct *p, unsigned long mask)
  631. {
  632. char state[] = { kdb_task_state_char(p), '\0' };
  633. return (mask & kdb_task_state_string(state)) != 0;
  634. }
  635. /*
  636. * kdb_print_nameval - Print a name and its value, converting the
  637. * value to a symbol lookup if possible.
  638. * Inputs:
  639. * name field name to print
  640. * val value of field
  641. */
  642. void kdb_print_nameval(const char *name, unsigned long val)
  643. {
  644. kdb_symtab_t symtab;
  645. kdb_printf(" %-11.11s ", name);
  646. if (kdbnearsym(val, &symtab))
  647. kdb_symbol_print(val, &symtab,
  648. KDB_SP_VALUE|KDB_SP_SYMSIZE|KDB_SP_NEWLINE);
  649. else
  650. kdb_printf("0x%lx\n", val);
  651. }
  652. /* Last ditch allocator for debugging, so we can still debug even when
  653. * the GFP_ATOMIC pool has been exhausted. The algorithms are tuned
  654. * for space usage, not for speed. One smallish memory pool, the free
  655. * chain is always in ascending address order to allow coalescing,
  656. * allocations are done in brute force best fit.
  657. */
  658. struct debug_alloc_header {
  659. u32 next; /* offset of next header from start of pool */
  660. u32 size;
  661. void *caller;
  662. };
  663. /* The memory returned by this allocator must be aligned, which means
  664. * so must the header size. Do not assume that sizeof(struct
  665. * debug_alloc_header) is a multiple of the alignment, explicitly
  666. * calculate the overhead of this header, including the alignment.
  667. * The rest of this code must not use sizeof() on any header or
  668. * pointer to a header.
  669. */
  670. #define dah_align 8
  671. #define dah_overhead ALIGN(sizeof(struct debug_alloc_header), dah_align)
  672. #ifdef CONFIG_MTK_EXTMEM
  673. #define SIZEOF_DEBUG_ALLOC_POOL_ALIGNED (sizeof(u64) * 256 * 1024/dah_align)
  674. static u64 *debug_alloc_pool_aligned;
  675. static char *debug_alloc_pool;
  676. void init_debug_alloc_pool_aligned(void)
  677. {
  678. debug_alloc_pool_aligned =
  679. extmem_malloc_page_align(SIZEOF_DEBUG_ALLOC_POOL_ALIGNED);
  680. if (debug_alloc_pool_aligned == NULL) {
  681. pr_err("%s[%s] ext memory alloc failed!!!\n", __FILE__, __func__);
  682. debug_alloc_pool = vmalloc(SIZEOF_DEBUG_ALLOC_POOL_ALIGNED);
  683. } else {
  684. debug_alloc_pool = (char *)debug_alloc_pool_aligned;
  685. }
  686. }
  687. EXPORT_SYMBOL(init_debug_alloc_pool_aligned);
  688. #else
  689. static u64 debug_alloc_pool_aligned[256*1024/dah_align]; /* 256K pool */
  690. static char *debug_alloc_pool = (char *)debug_alloc_pool_aligned;
  691. #endif
  692. static u32 dah_first, dah_first_call = 1, dah_used, dah_used_max;
  693. /* Locking is awkward. The debug code is called from all contexts,
  694. * including non maskable interrupts. A normal spinlock is not safe
  695. * in NMI context. Try to get the debug allocator lock, if it cannot
  696. * be obtained after a second then give up. If the lock could not be
  697. * previously obtained on this cpu then only try once.
  698. *
  699. * sparse has no annotation for "this function _sometimes_ acquires a
  700. * lock", so fudge the acquire/release notation.
  701. */
  702. static DEFINE_SPINLOCK(dap_lock);
  703. static int get_dap_lock(void)
  704. __acquires(dap_lock)
  705. {
  706. static int dap_locked = -1;
  707. int count;
  708. if (dap_locked == smp_processor_id())
  709. count = 1;
  710. else
  711. count = 1000;
  712. while (1) {
  713. if (spin_trylock(&dap_lock)) {
  714. dap_locked = -1;
  715. return 1;
  716. }
  717. if (!count--)
  718. break;
  719. udelay(1000);
  720. }
  721. dap_locked = smp_processor_id();
  722. __acquire(dap_lock);
  723. return 0;
  724. }
  725. void *debug_kmalloc(size_t size, gfp_t flags)
  726. {
  727. unsigned int rem, h_offset;
  728. struct debug_alloc_header *best, *bestprev, *prev, *h;
  729. void *p = NULL;
  730. if (!get_dap_lock()) {
  731. __release(dap_lock); /* we never actually got it */
  732. return NULL;
  733. }
  734. h = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
  735. if (dah_first_call) {
  736. #ifdef CONFIG_MTK_EXTMEM
  737. h->size = SIZEOF_DEBUG_ALLOC_POOL_ALIGNED - dah_overhead;
  738. #else
  739. h->size = sizeof(debug_alloc_pool_aligned) - dah_overhead;
  740. #endif
  741. dah_first_call = 0;
  742. }
  743. size = ALIGN(size, dah_align);
  744. prev = best = bestprev = NULL;
  745. while (1) {
  746. if (h->size >= size && (!best || h->size < best->size)) {
  747. best = h;
  748. bestprev = prev;
  749. if (h->size == size)
  750. break;
  751. }
  752. if (!h->next)
  753. break;
  754. prev = h;
  755. h = (struct debug_alloc_header *)(debug_alloc_pool + h->next);
  756. }
  757. if (!best)
  758. goto out;
  759. rem = best->size - size;
  760. /* The pool must always contain at least one header */
  761. if (best->next == 0 && bestprev == NULL && rem < dah_overhead)
  762. goto out;
  763. if (rem >= dah_overhead) {
  764. best->size = size;
  765. h_offset = ((char *)best - debug_alloc_pool) +
  766. dah_overhead + best->size;
  767. h = (struct debug_alloc_header *)(debug_alloc_pool + h_offset);
  768. h->size = rem - dah_overhead;
  769. h->next = best->next;
  770. } else
  771. h_offset = best->next;
  772. best->caller = __builtin_return_address(0);
  773. dah_used += best->size;
  774. dah_used_max = max(dah_used, dah_used_max);
  775. if (bestprev)
  776. bestprev->next = h_offset;
  777. else
  778. dah_first = h_offset;
  779. p = (char *)best + dah_overhead;
  780. memset(p, POISON_INUSE, best->size - 1);
  781. *((char *)p + best->size - 1) = POISON_END;
  782. out:
  783. spin_unlock(&dap_lock);
  784. return p;
  785. }
  786. void debug_kfree(void *p)
  787. {
  788. struct debug_alloc_header *h;
  789. unsigned int h_offset;
  790. if (!p)
  791. return;
  792. if ((char *)p < debug_alloc_pool ||
  793. #ifdef CONFIG_MTK_EXTMEM
  794. (char *)p >= debug_alloc_pool + SIZEOF_DEBUG_ALLOC_POOL_ALIGNED) {
  795. #else
  796. (char *)p >= debug_alloc_pool + sizeof(debug_alloc_pool_aligned)) {
  797. #endif
  798. kfree(p);
  799. return;
  800. }
  801. if (!get_dap_lock()) {
  802. __release(dap_lock); /* we never actually got it */
  803. return; /* memory leak, cannot be helped */
  804. }
  805. h = (struct debug_alloc_header *)((char *)p - dah_overhead);
  806. memset(p, POISON_FREE, h->size - 1);
  807. *((char *)p + h->size - 1) = POISON_END;
  808. h->caller = NULL;
  809. dah_used -= h->size;
  810. h_offset = (char *)h - debug_alloc_pool;
  811. if (h_offset < dah_first) {
  812. h->next = dah_first;
  813. dah_first = h_offset;
  814. } else {
  815. struct debug_alloc_header *prev;
  816. unsigned int prev_offset;
  817. prev = (struct debug_alloc_header *)(debug_alloc_pool +
  818. dah_first);
  819. while (1) {
  820. if (!prev->next || prev->next > h_offset)
  821. break;
  822. prev = (struct debug_alloc_header *)
  823. (debug_alloc_pool + prev->next);
  824. }
  825. prev_offset = (char *)prev - debug_alloc_pool;
  826. if (prev_offset + dah_overhead + prev->size == h_offset) {
  827. prev->size += dah_overhead + h->size;
  828. memset(h, POISON_FREE, dah_overhead - 1);
  829. *((char *)h + dah_overhead - 1) = POISON_END;
  830. h = prev;
  831. h_offset = prev_offset;
  832. } else {
  833. h->next = prev->next;
  834. prev->next = h_offset;
  835. }
  836. }
  837. if (h_offset + dah_overhead + h->size == h->next) {
  838. struct debug_alloc_header *next;
  839. next = (struct debug_alloc_header *)
  840. (debug_alloc_pool + h->next);
  841. h->size += dah_overhead + next->size;
  842. h->next = next->next;
  843. memset(next, POISON_FREE, dah_overhead - 1);
  844. *((char *)next + dah_overhead - 1) = POISON_END;
  845. }
  846. spin_unlock(&dap_lock);
  847. }
  848. void debug_kusage(void)
  849. {
  850. struct debug_alloc_header *h_free, *h_used;
  851. #ifdef CONFIG_IA64
  852. /* FIXME: using dah for ia64 unwind always results in a memory leak.
  853. * Fix that memory leak first, then set debug_kusage_one_time = 1 for
  854. * all architectures.
  855. */
  856. static int debug_kusage_one_time;
  857. #else
  858. static int debug_kusage_one_time = 1;
  859. #endif
  860. if (!get_dap_lock()) {
  861. __release(dap_lock); /* we never actually got it */
  862. return;
  863. }
  864. h_free = (struct debug_alloc_header *)(debug_alloc_pool + dah_first);
  865. #ifdef CONFIG_MTK_EXTMEM
  866. if (dah_first == 0 &&
  867. (h_free->size == SIZEOF_DEBUG_ALLOC_POOL_ALIGNED - dah_overhead ||
  868. dah_first_call))
  869. #else
  870. if (dah_first == 0 &&
  871. (h_free->size == sizeof(debug_alloc_pool_aligned) - dah_overhead ||
  872. dah_first_call))
  873. #endif
  874. goto out;
  875. if (!debug_kusage_one_time)
  876. goto out;
  877. debug_kusage_one_time = 0;
  878. kdb_printf("%s: debug_kmalloc memory leak dah_first %d\n",
  879. __func__, dah_first);
  880. if (dah_first) {
  881. h_used = (struct debug_alloc_header *)debug_alloc_pool;
  882. kdb_printf("%s: h_used %p size %d\n", __func__, h_used,
  883. h_used->size);
  884. }
  885. do {
  886. h_used = (struct debug_alloc_header *)
  887. ((char *)h_free + dah_overhead + h_free->size);
  888. kdb_printf("%s: h_used %p size %d caller %p\n",
  889. __func__, h_used, h_used->size, h_used->caller);
  890. h_free = (struct debug_alloc_header *)
  891. (debug_alloc_pool + h_free->next);
  892. } while (h_free->next);
  893. h_used = (struct debug_alloc_header *)
  894. ((char *)h_free + dah_overhead + h_free->size);
  895. if ((char *)h_used - debug_alloc_pool !=
  896. #ifdef CONFIG_MTK_EXTMEM
  897. SIZEOF_DEBUG_ALLOC_POOL_ALIGNED)
  898. #else
  899. sizeof(debug_alloc_pool_aligned))
  900. #endif
  901. kdb_printf("%s: h_used %p size %d caller %p\n",
  902. __func__, h_used, h_used->size, h_used->caller);
  903. out:
  904. spin_unlock(&dap_lock);
  905. }
  906. /* Maintain a small stack of kdb_flags to allow recursion without disturbing
  907. * the global kdb state.
  908. */
  909. static int kdb_flags_stack[4], kdb_flags_index;
  910. void kdb_save_flags(void)
  911. {
  912. BUG_ON(kdb_flags_index >= ARRAY_SIZE(kdb_flags_stack));
  913. kdb_flags_stack[kdb_flags_index++] = kdb_flags;
  914. }
  915. void kdb_restore_flags(void)
  916. {
  917. BUG_ON(kdb_flags_index <= 0);
  918. kdb_flags = kdb_flags_stack[--kdb_flags_index];
  919. }