unwind.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (C) 2002-2006 Novell, Inc.
  3. * Jan Beulich <jbeulich@novell.com>
  4. * This code is released under version 2 of the GNU GPL.
  5. *
  6. * A simple API for unwinding kernel stacks. This is used for
  7. * debugging and error reporting purposes. The kernel doesn't need
  8. * full-blown stack unwinding with all the bells and whistles, so there
  9. * is not much point in implementing the full Dwarf2 unwind API.
  10. */
  11. #include <linux/unwind.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/stop_machine.h>
  15. #include <asm/sections.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/unaligned.h>
  18. extern char __start_unwind[], __end_unwind[];
  19. #define MAX_STACK_DEPTH 8
  20. #define EXTRA_INFO(f) { \
  21. BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \
  22. % FIELD_SIZEOF(struct unwind_frame_info, f)) \
  23. + offsetof(struct unwind_frame_info, f) \
  24. / FIELD_SIZEOF(struct unwind_frame_info, f), \
  25. FIELD_SIZEOF(struct unwind_frame_info, f) \
  26. }
  27. #define PTREGS_INFO(f) EXTRA_INFO(regs.f)
  28. static const struct {
  29. unsigned offs:BITS_PER_LONG / 2;
  30. unsigned width:BITS_PER_LONG / 2;
  31. } reg_info[] = {
  32. UNW_REGISTER_INFO
  33. };
  34. #undef PTREGS_INFO
  35. #undef EXTRA_INFO
  36. #ifndef REG_INVALID
  37. #define REG_INVALID(r) (reg_info[r].width == 0)
  38. #endif
  39. #define DW_CFA_nop 0x00
  40. #define DW_CFA_set_loc 0x01
  41. #define DW_CFA_advance_loc1 0x02
  42. #define DW_CFA_advance_loc2 0x03
  43. #define DW_CFA_advance_loc4 0x04
  44. #define DW_CFA_offset_extended 0x05
  45. #define DW_CFA_restore_extended 0x06
  46. #define DW_CFA_undefined 0x07
  47. #define DW_CFA_same_value 0x08
  48. #define DW_CFA_register 0x09
  49. #define DW_CFA_remember_state 0x0a
  50. #define DW_CFA_restore_state 0x0b
  51. #define DW_CFA_def_cfa 0x0c
  52. #define DW_CFA_def_cfa_register 0x0d
  53. #define DW_CFA_def_cfa_offset 0x0e
  54. #define DW_CFA_def_cfa_expression 0x0f
  55. #define DW_CFA_expression 0x10
  56. #define DW_CFA_offset_extended_sf 0x11
  57. #define DW_CFA_def_cfa_sf 0x12
  58. #define DW_CFA_def_cfa_offset_sf 0x13
  59. #define DW_CFA_val_offset 0x14
  60. #define DW_CFA_val_offset_sf 0x15
  61. #define DW_CFA_val_expression 0x16
  62. #define DW_CFA_lo_user 0x1c
  63. #define DW_CFA_GNU_window_save 0x2d
  64. #define DW_CFA_GNU_args_size 0x2e
  65. #define DW_CFA_GNU_negative_offset_extended 0x2f
  66. #define DW_CFA_hi_user 0x3f
  67. #define DW_EH_PE_FORM 0x07
  68. #define DW_EH_PE_native 0x00
  69. #define DW_EH_PE_leb128 0x01
  70. #define DW_EH_PE_data2 0x02
  71. #define DW_EH_PE_data4 0x03
  72. #define DW_EH_PE_data8 0x04
  73. #define DW_EH_PE_signed 0x08
  74. #define DW_EH_PE_ADJUST 0x70
  75. #define DW_EH_PE_abs 0x00
  76. #define DW_EH_PE_pcrel 0x10
  77. #define DW_EH_PE_textrel 0x20
  78. #define DW_EH_PE_datarel 0x30
  79. #define DW_EH_PE_funcrel 0x40
  80. #define DW_EH_PE_aligned 0x50
  81. #define DW_EH_PE_indirect 0x80
  82. #define DW_EH_PE_omit 0xff
  83. typedef unsigned long uleb128_t;
  84. typedef signed long sleb128_t;
  85. static struct unwind_table {
  86. struct {
  87. unsigned long pc;
  88. unsigned long range;
  89. } core, init;
  90. const void *address;
  91. unsigned long size;
  92. struct unwind_table *link;
  93. const char *name;
  94. } root_table, *last_table;
  95. struct unwind_item {
  96. enum item_location {
  97. Nowhere,
  98. Memory,
  99. Register,
  100. Value
  101. } where;
  102. uleb128_t value;
  103. };
  104. struct unwind_state {
  105. uleb128_t loc, org;
  106. const u8 *cieStart, *cieEnd;
  107. uleb128_t codeAlign;
  108. sleb128_t dataAlign;
  109. struct cfa {
  110. uleb128_t reg, offs;
  111. } cfa;
  112. struct unwind_item regs[ARRAY_SIZE(reg_info)];
  113. unsigned stackDepth:8;
  114. unsigned version:8;
  115. const u8 *label;
  116. const u8 *stack[MAX_STACK_DEPTH];
  117. };
  118. static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
  119. static struct unwind_table *find_table(unsigned long pc)
  120. {
  121. struct unwind_table *table;
  122. for (table = &root_table; table; table = table->link)
  123. if ((pc >= table->core.pc
  124. && pc < table->core.pc + table->core.range)
  125. || (pc >= table->init.pc
  126. && pc < table->init.pc + table->init.range))
  127. break;
  128. return table;
  129. }
  130. static void init_unwind_table(struct unwind_table *table,
  131. const char *name,
  132. const void *core_start,
  133. unsigned long core_size,
  134. const void *init_start,
  135. unsigned long init_size,
  136. const void *table_start,
  137. unsigned long table_size)
  138. {
  139. table->core.pc = (unsigned long)core_start;
  140. table->core.range = core_size;
  141. table->init.pc = (unsigned long)init_start;
  142. table->init.range = init_size;
  143. table->address = table_start;
  144. table->size = table_size;
  145. table->link = NULL;
  146. table->name = name;
  147. }
  148. void __init unwind_init(void)
  149. {
  150. init_unwind_table(&root_table, "kernel",
  151. _text, _end - _text,
  152. NULL, 0,
  153. __start_unwind, __end_unwind - __start_unwind);
  154. }
  155. #ifdef CONFIG_MODULES
  156. /* Must be called with module_mutex held. */
  157. void *unwind_add_table(struct module *module,
  158. const void *table_start,
  159. unsigned long table_size)
  160. {
  161. struct unwind_table *table;
  162. if (table_size <= 0)
  163. return NULL;
  164. table = kmalloc(sizeof(*table), GFP_KERNEL);
  165. if (!table)
  166. return NULL;
  167. init_unwind_table(table, module->name,
  168. module->module_core, module->core_size,
  169. module->module_init, module->init_size,
  170. table_start, table_size);
  171. if (last_table)
  172. last_table->link = table;
  173. else
  174. root_table.link = table;
  175. last_table = table;
  176. return table;
  177. }
  178. struct unlink_table_info
  179. {
  180. struct unwind_table *table;
  181. int init_only;
  182. };
  183. static int unlink_table(void *arg)
  184. {
  185. struct unlink_table_info *info = arg;
  186. struct unwind_table *table = info->table, *prev;
  187. for (prev = &root_table; prev->link && prev->link != table; prev = prev->link)
  188. ;
  189. if (prev->link) {
  190. if (info->init_only) {
  191. table->init.pc = 0;
  192. table->init.range = 0;
  193. info->table = NULL;
  194. } else {
  195. prev->link = table->link;
  196. if (!prev->link)
  197. last_table = prev;
  198. }
  199. } else
  200. info->table = NULL;
  201. return 0;
  202. }
  203. /* Must be called with module_mutex held. */
  204. void unwind_remove_table(void *handle, int init_only)
  205. {
  206. struct unwind_table *table = handle;
  207. struct unlink_table_info info;
  208. if (!table || table == &root_table)
  209. return;
  210. if (init_only && table == last_table) {
  211. table->init.pc = 0;
  212. table->init.range = 0;
  213. return;
  214. }
  215. info.table = table;
  216. info.init_only = init_only;
  217. stop_machine_run(unlink_table, &info, NR_CPUS);
  218. if (info.table)
  219. kfree(table);
  220. }
  221. #endif /* CONFIG_MODULES */
  222. static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
  223. {
  224. const u8 *cur = *pcur;
  225. uleb128_t value;
  226. unsigned shift;
  227. for (shift = 0, value = 0; cur < end; shift += 7) {
  228. if (shift + 7 > 8 * sizeof(value)
  229. && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
  230. cur = end + 1;
  231. break;
  232. }
  233. value |= (uleb128_t)(*cur & 0x7f) << shift;
  234. if (!(*cur++ & 0x80))
  235. break;
  236. }
  237. *pcur = cur;
  238. return value;
  239. }
  240. static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
  241. {
  242. const u8 *cur = *pcur;
  243. sleb128_t value;
  244. unsigned shift;
  245. for (shift = 0, value = 0; cur < end; shift += 7) {
  246. if (shift + 7 > 8 * sizeof(value)
  247. && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
  248. cur = end + 1;
  249. break;
  250. }
  251. value |= (sleb128_t)(*cur & 0x7f) << shift;
  252. if (!(*cur & 0x80)) {
  253. value |= -(*cur++ & 0x40) << shift;
  254. break;
  255. }
  256. }
  257. *pcur = cur;
  258. return value;
  259. }
  260. static unsigned long read_pointer(const u8 **pLoc,
  261. const void *end,
  262. signed ptrType)
  263. {
  264. unsigned long value = 0;
  265. union {
  266. const u8 *p8;
  267. const u16 *p16u;
  268. const s16 *p16s;
  269. const u32 *p32u;
  270. const s32 *p32s;
  271. const unsigned long *pul;
  272. } ptr;
  273. if (ptrType < 0 || ptrType == DW_EH_PE_omit)
  274. return 0;
  275. ptr.p8 = *pLoc;
  276. switch(ptrType & DW_EH_PE_FORM) {
  277. case DW_EH_PE_data2:
  278. if (end < (const void *)(ptr.p16u + 1))
  279. return 0;
  280. if(ptrType & DW_EH_PE_signed)
  281. value = get_unaligned(ptr.p16s++);
  282. else
  283. value = get_unaligned(ptr.p16u++);
  284. break;
  285. case DW_EH_PE_data4:
  286. #ifdef CONFIG_64BIT
  287. if (end < (const void *)(ptr.p32u + 1))
  288. return 0;
  289. if(ptrType & DW_EH_PE_signed)
  290. value = get_unaligned(ptr.p32s++);
  291. else
  292. value = get_unaligned(ptr.p32u++);
  293. break;
  294. case DW_EH_PE_data8:
  295. BUILD_BUG_ON(sizeof(u64) != sizeof(value));
  296. #else
  297. BUILD_BUG_ON(sizeof(u32) != sizeof(value));
  298. #endif
  299. case DW_EH_PE_native:
  300. if (end < (const void *)(ptr.pul + 1))
  301. return 0;
  302. value = get_unaligned(ptr.pul++);
  303. break;
  304. case DW_EH_PE_leb128:
  305. BUILD_BUG_ON(sizeof(uleb128_t) > sizeof(value));
  306. value = ptrType & DW_EH_PE_signed
  307. ? get_sleb128(&ptr.p8, end)
  308. : get_uleb128(&ptr.p8, end);
  309. if ((const void *)ptr.p8 > end)
  310. return 0;
  311. break;
  312. default:
  313. return 0;
  314. }
  315. switch(ptrType & DW_EH_PE_ADJUST) {
  316. case DW_EH_PE_abs:
  317. break;
  318. case DW_EH_PE_pcrel:
  319. value += (unsigned long)*pLoc;
  320. break;
  321. default:
  322. return 0;
  323. }
  324. if ((ptrType & DW_EH_PE_indirect)
  325. && __get_user(value, (unsigned long *)value))
  326. return 0;
  327. *pLoc = ptr.p8;
  328. return value;
  329. }
  330. static signed fde_pointer_type(const u32 *cie)
  331. {
  332. const u8 *ptr = (const u8 *)(cie + 2);
  333. unsigned version = *ptr;
  334. if (version != 1)
  335. return -1; /* unsupported */
  336. if (*++ptr) {
  337. const char *aug;
  338. const u8 *end = (const u8 *)(cie + 1) + *cie;
  339. uleb128_t len;
  340. /* check if augmentation size is first (and thus present) */
  341. if (*ptr != 'z')
  342. return -1;
  343. /* check if augmentation string is nul-terminated */
  344. if ((ptr = memchr(aug = (const void *)ptr, 0, end - ptr)) == NULL)
  345. return -1;
  346. ++ptr; /* skip terminator */
  347. get_uleb128(&ptr, end); /* skip code alignment */
  348. get_sleb128(&ptr, end); /* skip data alignment */
  349. /* skip return address column */
  350. version <= 1 ? (void)++ptr : (void)get_uleb128(&ptr, end);
  351. len = get_uleb128(&ptr, end); /* augmentation length */
  352. if (ptr + len < ptr || ptr + len > end)
  353. return -1;
  354. end = ptr + len;
  355. while (*++aug) {
  356. if (ptr >= end)
  357. return -1;
  358. switch(*aug) {
  359. case 'L':
  360. ++ptr;
  361. break;
  362. case 'P': {
  363. signed ptrType = *ptr++;
  364. if (!read_pointer(&ptr, end, ptrType) || ptr > end)
  365. return -1;
  366. }
  367. break;
  368. case 'R':
  369. return *ptr;
  370. default:
  371. return -1;
  372. }
  373. }
  374. }
  375. return DW_EH_PE_native|DW_EH_PE_abs;
  376. }
  377. static int advance_loc(unsigned long delta, struct unwind_state *state)
  378. {
  379. state->loc += delta * state->codeAlign;
  380. return delta > 0;
  381. }
  382. static void set_rule(uleb128_t reg,
  383. enum item_location where,
  384. uleb128_t value,
  385. struct unwind_state *state)
  386. {
  387. if (reg < ARRAY_SIZE(state->regs)) {
  388. state->regs[reg].where = where;
  389. state->regs[reg].value = value;
  390. }
  391. }
  392. static int processCFI(const u8 *start,
  393. const u8 *end,
  394. unsigned long targetLoc,
  395. signed ptrType,
  396. struct unwind_state *state)
  397. {
  398. union {
  399. const u8 *p8;
  400. const u16 *p16;
  401. const u32 *p32;
  402. } ptr;
  403. int result = 1;
  404. if (start != state->cieStart) {
  405. state->loc = state->org;
  406. result = processCFI(state->cieStart, state->cieEnd, 0, ptrType, state);
  407. if (targetLoc == 0 && state->label == NULL)
  408. return result;
  409. }
  410. for (ptr.p8 = start; result && ptr.p8 < end; ) {
  411. switch(*ptr.p8 >> 6) {
  412. uleb128_t value;
  413. case 0:
  414. switch(*ptr.p8++) {
  415. case DW_CFA_nop:
  416. break;
  417. case DW_CFA_set_loc:
  418. if ((state->loc = read_pointer(&ptr.p8, end, ptrType)) == 0)
  419. result = 0;
  420. break;
  421. case DW_CFA_advance_loc1:
  422. result = ptr.p8 < end && advance_loc(*ptr.p8++, state);
  423. break;
  424. case DW_CFA_advance_loc2:
  425. result = ptr.p8 <= end + 2
  426. && advance_loc(*ptr.p16++, state);
  427. break;
  428. case DW_CFA_advance_loc4:
  429. result = ptr.p8 <= end + 4
  430. && advance_loc(*ptr.p32++, state);
  431. break;
  432. case DW_CFA_offset_extended:
  433. value = get_uleb128(&ptr.p8, end);
  434. set_rule(value, Memory, get_uleb128(&ptr.p8, end), state);
  435. break;
  436. case DW_CFA_val_offset:
  437. value = get_uleb128(&ptr.p8, end);
  438. set_rule(value, Value, get_uleb128(&ptr.p8, end), state);
  439. break;
  440. case DW_CFA_offset_extended_sf:
  441. value = get_uleb128(&ptr.p8, end);
  442. set_rule(value, Memory, get_sleb128(&ptr.p8, end), state);
  443. break;
  444. case DW_CFA_val_offset_sf:
  445. value = get_uleb128(&ptr.p8, end);
  446. set_rule(value, Value, get_sleb128(&ptr.p8, end), state);
  447. break;
  448. case DW_CFA_restore_extended:
  449. case DW_CFA_undefined:
  450. case DW_CFA_same_value:
  451. set_rule(get_uleb128(&ptr.p8, end), Nowhere, 0, state);
  452. break;
  453. case DW_CFA_register:
  454. value = get_uleb128(&ptr.p8, end);
  455. set_rule(value,
  456. Register,
  457. get_uleb128(&ptr.p8, end), state);
  458. break;
  459. case DW_CFA_remember_state:
  460. if (ptr.p8 == state->label) {
  461. state->label = NULL;
  462. return 1;
  463. }
  464. if (state->stackDepth >= MAX_STACK_DEPTH)
  465. return 0;
  466. state->stack[state->stackDepth++] = ptr.p8;
  467. break;
  468. case DW_CFA_restore_state:
  469. if (state->stackDepth) {
  470. const uleb128_t loc = state->loc;
  471. const u8 *label = state->label;
  472. state->label = state->stack[state->stackDepth - 1];
  473. memcpy(&state->cfa, &badCFA, sizeof(state->cfa));
  474. memset(state->regs, 0, sizeof(state->regs));
  475. state->stackDepth = 0;
  476. result = processCFI(start, end, 0, ptrType, state);
  477. state->loc = loc;
  478. state->label = label;
  479. } else
  480. return 0;
  481. break;
  482. case DW_CFA_def_cfa:
  483. state->cfa.reg = get_uleb128(&ptr.p8, end);
  484. /*nobreak*/
  485. case DW_CFA_def_cfa_offset:
  486. state->cfa.offs = get_uleb128(&ptr.p8, end);
  487. break;
  488. case DW_CFA_def_cfa_sf:
  489. state->cfa.reg = get_uleb128(&ptr.p8, end);
  490. /*nobreak*/
  491. case DW_CFA_def_cfa_offset_sf:
  492. state->cfa.offs = get_sleb128(&ptr.p8, end)
  493. * state->dataAlign;
  494. break;
  495. case DW_CFA_def_cfa_register:
  496. state->cfa.reg = get_uleb128(&ptr.p8, end);
  497. break;
  498. /*todo case DW_CFA_def_cfa_expression: */
  499. /*todo case DW_CFA_expression: */
  500. /*todo case DW_CFA_val_expression: */
  501. case DW_CFA_GNU_args_size:
  502. get_uleb128(&ptr.p8, end);
  503. break;
  504. case DW_CFA_GNU_negative_offset_extended:
  505. value = get_uleb128(&ptr.p8, end);
  506. set_rule(value,
  507. Memory,
  508. (uleb128_t)0 - get_uleb128(&ptr.p8, end), state);
  509. break;
  510. case DW_CFA_GNU_window_save:
  511. default:
  512. result = 0;
  513. break;
  514. }
  515. break;
  516. case 1:
  517. result = advance_loc(*ptr.p8++ & 0x3f, state);
  518. break;
  519. case 2:
  520. value = *ptr.p8++ & 0x3f;
  521. set_rule(value, Memory, get_uleb128(&ptr.p8, end), state);
  522. break;
  523. case 3:
  524. set_rule(*ptr.p8++ & 0x3f, Nowhere, 0, state);
  525. break;
  526. }
  527. if (ptr.p8 > end)
  528. result = 0;
  529. if (result && targetLoc != 0 && targetLoc < state->loc)
  530. return 1;
  531. }
  532. return result
  533. && ptr.p8 == end
  534. && (targetLoc == 0
  535. || (/*todo While in theory this should apply, gcc in practice omits
  536. everything past the function prolog, and hence the location
  537. never reaches the end of the function.
  538. targetLoc < state->loc &&*/ state->label == NULL));
  539. }
  540. /* Unwind to previous to frame. Returns 0 if successful, negative
  541. * number in case of an error. */
  542. int unwind(struct unwind_frame_info *frame)
  543. {
  544. #define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
  545. const u32 *fde = NULL, *cie = NULL;
  546. const u8 *ptr = NULL, *end = NULL;
  547. unsigned long pc = UNW_PC(frame) - frame->call_frame;
  548. unsigned long startLoc = 0, endLoc = 0, cfa;
  549. unsigned i;
  550. signed ptrType = -1;
  551. uleb128_t retAddrReg = 0;
  552. struct unwind_table *table;
  553. struct unwind_state state;
  554. if (UNW_PC(frame) == 0)
  555. return -EINVAL;
  556. if ((table = find_table(pc)) != NULL
  557. && !(table->size & (sizeof(*fde) - 1))) {
  558. unsigned long tableSize = table->size;
  559. for (fde = table->address;
  560. tableSize > sizeof(*fde) && tableSize - sizeof(*fde) >= *fde;
  561. tableSize -= sizeof(*fde) + *fde,
  562. fde += 1 + *fde / sizeof(*fde)) {
  563. if (!*fde || (*fde & (sizeof(*fde) - 1)))
  564. break;
  565. if (!fde[1])
  566. continue; /* this is a CIE */
  567. if ((fde[1] & (sizeof(*fde) - 1))
  568. || fde[1] > (unsigned long)(fde + 1)
  569. - (unsigned long)table->address)
  570. continue; /* this is not a valid FDE */
  571. cie = fde + 1 - fde[1] / sizeof(*fde);
  572. if (*cie <= sizeof(*cie) + 4
  573. || *cie >= fde[1] - sizeof(*fde)
  574. || (*cie & (sizeof(*cie) - 1))
  575. || cie[1]
  576. || (ptrType = fde_pointer_type(cie)) < 0) {
  577. cie = NULL; /* this is not a (valid) CIE */
  578. continue;
  579. }
  580. ptr = (const u8 *)(fde + 2);
  581. startLoc = read_pointer(&ptr,
  582. (const u8 *)(fde + 1) + *fde,
  583. ptrType);
  584. endLoc = startLoc
  585. + read_pointer(&ptr,
  586. (const u8 *)(fde + 1) + *fde,
  587. ptrType & DW_EH_PE_indirect
  588. ? ptrType
  589. : ptrType & (DW_EH_PE_FORM|DW_EH_PE_signed));
  590. if (pc >= startLoc && pc < endLoc)
  591. break;
  592. cie = NULL;
  593. }
  594. }
  595. if (cie != NULL) {
  596. memset(&state, 0, sizeof(state));
  597. state.cieEnd = ptr; /* keep here temporarily */
  598. ptr = (const u8 *)(cie + 2);
  599. end = (const u8 *)(cie + 1) + *cie;
  600. frame->call_frame = 1;
  601. if ((state.version = *ptr) != 1)
  602. cie = NULL; /* unsupported version */
  603. else if (*++ptr) {
  604. /* check if augmentation size is first (and thus present) */
  605. if (*ptr == 'z') {
  606. while (++ptr < end && *ptr) {
  607. switch(*ptr) {
  608. /* check for ignorable (or already handled)
  609. * nul-terminated augmentation string */
  610. case 'L':
  611. case 'P':
  612. case 'R':
  613. continue;
  614. case 'S':
  615. frame->call_frame = 0;
  616. continue;
  617. default:
  618. break;
  619. }
  620. break;
  621. }
  622. }
  623. if (ptr >= end || *ptr)
  624. cie = NULL;
  625. }
  626. ++ptr;
  627. }
  628. if (cie != NULL) {
  629. /* get code aligment factor */
  630. state.codeAlign = get_uleb128(&ptr, end);
  631. /* get data aligment factor */
  632. state.dataAlign = get_sleb128(&ptr, end);
  633. if (state.codeAlign == 0 || state.dataAlign == 0 || ptr >= end)
  634. cie = NULL;
  635. else {
  636. retAddrReg = state.version <= 1 ? *ptr++ : get_uleb128(&ptr, end);
  637. /* skip augmentation */
  638. if (((const char *)(cie + 2))[1] == 'z')
  639. ptr += get_uleb128(&ptr, end);
  640. if (ptr > end
  641. || retAddrReg >= ARRAY_SIZE(reg_info)
  642. || REG_INVALID(retAddrReg)
  643. || reg_info[retAddrReg].width != sizeof(unsigned long))
  644. cie = NULL;
  645. }
  646. }
  647. if (cie != NULL) {
  648. state.cieStart = ptr;
  649. ptr = state.cieEnd;
  650. state.cieEnd = end;
  651. end = (const u8 *)(fde + 1) + *fde;
  652. /* skip augmentation */
  653. if (((const char *)(cie + 2))[1] == 'z') {
  654. uleb128_t augSize = get_uleb128(&ptr, end);
  655. if ((ptr += augSize) > end)
  656. fde = NULL;
  657. }
  658. }
  659. if (cie == NULL || fde == NULL) {
  660. #ifdef CONFIG_FRAME_POINTER
  661. unsigned long top, bottom;
  662. #endif
  663. #ifdef CONFIG_FRAME_POINTER
  664. top = STACK_TOP(frame->task);
  665. bottom = STACK_BOTTOM(frame->task);
  666. # if FRAME_RETADDR_OFFSET < 0
  667. if (UNW_SP(frame) < top
  668. && UNW_FP(frame) <= UNW_SP(frame)
  669. && bottom < UNW_FP(frame)
  670. # else
  671. if (UNW_SP(frame) > top
  672. && UNW_FP(frame) >= UNW_SP(frame)
  673. && bottom > UNW_FP(frame)
  674. # endif
  675. && !((UNW_SP(frame) | UNW_FP(frame))
  676. & (sizeof(unsigned long) - 1))) {
  677. unsigned long link;
  678. if (!__get_user(link,
  679. (unsigned long *)(UNW_FP(frame)
  680. + FRAME_LINK_OFFSET))
  681. # if FRAME_RETADDR_OFFSET < 0
  682. && link > bottom && link < UNW_FP(frame)
  683. # else
  684. && link > UNW_FP(frame) && link < bottom
  685. # endif
  686. && !(link & (sizeof(link) - 1))
  687. && !__get_user(UNW_PC(frame),
  688. (unsigned long *)(UNW_FP(frame)
  689. + FRAME_RETADDR_OFFSET))) {
  690. UNW_SP(frame) = UNW_FP(frame) + FRAME_RETADDR_OFFSET
  691. # if FRAME_RETADDR_OFFSET < 0
  692. -
  693. # else
  694. +
  695. # endif
  696. sizeof(UNW_PC(frame));
  697. UNW_FP(frame) = link;
  698. return 0;
  699. }
  700. }
  701. #endif
  702. return -ENXIO;
  703. }
  704. state.org = startLoc;
  705. memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
  706. /* process instructions */
  707. if (!processCFI(ptr, end, pc, ptrType, &state)
  708. || state.loc > endLoc
  709. || state.regs[retAddrReg].where == Nowhere
  710. || state.cfa.reg >= ARRAY_SIZE(reg_info)
  711. || reg_info[state.cfa.reg].width != sizeof(unsigned long)
  712. || state.cfa.offs % sizeof(unsigned long))
  713. return -EIO;
  714. /* update frame */
  715. #ifndef CONFIG_AS_CFI_SIGNAL_FRAME
  716. if(frame->call_frame
  717. && !UNW_DEFAULT_RA(state.regs[retAddrReg], state.dataAlign))
  718. frame->call_frame = 0;
  719. #endif
  720. cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs;
  721. startLoc = min((unsigned long)UNW_SP(frame), cfa);
  722. endLoc = max((unsigned long)UNW_SP(frame), cfa);
  723. if (STACK_LIMIT(startLoc) != STACK_LIMIT(endLoc)) {
  724. startLoc = min(STACK_LIMIT(cfa), cfa);
  725. endLoc = max(STACK_LIMIT(cfa), cfa);
  726. }
  727. #ifndef CONFIG_64BIT
  728. # define CASES CASE(8); CASE(16); CASE(32)
  729. #else
  730. # define CASES CASE(8); CASE(16); CASE(32); CASE(64)
  731. #endif
  732. for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
  733. if (REG_INVALID(i)) {
  734. if (state.regs[i].where == Nowhere)
  735. continue;
  736. return -EIO;
  737. }
  738. switch(state.regs[i].where) {
  739. default:
  740. break;
  741. case Register:
  742. if (state.regs[i].value >= ARRAY_SIZE(reg_info)
  743. || REG_INVALID(state.regs[i].value)
  744. || reg_info[i].width > reg_info[state.regs[i].value].width)
  745. return -EIO;
  746. switch(reg_info[state.regs[i].value].width) {
  747. #define CASE(n) \
  748. case sizeof(u##n): \
  749. state.regs[i].value = FRAME_REG(state.regs[i].value, \
  750. const u##n); \
  751. break
  752. CASES;
  753. #undef CASE
  754. default:
  755. return -EIO;
  756. }
  757. break;
  758. }
  759. }
  760. for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
  761. if (REG_INVALID(i))
  762. continue;
  763. switch(state.regs[i].where) {
  764. case Nowhere:
  765. if (reg_info[i].width != sizeof(UNW_SP(frame))
  766. || &FRAME_REG(i, __typeof__(UNW_SP(frame)))
  767. != &UNW_SP(frame))
  768. continue;
  769. UNW_SP(frame) = cfa;
  770. break;
  771. case Register:
  772. switch(reg_info[i].width) {
  773. #define CASE(n) case sizeof(u##n): \
  774. FRAME_REG(i, u##n) = state.regs[i].value; \
  775. break
  776. CASES;
  777. #undef CASE
  778. default:
  779. return -EIO;
  780. }
  781. break;
  782. case Value:
  783. if (reg_info[i].width != sizeof(unsigned long))
  784. return -EIO;
  785. FRAME_REG(i, unsigned long) = cfa + state.regs[i].value
  786. * state.dataAlign;
  787. break;
  788. case Memory: {
  789. unsigned long addr = cfa + state.regs[i].value
  790. * state.dataAlign;
  791. if ((state.regs[i].value * state.dataAlign)
  792. % sizeof(unsigned long)
  793. || addr < startLoc
  794. || addr + sizeof(unsigned long) < addr
  795. || addr + sizeof(unsigned long) > endLoc)
  796. return -EIO;
  797. switch(reg_info[i].width) {
  798. #define CASE(n) case sizeof(u##n): \
  799. __get_user(FRAME_REG(i, u##n), (u##n *)addr); \
  800. break
  801. CASES;
  802. #undef CASE
  803. default:
  804. return -EIO;
  805. }
  806. }
  807. break;
  808. }
  809. }
  810. return 0;
  811. #undef CASES
  812. #undef FRAME_REG
  813. }
  814. EXPORT_SYMBOL(unwind);
  815. int unwind_init_frame_info(struct unwind_frame_info *info,
  816. struct task_struct *tsk,
  817. /*const*/ struct pt_regs *regs)
  818. {
  819. info->task = tsk;
  820. info->call_frame = 0;
  821. arch_unw_init_frame_info(info, regs);
  822. return 0;
  823. }
  824. EXPORT_SYMBOL(unwind_init_frame_info);
  825. /*
  826. * Prepare to unwind a blocked task.
  827. */
  828. int unwind_init_blocked(struct unwind_frame_info *info,
  829. struct task_struct *tsk)
  830. {
  831. info->task = tsk;
  832. info->call_frame = 0;
  833. arch_unw_init_blocked(info);
  834. return 0;
  835. }
  836. EXPORT_SYMBOL(unwind_init_blocked);
  837. /*
  838. * Prepare to unwind the currently running thread.
  839. */
  840. int unwind_init_running(struct unwind_frame_info *info,
  841. asmlinkage int (*callback)(struct unwind_frame_info *,
  842. void *arg),
  843. void *arg)
  844. {
  845. info->task = current;
  846. info->call_frame = 0;
  847. return arch_unwind_init_running(info, callback, arg);
  848. }
  849. EXPORT_SYMBOL(unwind_init_running);
  850. /*
  851. * Unwind until the return pointer is in user-land (or until an error
  852. * occurs). Returns 0 if successful, negative number in case of
  853. * error.
  854. */
  855. int unwind_to_user(struct unwind_frame_info *info)
  856. {
  857. while (!arch_unw_user_mode(info)) {
  858. int err = unwind(info);
  859. if (err < 0)
  860. return err;
  861. }
  862. return 0;
  863. }
  864. EXPORT_SYMBOL(unwind_to_user);